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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [fx2/] [ztex-fpga7.h] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
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
    FPGA support for ZTEX USB FPGA Modules 2.01 and 2.04
28
*/
29
 
30
#ifndef[ZTEX_FPGA_H]
31
#define[ZTEX_FPGA_H]
32
 
33
#define[@CAPABILITY_FPGA;]
34
 
35
__xdata BYTE fpga_checksum;         // checksum
36
__xdata DWORD fpga_bytes;           // transfered bytes
37
__xdata BYTE fpga_init_b;           // init_b state (should be 222 after configuration)
38
__xdata BYTE fpga_flash_result;     // result of automatic fpga configuarion from Flash
39
 
40
__xdata BYTE fpga_conf_initialized; // 123 if initialized
41
__xdata BYTE OOEA;
42
 
43
/* *********************************************************************
44
   ***** reset_fpga ****************************************************
45
   ********************************************************************* */
46
static void reset_fpga () {
47
    OEE = (OEE & ~bmBIT6) | bmBIT7;
48
    IOE = IOE & ~bmBIT7;
49
    wait(1);
50
    IOE = IOE | bmBIT7;
51
    fpga_conf_initialized = 0;
52
}
53
 
54
/* *********************************************************************
55
   ***** init_fpga *****************************************************
56
   ********************************************************************* */
57
static void init_fpga () {
58
    IOE = IOE | bmBIT7;
59
    OEE = (OEE & ~bmBIT6) | bmBIT7;
60
    if ( ! (IOE & bmBIT6) ) {
61
        // ensure that FPGA is in a proper configuration mode
62
        IOE = IOE & ~bmBIT7;                    // PROG_B = 0
63
        OEA = (OEA & bmBIT2 ) | bmBIT4 | bmBIT5 | bmBIT6;
64
        IOA = (IOA & bmBIT2 ) | bmBIT5;
65
        wait(1);
66
        IOE = IOE | bmBIT7;                     // PROG_B = 1
67
 
68
    }
69
    fpga_conf_initialized = 0;
70
}
71
 
72
/* *********************************************************************
73
   ***** init_fpga_configuration ***************************************
74
   ********************************************************************* */
75
static void init_fpga_configuration () {
76
    unsigned short k;
77
 
78
    {
79
        PRE_FPGA_RESET
80
    }
81
 
82
    IFCONFIG = bmBIT7;
83
    SYNCDELAY;
84
    PORTACFG = 0;
85
    PORTCCFG = 0;
86
 
87
    OOEA = OEA;
88
    fpga_conf_initialized = 123;
89
 
90
    OEA &= bmBIT2;                      // only unsed PA bit
91
 
92
    OEE = (OEE & ~bmBIT6) | bmBIT7;
93
    IOE = IOE & ~bmBIT7;                // PROG_B = 0
94
 
95
    //     CSI      M0       M1       RDWR
96
    OEA |= bmBIT1 | bmBIT4 | bmBIT5 | bmBIT6;
97
    IOA = ( IOA & bmBIT2 ) | bmBIT1 | bmBIT5;
98
    wait(5);
99
 
100
    IOE = IOE | bmBIT7;                 // PROG_B = 1
101
    IOA1 = 0;                            // CS = 0
102
 
103
    k=0;
104
    while (!IOA7 && k<65535)
105
        k++;
106
 
107
    //     CCLK 
108
    OEA |= bmBIT0;                      // ready for configuration
109
 
110
    fpga_init_b = IOA7 ? 200 : 100;
111
    fpga_bytes = 0;
112
    fpga_checksum = 0;
113
}
114
 
115
/* *********************************************************************
116
   ***** post_fpga_confog **********************************************
117
   ********************************************************************* */
118
static void post_fpga_config () {
119
    POST_FPGA_CONFIG
120
}
121
 
122
/* *********************************************************************
123
   ***** finish_fpga_configuration *************************************
124
   ********************************************************************* */
125
static void finish_fpga_configuration () {
126
    BYTE w;
127
    fpga_init_b += IOA7 ? 22 : 11;
128
 
129
    for ( w=0; w<64; w++ ) {
130
        IOA0 = 1; IOA0 = 0;
131
    }
132
    IOA1 = 1;
133
    IOA0 = 1; IOA0 = 0;
134
    IOA0 = 1; IOA0 = 0;
135
    IOA0 = 1; IOA0 = 0;
136
    IOA0 = 1; IOA0 = 0;
137
 
138
    OEA = OOEA;
139
    if ( IOE & bmBIT6 )  {
140
        post_fpga_config();
141
    }
142
}
143
 
144
 
145
/* *********************************************************************
146
   ***** EP0 vendor request 0x30 ***************************************
147
   ********************************************************************* */
148
ADD_EP0_VENDOR_REQUEST((0x30,,          // get FPGA state
149
    MEM_COPY1(fpga_checksum,EP0BUF+1,7);
150
 
151
    OEE = (OEE & ~bmBIT6) | bmBIT7;
152
    if ( IOE & bmBIT6 )  {
153
        EP0BUF[0] = 0;                    // FPGA configured 
154
    }
155
    else {
156
        EP0BUF[0] = 1;                   // FPGA unconfigured 
157
        reset_fpga();                   // prepare FPGA for configuration
158
     }
159
//    EP0BUF[8] = 0;                    // bit order for bitstream in Flash memory: non-swapped
160
    EP0BUF[8] = 1;                      // bit order for bitstream in Flash memory: swapped
161
 
162
    EP0BCH = 0;
163
    EP0BCL = 9;
164
,,));;
165
 
166
 
167
/* *********************************************************************
168
   ***** EP0 vendor command 0x31 ***************************************
169
   ********************************************************************* */
170
ADD_EP0_VENDOR_COMMAND((0x31,,reset_fpga();,,));;       // reset FPGA
171
 
172
 
173
/* *********************************************************************
174
   ***** EP0 vendor command 0x32 ***************************************
175
   ********************************************************************* */
176
void fpga_send_ep0() {
177
    BYTE oOEC;
178
    oOEC = OEC;
179
    OEC = 255;
180
    fpga_bytes += ep0_payload_transfer;
181
    __asm
182
        mov     dptr,#_EP0BCL
183
        movx    a,@dptr
184
        jz      010000$
185
        mov     r2,a
186
        mov     _AUTOPTRL1,#(_EP0BUF)
187
        mov     _AUTOPTRH1,#(_EP0BUF >> 8)
188
        mov     _AUTOPTRSETUP,#0x07
189
        mov     dptr,#_fpga_checksum
190
        movx    a,@dptr
191
        mov     r1,a
192
        mov     dptr,#_XAUTODAT1
193
010001$:
194
        movx    a,@dptr                 // 2
195
        mov     _IOC,a                  // 2
196
        setb    _IOA0                   // 2
197
        add     a,r1                    // 1
198
        mov     r1,a                    // 1
199
        clr     _IOA0                   // 2
200
        djnz    r2, 010001$             // 4
201
 
202
        mov     dptr,#_fpga_checksum
203
        mov     a,r1
204
        movx    @dptr,a
205
 
206
010000$:
207
        __endasm;
208
    OEC = oOEC;
209
    if ( EP0BCL<64 ) {
210
        finish_fpga_configuration();
211
    }
212
}
213
 
214
ADD_EP0_VENDOR_COMMAND((0x32,,          // send FPGA configuration data
215
    if ( fpga_conf_initialized != 123 )
216
        init_fpga_configuration();
217
,,
218
    fpga_send_ep0();
219
));;
220
 
221
 
222
#ifeq[FLASH_BITSTREAM_ENABLED][1]
223
/* *********************************************************************
224
   ***** fpga_configure_from_flash *************************************
225
   ********************************************************************* */
226
/*
227
    Configure the FPGA using a bitstream from flash.
228
    If force == 0 a already configured FPGA is not re-configured.
229
    Return values:
230
 
231
        1 : FPGA already configured
232
        4 : Configuration error
233
*/
234
BYTE fpga_configure_from_flash( BYTE force ) {
235
//    BYTE c,d;
236
    WORD i;
237
    BYTE j;
238
 
239
    if ( ( force == 0 ) && ( IOE & bmBIT6 ) ) {
240
        fpga_flash_result = 1;
241
        return 1;
242
    }
243
 
244
    fpga_flash_result = 0;
245
 
246
    IFCONFIG = bmBIT7;
247
    SYNCDELAY;
248
    PORTACFG = 0;
249
    PORTCCFG = 0;
250
 
251
//    c = OEA;
252
    OEA &= bmBIT2;                      // only unsed PA bit
253
 
254
//    d = OEC;
255
    OEC &= ~bmBIT0;
256
 
257
    OEE = (OEE & ~bmBIT6) | bmBIT7;
258
    IOE = IOE & ~bmBIT7;                // PROG_B = 0
259
 
260
    //     M0       M1
261
    OEA |= bmBIT4 | bmBIT5;
262
    IOA = ( IOA & bmBIT2 ) | bmBIT4;
263
    wait(1);
264
 
265
    IOE = IOE | bmBIT7;                 // PROG_B = 1
266
 
267
// wait up to 4s for DONE going high
268
    wait(70);
269
    j=0;
270
    for (i=0; i<4000 && j<250 && !(IOE & bmBIT6); i++ ) {
271
        j = IOA3 ? j+1 : 0;
272
        wait(1);
273
    }
274
 
275
    if ( IOE & bmBIT6 )  {
276
//      IOA = ( IOA & bmBIT2 ) | bmBIT3;
277
        post_fpga_config();
278
//      OEC = d;
279
//      OEA = c;
280
    }
281
    else {
282
        init_fpga();
283
        fpga_flash_result = 4;
284
    }
285
 
286
    return fpga_flash_result;
287
}
288
 
289
#include[ztex-fpga-flash2.h]
290
 
291
#endif
292
 
293
#endif  /*ZTEX_FPGA_H*/

powered by: WebSVN 2.1.0

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