OpenCores
URL https://opencores.org/ocsvn/openarty/openarty/trunk

Subversion Repositories openarty

[/] [openarty/] [trunk/] [sw/] [host/] [wbprogram.cpp] - Blame information for rev 51

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 18 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 4 dgisselq
//
3
// Filename:    wbprogram.cpp
4
//
5 18 dgisselq
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
6 4 dgisselq
//
7 51 dgisselq
// Purpose:     Program the memory with a given '.bin' file onto the flash
8
//              memory on a given board.
9 4 dgisselq
//
10 18 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
11
//              Gisselquist Technology, LLC
12 4 dgisselq
//
13 18 dgisselq
////////////////////////////////////////////////////////////////////////////////
14 4 dgisselq
//
15 51 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
16 4 dgisselq
//
17 18 dgisselq
// This program is free software (firmware): you can redistribute it and/or
18
// modify it under the terms of  the GNU General Public License as published
19
// by the Free Software Foundation, either version 3 of the License, or (at
20
// your option) any later version.
21
//
22
// This program is distributed in the hope that it will be useful, but WITHOUT
23
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
24
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
25
// for more details.
26
//
27
// You should have received a copy of the GNU General Public License along
28 51 dgisselq
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
29 18 dgisselq
// target there if the PDF file isn't present.)  If not, see
30
// <http://www.gnu.org/licenses/> for a copy.
31
//
32
// License:     GPL, v3, as defined and found on www.gnu.org,
33
//              http://www.gnu.org/licenses/gpl.html
34
//
35
//
36
////////////////////////////////////////////////////////////////////////////////
37
//
38
//
39
//
40 4 dgisselq
#include <stdio.h>
41
#include <stdlib.h>
42
#include <unistd.h>
43
#include <strings.h>
44
#include <ctype.h>
45
#include <string.h>
46
#include <signal.h>
47
#include <assert.h>
48
 
49
#include "port.h"
50
#include "llcomms.h"
51
#include "regdefs.h"
52
#include "flashdrvr.h"
53 51 dgisselq
#include "byteswap.h"
54 4 dgisselq
 
55
DEVBUS  *m_fpga;
56
void    closeup(int v) {
57
        m_fpga->kill();
58
        exit(0);
59
}
60
 
61 30 dgisselq
void    usage(void) {
62
        printf("USAGE: wbprogram [@<Address>] file.bit\n");
63
        printf("\tYou can also use a .bin file in place of the file.bit.\n");
64
}
65
 
66 51 dgisselq
void    skip_bitfile_header(FILE *fp) {
67
        const unsigned  SEARCHLN = 204, MATCHLN = 52;
68
        const unsigned char matchstr[MATCHLN] = {
69
                0xff, 0xff, 0xff, 0xff,
70
                0xff, 0xff, 0xff, 0xff,
71
                0xff, 0xff, 0xff, 0xff,
72
                0xff, 0xff, 0xff, 0xff,
73
                //
74
                0xff, 0xff, 0xff, 0xff,
75
                0xff, 0xff, 0xff, 0xff,
76
                0xff, 0xff, 0xff, 0xff,
77
                0xff, 0xff, 0xff, 0xff,
78
                //
79
                0x00, 0x00, 0x00, 0xbb,
80
                0x11, 0x22, 0x00, 0x44,
81
                0xff, 0xff, 0xff, 0xff,
82
                0xff, 0xff, 0xff, 0xff,
83
                //
84
                0xaa, 0x99, 0x55, 0x66 };
85
        unsigned char   buf[SEARCHLN];
86
 
87
        rewind(fp);
88
        fread(buf, sizeof(char), SEARCHLN, fp);
89
        for(int start=0; start+MATCHLN<SEARCHLN; start++) {
90
                int     mloc;
91
 
92
                // Search backwards, since the starting bytes just aren't that
93
                // interesting.
94
                for(mloc = MATCHLN-1; mloc >= 0; mloc--)
95
                        if (buf[start+mloc] != matchstr[mloc])
96
                                break;
97
                if (mloc < 0) {
98
                        fseek(fp, start, SEEK_SET);
99
                        return;
100
                }
101
        }
102
 
103
        fprintf(stderr, "Could not find bin-file header within bit file\n");
104
        fclose(fp);
105
        exit(EXIT_FAILURE);
106
}
107
 
108 4 dgisselq
int main(int argc, char **argv) {
109
        FILE    *fp;
110 51 dgisselq
        DEVBUS::BUSW    addr = EQSPIFLASH;
111
        char            *buf = new char[FLASHLEN];
112 4 dgisselq
        FLASHDRVR       *flash;
113
        int             argn = 1;
114
 
115
        if ((argc > argn)&&(NULL != strstr(argv[argn],"tty")))
116
                m_fpga = new FPGA(new TTYCOMMS(argv[argn++]));
117
        else if ((argc > argn)&&(NULL != strchr(argv[argn],':'))) {
118
                char *ptr = strchr(argv[argn],':');
119
                *ptr++ = '\n';
120
                m_fpga = new FPGA(new NETCOMMS(argv[argn++], atoi(ptr)));
121
        } else {
122
                FPGAOPEN(m_fpga);
123
        }
124
 
125
        // Start with testing the version:
126
        try {
127
                printf("VERSION: %08x\n", m_fpga->readio(R_VERSION));
128
        } catch(BUSERR b) {
129
                printf("VERSION: (Bus-Err)\n");
130
                exit(-1);
131
        }
132
 
133
        unsigned        sz;
134
 
135
        argn = 1;
136
        if (argc <= argn) {
137 30 dgisselq
                usage();
138 4 dgisselq
                exit(-1);
139
        } else if (argv[argn][0] == '@') {
140
                addr = strtoul(&argv[argn][1], NULL, 0);
141 14 dgisselq
                if ((addr < EQSPIFLASH)||(addr > EQSPIFLASH*2)) {
142 4 dgisselq
                        printf("BAD ADDRESS: 0x%08x (from %s)\n", addr, argv[argn]);
143 30 dgisselq
                        printf("The address you've selected, 0x%08x, is outside the range", addr);
144
                        printf("from 0x%08x to 0x%08x\n", EQSPIFLASH, EQSPIFLASH*2);
145 4 dgisselq
                        exit(-1);
146
                } argn++;
147
        }
148
 
149
        if (argc<= argn) {
150
                printf("BAD USAGE: no file argument\n");
151
                exit(-1);
152
        } else if (0 != access(argv[argn], R_OK)) {
153
                printf("Cannot access %s\n", argv[argn]);
154
                exit(-1);
155
        }
156
 
157
        flash = new FLASHDRVR(m_fpga);
158
 
159 30 dgisselq
        if ((strcmp(&argv[argn][strlen(argv[argn])-4],".bit")!=0)
160
                &&(strcmp(&argv[argn][strlen(argv[argn])-4],".bin")!=0)) {
161
                printf("I'm expecting a '.bit' or \'.bin\' file extension\n");
162
                exit(-1);
163
        }
164
 
165 4 dgisselq
        fp = fopen(argv[argn], "r");
166 30 dgisselq
        if (strcmp(&argv[argn][strlen(argv[argn])-4],".bit")==0)
167 51 dgisselq
                skip_bitfile_header(fp);
168
        sz = fread(buf, sizeof(buf[0]), FLASHLEN, fp);
169 4 dgisselq
        fclose(fp);
170
 
171
        try {
172 14 dgisselq
                flash->write(addr, sz, buf, true);
173 4 dgisselq
        } catch(BUSERR b) {
174
                fprintf(stderr, "BUS-ERR @0x%08x\n", b.addr);
175
                exit(-1);
176
        }
177
 
178
        try {
179
                // Turn on the write protect flag
180
                m_fpga->writeio(R_QSPI_EREG, 0);
181
        } catch(BUSERR b) {
182
                fprintf(stderr, "BUS-ERR, trying to read QSPI port\n");
183
                exit(-1);
184
        }
185
 
186 30 dgisselq
        printf("ALL-DONE\n");
187 4 dgisselq
        delete  m_fpga;
188
}
189
 
190
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.