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

Subversion Repositories usb_fpga_2_04

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

powered by: WebSVN 2.1.0

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