1 |
2 |
ZTEX |
/*%
|
2 |
|
|
ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
|
3 |
|
|
Copyright (C) 2009-2017 ZTEX GmbH.
|
4 |
|
|
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
7 |
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
8 |
|
|
You can obtain one at http://mozilla.org/MPL/2.0/.
|
9 |
|
|
|
10 |
|
|
Alternatively, the contents of this file may be used under the terms
|
11 |
|
|
of the GNU General Public License Version 3, as described below:
|
12 |
|
|
|
13 |
|
|
This program is free software; you can redistribute it and/or modify
|
14 |
|
|
it under the terms of the GNU General Public License version 3 as
|
15 |
|
|
published by the Free Software Foundation.
|
16 |
|
|
|
17 |
|
|
This program is distributed in the hope that it will be useful, but
|
18 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
19 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
20 |
|
|
General Public License for more details.
|
21 |
|
|
|
22 |
|
|
You should have received a copy of the GNU General Public License
|
23 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
24 |
|
|
%*/
|
25 |
|
|
|
26 |
|
|
/*
|
27 |
|
|
Common functions for FPGA configuration from SPI flash
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
__code BYTE fpga_flash_boot_id[] = {'Z','T','E', 'X', 'B', 'S', '\1', '\1'};
|
31 |
|
|
|
32 |
|
|
/* *********************************************************************
|
33 |
|
|
***** fpga_first_free_sector ****************************************
|
34 |
|
|
********************************************************************* */
|
35 |
|
|
// First free sector. Returns 0 if no boot sector exeists.
|
36 |
|
|
// Use the macro FLASH_FIRST_FREE_SECTOR instead of this function.
|
37 |
|
|
#ifeq[FLASH_ENABLED][1]
|
38 |
|
|
#define[FLASH_FIRST_FREE_SECTOR][fpga_first_free_sector()];
|
39 |
|
|
WORD fpga_first_free_sector() {
|
40 |
|
|
BYTE i,j;
|
41 |
|
|
#ifdef[@CAPABILITY_MAC_EEPROM;]
|
42 |
|
|
__xdata WORD buf[2];
|
43 |
|
|
|
44 |
|
|
if ( config_data_valid ) {
|
45 |
|
|
mac_eeprom_read ( (__xdata BYTE*) buf, 26, 4 ); // read actual and max bitstream size
|
46 |
|
|
if ( buf[1] != 0 ) {
|
47 |
|
|
return ( ( ( buf[1] > buf[0] ? buf[1] : buf[0] ) - 1 ) >> ((flash_sector_size & 255) - 12) ) + 1;
|
48 |
|
|
}
|
49 |
|
|
}
|
50 |
|
|
#endif
|
51 |
|
|
flash_read_init( 0 ); // prepare reading sector 0
|
52 |
|
|
for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
|
53 |
|
|
if ( i != 8 ) {
|
54 |
|
|
flash_read_finish(flash_sector_size - i); // dummy-read the rest of the sector + finish read opration
|
55 |
|
|
return 0;
|
56 |
|
|
}
|
57 |
|
|
i=flash_read_byte();
|
58 |
|
|
j=flash_read_byte();
|
59 |
|
|
flash_read_finish(flash_sector_size - 10); // dummy-read the rest of the sector + finish read opration
|
60 |
|
|
|
61 |
|
|
return (i | (j<<8))+1;
|
62 |
|
|
}
|
63 |
|
|
#endif
|
64 |
|
|
|
65 |
|
|
/* *********************************************************************
|
66 |
|
|
***** fpga_configure_from_flash_init ********************************
|
67 |
|
|
********************************************************************* */
|
68 |
|
|
// this function is called by init_USB;
|
69 |
|
|
BYTE fpga_configure_from_flash_init() {
|
70 |
|
|
#ifneq[FLASH_BITSTREAM_FORCE_ENABLED][1]
|
71 |
|
|
#ifeq[FLASH_ENABLED][1]
|
72 |
|
|
BYTE i;
|
73 |
|
|
#endif
|
74 |
|
|
#ifdef[@CAPABILITY_MAC_EEPROM;]
|
75 |
|
|
__xdata WORD buf[2];
|
76 |
|
|
|
77 |
|
|
if ( config_data_valid ) {
|
78 |
|
|
mac_eeprom_read ( (__xdata BYTE*) buf, 26, 4 ); // read actual and max bitstream size
|
79 |
|
|
if ( buf[1] != 0 ) {
|
80 |
|
|
if ( buf[0] == 0 ) {
|
81 |
|
|
return fpga_flash_result = 3;
|
82 |
|
|
}
|
83 |
|
|
// return 10;
|
84 |
|
|
goto flash_config;
|
85 |
|
|
}
|
86 |
|
|
// return 15;
|
87 |
|
|
}
|
88 |
|
|
#endif
|
89 |
|
|
#ifeq[FLASH_ENABLED][1]
|
90 |
|
|
// read the boot sector
|
91 |
|
|
if ( flash_read_init( 0 ) ) // prepare reading sector 0
|
92 |
|
|
return fpga_flash_result = 2;
|
93 |
|
|
for ( i=0; i<8 && flash_read_byte()==fpga_flash_boot_id[i]; i++ );
|
94 |
|
|
if ( i != 8 ) {
|
95 |
|
|
flash_read_finish(flash_sector_size - i); // dummy-read the rest of the sector + finish read opration
|
96 |
|
|
return fpga_flash_result = 3;
|
97 |
|
|
}
|
98 |
|
|
i = flash_read_byte();
|
99 |
|
|
i |= flash_read_byte();
|
100 |
|
|
flash_read_finish(flash_sector_size - 10); // dummy-read the rest of the sector + finish read opration
|
101 |
|
|
if ( i==0 )
|
102 |
|
|
return fpga_flash_result = 3;
|
103 |
|
|
#else
|
104 |
|
|
return 2;
|
105 |
|
|
#endif
|
106 |
|
|
#endif
|
107 |
|
|
flash_config:
|
108 |
|
|
fpga_flash_result = fpga_configure_from_flash(0);
|
109 |
|
|
if ( fpga_flash_result == 1 ) {
|
110 |
|
|
post_fpga_config();
|
111 |
|
|
}
|
112 |
|
|
else if ( fpga_flash_result == 4 ) {
|
113 |
|
|
fpga_flash_result = fpga_configure_from_flash(0); // up to two tries
|
114 |
|
|
}
|
115 |
|
|
return fpga_flash_result;
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
#define[INIT_CMDS;][INIT_CMDS;
|
119 |
|
|
fpga_flash_result= 255;
|
120 |
|
|
]
|