1 |
8 |
ZTEX |
/*!
|
2 |
|
|
ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
|
3 |
9 |
ZTEX |
Copyright (C) 2009-2014 ZTEX GmbH.
|
4 |
8 |
ZTEX |
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License version 3 as
|
8 |
|
|
published by the Free Software Foundation.
|
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, but
|
11 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
|
|
General Public License for more details.
|
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License
|
16 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
17 |
|
|
!*/
|
18 |
|
|
|
19 |
|
|
/*
|
20 |
|
|
Common functions for FPGA configuration from microSD flash
|
21 |
|
|
*/
|
22 |
|
|
|
23 |
|
|
|
24 |
|
|
__code BYTE fpga_flash_boot_id[] = {'Z','T','E', 'X', 'B', 'S', '\1', '\1'};
|
25 |
|
|
|
26 |
|
|
/* *********************************************************************
|
27 |
|
|
***** fpga_configure_from_flash *************************************
|
28 |
|
|
********************************************************************* */
|
29 |
|
|
/*
|
30 |
|
|
Configure the FPGA using a bitstream from flash.
|
31 |
|
|
If force == 0 a already configured FPGA is not re-configured.
|
32 |
|
|
Return values:
|
33 |
|
|
|
34 |
|
|
1 : FPGA already configured
|
35 |
|
|
2 : Flash error
|
36 |
|
|
3 : No bitstream found
|
37 |
|
|
4 : Configuration error
|
38 |
|
|
|
39 |
|
|
The format of the boot sector (secor 0 of the flash memeory) is
|
40 |
|
|
0..7 ID, must be "ZTEXBS",1,1
|
41 |
|
|
8..9 Number of used sectors, or 0 is disabled
|
42 |
|
|
10..11 Number of bytes in the last sector, i.e. th total size of bitstream is ((bs[8] | (bs[9]<<8) - 1) * flash_sector_size + ((bs[10] | (bs[11]<<8)
|
43 |
|
|
the rest of the sector is reserved for future use and preserved by the host software
|
44 |
|
|
*/
|
45 |
|
|
|
46 |
|
|
BYTE fpga_configure_from_flash( BYTE force) {
|
47 |
|
|
WORD i,j,k;
|
48 |
|
|
if ( force == 0 ) {
|
49 |
|
|
OEA &= ~bmBIT1;
|
50 |
|
|
if ( IOA1 ) { // FPGA already configured
|
51 |
|
|
IOA1 = 1;
|
52 |
|
|
OEA |= bmBIT1;
|
53 |
|
|
return 1;
|
54 |
|
|
}
|
55 |
|
|
}
|
56 |
|
|
{
|
57 |
|
|
PRE_FPGA_RESET
|
58 |
|
|
}
|
59 |
|
|
reset_fpga_flash(); // reset FPGA
|
60 |
|
|
|
61 |
|
|
// read the boot sector
|
62 |
|
|
if ( flash_read_init( 0 ) ) // prepare reading sector 0
|
63 |
|
|
return 2;
|
64 |
|
|
for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
|
65 |
|
|
if ( i != 8 ) {
|
66 |
|
|
flash_read_finish(flash_sector_size - i); // dummy-read the rest of the sector + finish read opration
|
67 |
|
|
return 3;
|
68 |
|
|
}
|
69 |
|
|
i = flash_read_byte();
|
70 |
|
|
i |= flash_read_byte() << 8;
|
71 |
|
|
j = flash_read_byte();
|
72 |
|
|
j |= flash_read_byte() << 8;
|
73 |
|
|
flash_read_finish(flash_sector_size - 12); // dummy-read the rest of the sector + finish read opration
|
74 |
|
|
if ( i==0 )
|
75 |
|
|
return 3;
|
76 |
|
|
|
77 |
|
|
// read the bitstream
|
78 |
|
|
if ( flash_read_init( 1 ) ) // prepare reading sector 1
|
79 |
|
|
return 2;
|
80 |
|
|
for ( k=1; k<i; k++ ) {
|
81 |
|
|
fpga_send_bitstream_from_flash( flash_sector_size );
|
82 |
|
|
if ( flash_read_next() ) // prepare reading next sector
|
83 |
|
|
return 2;
|
84 |
|
|
}
|
85 |
|
|
fpga_send_bitstream_from_flash( j );
|
86 |
|
|
flash_read_finish(flash_sector_size - j); // finish read opration
|
87 |
|
|
|
88 |
|
|
finish_fpga_configuration(); // finish the FPGA configuration
|
89 |
|
|
|
90 |
|
|
OEA &= ~bmBIT1;
|
91 |
|
|
if ( IOA1 ) { // FPGA configured
|
92 |
|
|
IOA1 = 1;
|
93 |
|
|
OEA |= bmBIT1;
|
94 |
|
|
return 0;
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
IOA1 = 1;
|
98 |
|
|
OEA |= bmBIT1;
|
99 |
|
|
return 4; // FPGA not configured correctly
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/* *********************************************************************
|
103 |
|
|
***** fpga_first_free_sector ****************************************
|
104 |
|
|
********************************************************************* */
|
105 |
|
|
// First free sector. Returns 0 if no boot sector exeists.
|
106 |
|
|
// Use the macro FLASH_FIRST_FREE_SECTOR instead of this function.
|
107 |
|
|
#define[FLASH_FIRST_FREE_SECTOR][fpga_first_free_sector()];
|
108 |
|
|
WORD fpga_first_free_sector() {
|
109 |
|
|
BYTE i,j;
|
110 |
|
|
flash_read_init( 0 ); // prepare reading sector 0
|
111 |
|
|
for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
|
112 |
|
|
if ( i != 8 ) {
|
113 |
|
|
flash_read_finish(flash_sector_size - i); // dummy-read the rest of the sector + finish read opration
|
114 |
|
|
return 0;
|
115 |
|
|
}
|
116 |
|
|
i=flash_read_byte();
|
117 |
|
|
j=flash_read_byte();
|
118 |
|
|
flash_read_finish(flash_sector_size - 10); // dummy-read the rest of the sector + finish read opration
|
119 |
|
|
|
120 |
|
|
return (i | (j<<8))+1;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
/* *********************************************************************
|
125 |
|
|
***** fpga_configure_from_flash_init ********************************
|
126 |
|
|
********************************************************************* */
|
127 |
|
|
// this function is called by init_USB;
|
128 |
|
|
void fpga_configure_from_flash_init() {
|
129 |
|
|
if ( ! flash_enabled ) {
|
130 |
|
|
fpga_flash_result = 2;
|
131 |
|
|
return;
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
fpga_flash_result = fpga_configure_from_flash(0);
|
135 |
|
|
if ( fpga_flash_result == 1 ) {
|
136 |
|
|
post_fpga_config();
|
137 |
|
|
}
|
138 |
|
|
else if ( fpga_flash_result == 4 ) {
|
139 |
|
|
fpga_flash_result = fpga_configure_from_flash(0); // up to two tries
|
140 |
|
|
}
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
|
144 |
|
|
#define[INIT_CMDS;][INIT_CMDS;
|
145 |
|
|
fpga_flash_result= 255;
|
146 |
|
|
]
|