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-fpga6.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.13 and 2.16
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 configuration from Flash
39
 
40
__xdata BYTE fpga_conf_initialized; // 123 if initialized
41
__xdata BYTE OOEC;
42
 
43
/* *********************************************************************
44
   ***** reset_fpga ****************************************************
45
   ********************************************************************* */
46
static void reset_fpga () {
47
    OEE = bmBIT7;
48
    IOE = 0;
49
    wait(1);
50
    OEE = 0;
51
    fpga_conf_initialized = 0;
52
}
53
 
54
/* *********************************************************************
55
   ***** init_fpga *****************************************************
56
   ********************************************************************* */
57
static void init_fpga () {
58
    if ( (IOE & bmBIT0) == 0 ) {
59
        // ensure that FPGA is in a proper configuration mode
60
        OEE = bmBIT7;
61
        IOE = 0;
62
        wait(1);
63
    }
64
    OEE = 0;
65
    fpga_conf_initialized = 0;
66
}
67
 
68
/* *********************************************************************
69
   ***** init_fpga_configuration ***************************************
70
   ********************************************************************* */
71
static void init_fpga_configuration () {
72
    unsigned short k;
73
    {
74
        PRE_FPGA_RESET
75
    }
76
    IFCONFIG = bmBIT7;
77
    SYNCDELAY;
78
    PORTCCFG = 0;
79
    PORTECFG = 0;
80
 
81
    OOEC = OEC;
82
    fpga_conf_initialized = 123;
83
 
84
    OEC &= ~( bmBIT7 | bmBIT4);         // in: MOSI, MISO
85
    OEC |= bmBIT6;                      // out: CCLK
86
    IOC6 = 1;
87
//  in:       INIT_B  DONE 
88
//  OEE &= ~( bmBIT1 | bmBIT0 );
89
//  out:     CM0      CM1   RESET_N     CSI     RDWR
90
    OEE = bmBIT3 | bmBIT4 | bmBIT7 | bmBIT2 | bmBIT5;
91
    IOE = bmBIT3;
92
 
93
    wait(2);
94
    IOE = bmBIT3 | bmBIT7;              // ready for configuration
95
    IOC6 = 0;
96
 
97
    k=0;
98
    while (!(IOE & bmBIT1) && k<65535)
99
        k++;
100
 
101
    fpga_init_b = (IOE & bmBIT1) ? 200 : 100;
102
    fpga_bytes = 0;
103
    fpga_checksum = 0;
104
}
105
 
106
/* *********************************************************************
107
   ***** post_fpga_config **********************************************
108
   ********************************************************************* */
109
static void post_fpga_config () {
110
    POST_FPGA_CONFIG
111
}
112
 
113
/* *********************************************************************
114
   ***** finish_fpga_configuration *************************************
115
   ********************************************************************* */
116
static void finish_fpga_configuration () {
117
    BYTE w;
118
    fpga_init_b += (IOE & bmBIT1) ? 22 : 11;
119
 
120
    for ( w=0; w<64; w++ ) {
121
        IOC6 = 1; IOC6 = 0;
122
    }
123
    IOE |= bmBIT2;              // CSI = 1
124
    IOC6 = 1; IOC6 = 0;
125
    IOC6 = 1; IOC6 = 0;
126
    IOC6 = 1; IOC6 = 0;
127
    IOC6 = 1; IOC6 = 0;
128
 
129
    OEE = 0;
130
    OEC = OOEC;
131
    if ( IOE & bmBIT0 )  {
132
        post_fpga_config();
133
    }
134
}
135
 
136
/* *********************************************************************
137
   ***** EP0 vendor request 0x30 ***************************************
138
   ********************************************************************* */
139
ADD_EP0_VENDOR_REQUEST((0x30,,          // get FPGA state
140
    MEM_COPY1(fpga_checksum,EP0BUF+1,7);
141
 
142
    if ( IOE & bmBIT0 )  {
143
        EP0BUF[0] = 0;                    // FPGA configured 
144
    }
145
    else {
146
        EP0BUF[0] = 1;                   // FPGA unconfigured 
147
        OEE = 0;
148
        reset_fpga();                   // prepare FPGA for configuration
149
     }
150
//    EP0BUF[8] = 0;                    // bit order for bitstream in Flash memory: non-swapped
151
    EP0BUF[8] = 1;                      // bit order for bitstream in Flash memory: swapped
152
 
153
    EP0BCH = 0;
154
    EP0BCL = 9;
155
,,));;
156
 
157
 
158
/* *********************************************************************
159
   ***** EP0 vendor command 0x31 ***************************************
160
   ********************************************************************* */
161
ADD_EP0_VENDOR_COMMAND((0x31,,reset_fpga();,,));;       // reset FPGA
162
 
163
 
164
/* *********************************************************************
165
   ***** EP0 vendor command 0x32 ***************************************
166
   ********************************************************************* */
167
void fpga_send_ep0() {                  // send FPGA configuration data
168
    BYTE oOEB;
169
    oOEB = OEB;
170
    OEB = 255;
171
    fpga_bytes += ep0_payload_transfer;
172
    __asm
173
        mov     dptr,#_EP0BCL
174
        movx    a,@dptr
175
        jz      010000$
176
        mov     r2,a
177
        mov     _AUTOPTRL1,#(_EP0BUF)
178
        mov     _AUTOPTRH1,#(_EP0BUF >> 8)
179
        mov     _AUTOPTRSETUP,#0x07
180
        mov     dptr,#_fpga_checksum
181
        movx    a,@dptr
182
        mov     r1,a
183
        mov     dptr,#_XAUTODAT1
184
010001$:
185
        movx    a,@dptr                 // 2
186
        mov     _IOB,a                  // 2
187
        setb    _IOC6                   // 2
188
        add     a,r1                    // 1
189
        mov     r1,a                    // 1
190
        clr     _IOC6                   // 2
191
        djnz    r2, 010001$             // 4
192
 
193
        mov     dptr,#_fpga_checksum
194
        mov     a,r1
195
        movx    @dptr,a
196
 
197
010000$:
198
        __endasm;
199
    OEB = oOEB;
200
    if ( EP0BCL<64 ) {
201
        finish_fpga_configuration();
202
    }
203
}
204
 
205
ADD_EP0_VENDOR_COMMAND((0x32,,          // send FPGA configuration data
206
    if ( fpga_conf_initialized != 123 )
207
        init_fpga_configuration();
208
,,
209
    fpga_send_ep0();
210
));;
211
 
212
 
213
#ifdef[HS_FPGA_CONF_EP]
214
 
215
#ifeq[HS_FPGA_CONF_EP][2]
216
#elifeq[HS_FPGA_CONF_EP][4]
217
#elifeq[HS_FPGA_CONF_EP][6]
218
#elifneq[HS_FPGA_CONF_EP][8]
219
#error[`HS_FPGA_CONF_EP' is not defined correctly. Valid values are: `2', `4', `6', `8'.]
220
#endif
221
 
222
#define[@CAPABILITY_HS_FPGA;]
223
 
224
/* *********************************************************************
225
   ***** EP0 vendor request 0x33 ***************************************
226
   ********************************************************************* */
227
ADD_EP0_VENDOR_REQUEST((0x33,,          // get high speed fpga configuration endpoint and interface 
228
    EP0BUF[0] = HS_FPGA_CONF_EP; // endpoint
229
    EP0BUF[1] = EPHS_FPGA_CONF_EP_INTERFACE; // interface
230
    EP0BCH = 0;
231
    EP0BCL = 2;
232
,,));;
233
 
234
 
235
/* *********************************************************************
236
   ***** EP0 vendor command 0x34 ***************************************
237
   ********************************************************************* */
238
// FIFO write wave form
239
const char __xdata GPIF_WAVE_DATA_HSFPGA_24MHZ[32] =
240
{
241
/* LenBr */ 0x01,     0x88,     0x01,     0x01,     0x01,     0x01,     0x01,     0x07,
242
/* Opcode*/ 0x02,     0x07,     0x02,     0x02,     0x02,     0x02,     0x02,     0x00,
243
/* Output*/ 0x20,     0x00,     0x00,     0x00,     0x00,     0x00,     0x00,     0x20,
244
/* LFun  */ 0x00,     0x36,     0x00,     0x00,     0x00,     0x00,     0x00,     0x3F,
245
};
246
 
247
const char __xdata GPIF_WAVE_DATA_HSFPGA_12MHZ[32] =
248
{
249
/* LenBr */ 0x02,     0x01,     0x90,     0x01,     0x01,     0x01,     0x01,     0x07,
250
/* Opcode*/ 0x02,     0x02,     0x07,     0x02,     0x02,     0x02,     0x02,     0x00,
251
/* Output*/ 0x20,     0x00,     0x00,     0x00,     0x00,     0x00,     0x00,     0x20,
252
/* LFun  */ 0x00,     0x00,     0x36,     0x00,     0x00,     0x00,     0x00,     0x3F,
253
};
254
 
255
 
256
void init_cpld_fpga_configuration() {
257
    IFCONFIG = bmBIT7 | bmBIT6 | 2;     // Internal source, 48MHz, GPIF
258
 
259
    GPIFREADYCFG = 0;
260
    GPIFCTLCFG = 0x0;
261
    GPIFIDLECS = 0;
262
    GPIFIDLECTL = 4;
263
    GPIFWFSELECT = 0x4E;
264
    GPIFREADYSTAT = 0;
265
 
266
    MEM_COPY1(GPIF_WAVE_DATA_HSFPGA_24MHZ,GPIF_WAVE3_DATA,32);
267
 
268
    FLOWSTATE = 0;
269
    FLOWLOGIC = 0x10;
270
    FLOWEQ0CTL = 0;
271
    FLOWEQ1CTL = 0;
272
    FLOWHOLDOFF = 0;
273
    FLOWSTB = 0;
274
    FLOWSTBEDGE = 0;
275
    FLOWSTBHPERIOD = 0;
276
 
277
    REVCTL = 0x1;
278
    SYNCDELAY;
279
 
280
    FIFORESET = 0x80;                           // NAK-ALL
281
    SYNCDELAY;
282
    FIFORESET = HS_FPGA_CONF_EP;                // reset FIFO, undocumented: no NAK bit because it seems to disable (auto-)arming the FIFO
283
    SYNCDELAY;
284
    EPHS_FPGA_CONF_EPFIFOCFG = 0;                // manu-out
285
    SYNCDELAY;
286
    EPHS_FPGA_CONF_EPFIFOCFG = bmBIT4;          // auto-out, arm FIFO
287
    SYNCDELAY;
288
    FIFORESET = 0x0;                            // stop NAK-ALL
289
    SYNCDELAY;
290
 
291
    EPHS_FPGA_CONF_EPGPIFFLGSEL = 1;
292
    SYNCDELAY;
293
 
294
    GPIFTCB3 = 1;                               // abort after at least 14*65536 transactions
295
    SYNCDELAY;
296
    GPIFTCB2 = 0;
297
    SYNCDELAY;
298
    GPIFTCB1 = 0;
299
    SYNCDELAY;
300
    GPIFTCB0 = 0;
301
    SYNCDELAY;
302
 
303
    EPHS_FPGA_CONF_EPGPIFTRIG = 0xff;           // arm FIFOs
304
    SYNCDELAY;
305
 
306
    OEC &= ~bmBIT6;                             // disable CCLK output
307
    IOE = bmBIT4 | bmBIT7;                      // HS config mode
308
}
309
 
310
 
311
ADD_EP0_VENDOR_COMMAND((0x34,,                  // init FPGA configuration
312
    init_fpga_configuration();
313
 
314
    EPHS_FPGA_CONF_EPCS &= ~bmBIT0;             // clear stall bit
315
 
316
    GPIFABORT = 0xFF;                           // abort pending 
317
 
318
    init_cpld_fpga_configuration();
319
 
320
,,));;
321
 
322
 
323
/* *********************************************************************
324
   ***** EP0 vendor command 0x35 ***************************************
325
   ********************************************************************* */
326
ADD_EP0_VENDOR_COMMAND((0x35,,          // finish fpga configuration
327
    IOE = bmBIT3 | bmBIT7;
328
    OEC |= bmBIT6;                      // out: CCLK
329
 
330
    GPIFABORT = 0xFF;
331
    SYNCDELAY;
332
    IFCONFIG &= 0xf0;
333
    SYNCDELAY;
334
 
335
    finish_fpga_configuration();
336
,,));;
337
 
338
#endif  // HS_FPGA_CONF_EP
339
 
340
#ifeq[FLASH_BITSTREAM_ENABLED][1]
341
/* *********************************************************************
342
   ***** fpga_configure_from_flash *************************************
343
   ********************************************************************* */
344
/*
345
    Configure the FPGA using a bitstream from flash.
346
    If force == 0 a already configured FPGA is not re-configured.
347
    Return values:
348
 
349
        1 : FPGA already configured
350
        4 : Configuration error
351
*/
352
#define[SPI_CS][IOSPI_PORTSPI_BIT_CS]
353
#define[SPI_PORT][C]
354
#define[SPI_BIT_DO][4]
355
#define[SPI_BIT_CS][5]
356
#define[SPI_BIT_CLK][6]
357
#define[SPI_BIT_DI][7]
358
 
359
BYTE fpga_configure_from_flash( BYTE force) {
360
    BYTE c,j;
361
    WORD i;
362
 
363
    if ( ( force == 0 ) && ( IOE & bmBIT0 ) ) {
364
        fpga_flash_result = 1;
365
        return 1;
366
    }
367
 
368
    fpga_flash_result = 0;
369
 
370
    c = OESPI_PORT;
371
    OESPI_PORT &= ~( bmBITSPI_BIT_CS | bmBITSPI_BIT_DI | bmBITSPI_BIT_CLK );    // disable SPI outputs
372
 
373
    {
374
        PRE_FPGA_RESET
375
    }
376
 
377
// reset FPGA and start configuration from flash
378
//  out:     CM0      CM1   RESET_N     CSI     RDWR
379
    OEE = bmBIT3 | bmBIT4 | bmBIT7;
380
    IOE = 0;
381
    wait(1);
382
    IOE = bmBIT7;
383
 
384
// wait up to 10s for DONE going high
385
    wait(50);
386
    j=0;
387
    for (i=0; i<10000 && j<250 && !(IOE & bmBIT0); i++ ) {
388
        j = SPI_CS ? j+1 : 0;
389
        wait(1);
390
    }
391
 
392
    if ( IOE & bmBIT0 )  {
393
        post_fpga_config();
394
    }
395
    else {
396
        IOE =  bmBIT3 | bmBIT4; // leave master SPI config mode
397
        wait(1);
398
        fpga_flash_result = 4;
399
    }
400
    OEE = 0;
401
 
402
    OESPI_PORT = c;
403
    SPI_CS = 1;
404
 
405
    return fpga_flash_result;
406
}
407
 
408
#include[ztex-fpga-flash2.h]
409
 
410
#endif
411
 
412
#endif  /*ZTEX_FPGA_H*/

powered by: WebSVN 2.1.0

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