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

Subversion Repositories usb_fpga_1_2

[/] [usb_fpga_1_2/] [trunk/] [include/] [ztex-fpga4.h] - Blame information for rev 8

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 8 ZTEX
/*!
2
   ZTEX Firmware Kit for EZ-USB FX2 Microcontrollers
3
   Copyright (C) 2009-2011 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 1.15
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
/* *********************************************************************
34
   ***** reset_fpga ****************************************************
35
   ********************************************************************* */
36
static void reset_fpga_int (BYTE mode) {                // reset FPGA
37
    unsigned short k;
38
    IFCONFIG = bmBIT7;
39
    SYNCDELAY;
40
    PORTACFG = 0;
41
    PORTCCFG = 0;
42
    OEC &= ~( bmBIT1 | bmBIT2);  // in: DOUT, INIT_B
43
//  out:  RESET,   M1,      CCLK,    M0,      CSI
44
    OEA = bmBIT1 | bmBIT2 | bmBIT4 | bmBIT5 | bmBIT7;
45
    IOA = bmBIT7 | bmBIT4 | mode;
46
    OEC |= bmBIT3;              // out: RDWR_B
47
    IOC &= ~bmBIT3;
48
    wait(10);
49
 
50
    IOA = bmBIT1 | mode;                                // ready for configuration
51
    k=0;
52
    while (!IOC2 && k<65535)
53
        k++;
54
 
55
    fpga_init_b = IOC2 ? 200 : 100;
56
    fpga_bytes = 0;
57
    fpga_checksum = 0;
58
}
59
 
60
static void reset_fpga () {
61
    reset_fpga_int(bmBIT2);
62
}
63
 
64
static void reset_fpga_flash () {
65
    reset_fpga_int(bmBIT2 | bmBIT4 | bmBIT5 );
66
}
67
 
68
/* *********************************************************************
69
   ***** init_fpga_configuration ***************************************
70
   ********************************************************************* */
71
static void init_fpga_configuration () {
72
    {
73
        PRE_FPGA_RESET
74
    }
75
    reset_fpga();                       // reset FPGA
76
}
77
 
78
/* *********************************************************************
79
   ***** post_fpga_confog **********************************************
80
   ********************************************************************* */
81
static void post_fpga_config () {
82
    POST_FPGA_CONFIG
83
}
84
 
85
/* *********************************************************************
86
   ***** finish_fpga_configuration *************************************
87
   ********************************************************************* */
88
static void finish_fpga_configuration () {
89
    WORD w;
90
    fpga_init_b += IOC2 ? 20 : 10;
91
 
92
    for ( w=0; w<64; w++ ) {
93
        IOA4 = 1; IOA4 = 0;
94
    }
95
    IOA7 = 1;
96
    IOA4 = 1; IOA4 = 0;
97
    IOA4 = 1; IOA4 = 0;
98
    IOA4 = 1; IOA4 = 0;
99
    IOA4 = 1; IOA4 = 0;
100
 
101
    OEA = 0;
102
    OEC &= ~bmBIT3;
103
    fpga_init_b += IOC2 ? 2 : 1;
104
    if ( IOA1 )  {
105
        IOA1 = 1;
106
        post_fpga_config();
107
    }
108
 
109
    IOA1 = 1;
110
    OEA |= bmBIT1;
111
}
112
 
113
 
114
/* *********************************************************************
115
   ***** EP0 vendor request 0x30 ***************************************
116
   ********************************************************************* */
117
ADD_EP0_VENDOR_REQUEST((0x30,,          // get FPGA state
118
    MEM_COPY1(fpga_checksum,EP0BUF+1,7);
119
    OEA &= ~bmBIT1;
120
    if ( IOA1 )  {
121
        EP0BUF[0] = 0;                    // FPGA configured 
122
        IOA1 = 1;
123
        OEA |= bmBIT1;
124
    }
125
    else {
126
        EP0BUF[0] = 1;                   // FPGA unconfigured 
127
        reset_fpga();                   // prepare FPGA for configuration
128
    }
129
    EP0BUF[8] = 1;                      // bit order for bitstream in Flash memory: swapped
130
 
131
    EP0BCH = 0;
132
    EP0BCL = 9;
133
,,));;
134
 
135
 
136
/* *********************************************************************
137
   ***** EP0 vendor command 0x31 ***************************************
138
   ********************************************************************* */
139
ADD_EP0_VENDOR_COMMAND((0x31,,init_fpga_configuration();,,));;  // reset FPGA
140
 
141
 
142
/* *********************************************************************
143
   ***** EP0 vendor command 0x32 ***************************************
144
   ********************************************************************* */
145
void fpga_send_ep0() {                  // send FPGA configuration data
146
    BYTE oOEB;
147
    oOEB = OEB;
148
    OEB = 255;
149
    fpga_bytes += ep0_payload_transfer;
150
    __asm
151
        mov     dptr,#_EP0BCL
152
        movx    a,@dptr
153
        jz      010000$
154
        mov     r2,a
155
        mov     _AUTOPTRL1,#(_EP0BUF)
156
        mov     _AUTOPTRH1,#(_EP0BUF >> 8)
157
        mov     _AUTOPTRSETUP,#0x07
158
        mov     dptr,#_fpga_checksum
159
        movx    a,@dptr
160
        mov     r1,a
161
        mov     dptr,#_XAUTODAT1
162
010001$:
163
        movx    a,@dptr                 // 2
164
        mov     _IOB,a                  // 2
165
        setb    _IOA4                   // 2
166
        add     a,r1                    // 1
167
        mov     r1,a                    // 1
168
        clr     _IOA4                   // 2
169
        djnz    r2, 010001$             // 4
170
 
171
        mov     dptr,#_fpga_checksum
172
        mov     a,r1
173
        movx    @dptr,a
174
 
175
010000$:
176
        __endasm;
177
    OEB = oOEB;
178
    if ( EP0BCL<64 ) {
179
        finish_fpga_configuration();
180
    }
181
}
182
 
183
ADD_EP0_VENDOR_COMMAND((0x32,,          // send FPGA configuration data
184
,,
185
    fpga_send_ep0();
186
));;
187
 
188
 
189
#ifdef[HS_FPGA_CONF_EP]
190
 
191
#ifeq[HS_FPGA_CONF_EP][2]
192
#elifeq[HS_FPGA_CONF_EP][4]
193
#elifeq[HS_FPGA_CONF_EP][6]
194
#elifneq[HS_FPGA_CONF_EP][8]
195
#error[`HS_FPGA_CONF_EP' is not defined correctly. Valid values are: `2', `4', `6', `8'.]
196
#endif
197
 
198
#define[@CAPABILITY_HS_FPGA;]
199
 
200
/* *********************************************************************
201
   ***** EP0 vendor request 0x33 ***************************************
202
   ********************************************************************* */
203
ADD_EP0_VENDOR_REQUEST((0x33,,          // get high speed fpga configuration endpoint and interface 
204
    EP0BUF[0] = HS_FPGA_CONF_EP; // endpoint
205
    EP0BUF[1] = EPHS_FPGA_CONF_EP_INTERFACE; // interface
206
    EP0BCH = 0;
207
    EP0BCL = 2;
208
,,));;
209
 
210
/* *********************************************************************
211
   ***** EP0 vendor command 0x34 ***************************************
212
   ********************************************************************* */
213
// FIFO write wave form
214
const char __xdata GPIF_WAVE_DATA_HSFPGA_24MHZ[32] =
215
{
216
/* LenBr */ 0x01,     0x88,     0x01,     0x01,     0x01,     0x01,     0x01,     0x07,
217
/* Opcode*/ 0x02,     0x07,     0x02,     0x02,     0x02,     0x02,     0x02,     0x00,
218
/* Output*/ 0x04,     0x00,     0x00,     0x00,     0x00,     0x00,     0x00,     0x04,
219
/* LFun  */ 0x00,     0x36,     0x00,     0x00,     0x00,     0x00,     0x00,     0x3F,
220
};
221
 
222
const char __xdata GPIF_WAVE_DATA_HSFPGA_12MHZ[32] =
223
{
224
/* LenBr */ 0x02,     0x01,     0x90,     0x01,     0x01,     0x01,     0x01,     0x07,
225
/* Opcode*/ 0x02,     0x02,     0x07,     0x02,     0x02,     0x02,     0x02,     0x00,
226
/* Output*/ 0x04,     0x00,     0x00,     0x00,     0x00,     0x00,     0x00,     0x04,
227
/* LFun  */ 0x00,     0x00,     0x36,     0x00,     0x00,     0x00,     0x00,     0x3F,
228
};
229
 
230
 
231
 
232
ADD_EP0_VENDOR_COMMAND((0x34,,                  // init fpga configuration
233
    init_fpga_configuration();
234
 
235
    EPHS_FPGA_CONF_EPCS &= ~bmBIT0;             // clear stall bit
236
 
237
    GPIFABORT = 0xFF;                           // abort pendig 
238
 
239
    IFCONFIG = bmBIT7 | bmBIT6 | bmBIT5 | 2;    // Internal source, 48MHz, GPIF
240
 
241
    GPIFREADYCFG = 0; //bmBIT7 | bmBIT6 | bmBIT5;
242
    GPIFCTLCFG = 0x0;
243
    GPIFIDLECS = 0;
244
    GPIFIDLECTL = 4;
245
    GPIFWFSELECT = 0x4E;
246
    GPIFREADYSTAT = 0;
247
 
248
    MEM_COPY1(GPIF_WAVE_DATA_HSFPGA_12MHZ,GPIF_WAVE3_DATA,32);
249
 
250
    FLOWSTATE = 0;
251
    FLOWLOGIC = 0x10;
252
    FLOWEQ0CTL = 0;
253
    FLOWEQ1CTL = 0;
254
    FLOWHOLDOFF = 0;
255
    FLOWSTB = 0;
256
    FLOWSTBEDGE = 0;
257
    FLOWSTBHPERIOD = 0;
258
 
259
    REVCTL = 0x1;                               // reset fifo
260
    SYNCDELAY;
261
    FIFORESET = 0x80;
262
    SYNCDELAY;
263
    FIFORESET = HS_FPGA_CONF_EP;
264
    SYNCDELAY;
265
    FIFORESET = 0x0;
266
    SYNCDELAY;
267
 
268
    EPHS_FPGA_CONF_EPFIFOCFG = 0;                // config fifo
269
    SYNCDELAY;
270
    EPHS_FPGA_CONF_EPFIFOCFG = bmBIT4 | 0;
271
    SYNCDELAY;
272
    EPHS_FPGA_CONF_EPGPIFFLGSEL = 1;
273
    SYNCDELAY;
274
 
275
    GPIFTCB3 = 1;                               // abort after at least 14*65536 transactions
276
    SYNCDELAY;
277
    GPIFTCB2 = 0;
278
    SYNCDELAY;
279
    GPIFTCB1 = 0;
280
    SYNCDELAY;
281
    GPIFTCB0 = 0;
282
    SYNCDELAY;
283
 
284
    EPHS_FPGA_CONF_EPGPIFTRIG = 0xff;           // arm fifos
285
    SYNCDELAY;
286
 
287
    OEA &= ~bmBIT4;                             // disable CCLK output
288
    OEA |= bmBIT0;                              // enable GPIF mode of CPLD
289
    IOA0 = 0;
290
 
291
//    OEA |= bmBIT7;
292
//    IOA7 = 0;
293
,,));;
294
 
295
 
296
/* *********************************************************************
297
   ***** EP0 vendor command 0x35 ***************************************
298
   ********************************************************************* */
299
ADD_EP0_VENDOR_COMMAND((0x35,,          // finish fpga configuration
300
    IOA0 = 1;                           // disable GPIF mode of CPLD
301
    IOA4 = 1;                           // enable CCLK output
302
    OEA |= bmBIT4;
303
 
304
    GPIFABORT = 0xFF;
305
    SYNCDELAY;
306
    IFCONFIG &= 0xf0;
307
    SYNCDELAY;
308
    finish_fpga_configuration();
309
,,));;
310
 
311
#endif  // HS_FPGA_CONF_EP
312
 
313
 
314
 
315
#ifeq[FLASH_BITSTREAM_ENABLED][1]
316
#ifeq[FLASH_ENABLED][1]
317
 
318
/* *********************************************************************
319
   ***** fpga_send_bitstream_from_flash ********************************
320
   ********************************************************************* */
321
void fpga_send_bitstream_from_flash (WORD size) {
322
        size;                   // this avoids stupid warnings
323
__asm
324
        push    _OEB
325
 
326
        mov     r5,dpl          // = size
327
        mov     r6,dph
328
 
329
        // fpga_bytes+=size
330
        mov     dptr,#_fpga_bytes
331
        movx    a,@dptr
332
        mov     r1,a
333
        inc     dptr
334
        movx    a,@dptr
335
        mov     r2,a
336
        inc     dptr
337
        movx    a,@dptr
338
        mov     r3,a
339
        inc     dptr
340
        movx    a,@dptr
341
        mov     r4,a
342
 
343
        mov     dptr,#_fpga_bytes
344
        mov     a,r5
345
        add     a,r1
346
        movx    @dptr,a
347
        mov     a,r6
348
        addc    a,r2
349
        inc     dptr
350
        movx    @dptr,a
351
        mov     a,#0
352
        addc    a,r3
353
        inc     dptr
354
        movx    @dptr,a
355
        mov     a,#0
356
        addc    a,r4
357
        inc     dptr
358
        movx    @dptr,a
359
 
360
// size == 512
361
        cjne    r5,#0,010004$   
362
        cjne    r6,#2,010004$   
363
//      sjmp    010004$ 
364
 
365
        mov     _OEB, #0
366
        anl     _OEA, #(~bmBIT4)        
367
        setb    _IOC6
368
        anl     _OEC, #(~bmBIT6)
369
        orl     _OEA, #(bmBIT3)
370
        clr     _IOA3
371
        setb    _IOA3
372
        anl     _OEA, #(~bmBIT3)
373
 
374
        mov     r2, #3                  // wait > 2 clocks
375
010008$:
376
        mov     r1, #227
377
010009$:
378
        djnz    r1, 010009$
379
        djnz    r2, 010008$
380
 
381
        setb    _IOA4
382
        orl     _OEA, #(bmBIT4) 
383
        orl     _OEC, #(bmBIT6)
384
        clr     _IOC6
385
        pop     _OEB
386
        ret
387
 
388
// size != 512
389
010004$:
390
        mov     _OEB,#1
391
010003$:
392
        cjne    r5,#0x00,010002$        // 4
393
        cjne    r6,#0x00,010002$
394
        pop     _OEB
395
        ret
396
010002$:                                // approx 105 cycles per byte
397
        mov     C, _IOC4  // 2
398
        mov     _IOB0, C  // 2
399
        clr     _IOA4     // 2
400
        setb    _IOA4     // 2
401
        setb    _IOC6     // 2
402
        clr     _IOC6     // 2
403
 
404
        mov     C, _IOC4
405
        mov     _IOB0, C
406
        clr     _IOA4
407
        setb    _IOA4
408
        setb    _IOC6
409
        clr     _IOC6
410
 
411
        mov     C, _IOC4
412
        mov     _IOB0, C
413
        clr     _IOA4
414
        setb    _IOA4
415
        setb    _IOC6
416
        clr     _IOC6
417
 
418
        mov     C, _IOC4
419
        mov     _IOB0, C
420
        clr     _IOA4
421
        setb    _IOA4
422
        setb    _IOC6
423
        clr     _IOC6
424
 
425
        mov     C, _IOC4
426
        mov     _IOB0, C
427
        clr     _IOA4
428
        setb    _IOA4
429
        setb    _IOC6
430
        clr     _IOC6
431
 
432
        mov     C, _IOC4
433
        mov     _IOB0, C
434
        clr     _IOA4
435
        setb    _IOA4
436
        setb    _IOC6
437
        clr     _IOC6
438
 
439
        mov     C, _IOC4
440
        mov     _IOB0, C
441
        clr     _IOA4
442
        setb    _IOA4
443
        setb    _IOC6
444
        clr     _IOC6
445
 
446
        mov     C, _IOC4
447
        mov     _IOB0, C
448
        clr     _IOA4
449
        setb    _IOA4
450
        setb    _IOC6
451
        clr     _IOC6
452
 
453
        dec     r5                      // 1
454
        cjne    r5,#0xff,010003$        // 4
455
        dec     r6
456
        sjmp    010003$
457
__endasm;
458
}
459
 
460
#include[ztex-fpga-flash.h]
461
 
462
#else
463
#warning[Flash interface is not enabled but required for FPGA configuration using a bitstream from Flash meomory]
464
#define[FLASH_BITSTREAM_ENABLED][0]
465
#endif
466
#endif
467
 
468
#endif  /*ZTEX_FPGA_H*/

powered by: WebSVN 2.1.0

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