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

Subversion Repositories openarty

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

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

powered by: WebSVN 2.1.0

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