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

Subversion Repositories usb_fpga_2_13

[/] [usb_fpga_2_13/] [trunk/] [include/] [ztex-fpga5.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 1.15y
21
*/
22
 
23
#ifndef[ZTEX_FPGA_H]
24
#define[ZTEX_FPGA_H]
25
 
26
#define[@CAPABILITY_FPGA;]
27
#define[@CAPABILITY_MULTI_FPGA;]
28
__xdata BYTE fpga_checksum;         // checksum
29
__xdata DWORD fpga_bytes;           // transferred bytes
30
__xdata BYTE fpga_init_b;           // init_b state (should be 222 after configuration)
31
 
32
__xdata BYTE select_num;
33
__xdata BYTE prev_select_num;
34
__xdata BYTE select_mask;
35
__xdata BYTE config_mask_h;
36
__xdata BYTE config_mask_l;
37
 
38
 
39
/* *********************************************************************
40
   ***** init_fpga *****************************************************
41
   ********************************************************************* */
42
void init_fpga () {
43
    IOE = 0x1f;
44
    OEE = 0xff;
45
 
46
    prev_select_num = 0;
47
    select_num = 0;
48
    select_mask = 0x10;
49
    config_mask_h = 0x10;
50
    config_mask_l = 0x01;
51
}
52
 
53
 
54
/* *********************************************************************
55
   ***** reset_fpga ****************************************************
56
   ********************************************************************* */
57
static void reset_fpga () {             // reset FPGA
58
    WORD k;
59
    IFCONFIG = bmBIT7;
60
    SYNCDELAY;
61
    PORTACFG = 0;
62
    PORTCCFG = 0;
63
 
64
    IOC2 = 1;                    // out: INIT_B
65
    OEC |= bmBIT2;
66
 
67
    OEA &= ~bmBIT6;              // in: CSO
68
    OEC &= ~bmBIT1;              // in: DOUT
69
 
70
    IOC3 = 0;
71
    OEC |= bmBIT3;               // out: RDWR_B
72
 
73
//  out:    CCLK,    M1,      GPIF,    M0,      CSI
74
    OEA |= bmBIT1 | bmBIT2 | bmBIT3 | bmBIT5 | bmBIT7;
75
    IOA5 = 0;
76
    IOA |= bmBIT1 | bmBIT2 | bmBIT3 |          bmBIT7;
77
 
78
    IOE = config_mask_h | ((~config_mask_l) & 0x0f);
79
    OEE = 0xff;
80
    wait(1);
81
 
82
    IOA7 = 0;
83
    IOA1 = 0;
84
    IOE = config_mask_h | 0x0f;
85
    k=0;
86
    OEC &= ~bmBIT2;              // in: INIT_B
87
    while ( (!IOC2) && (k<65535) ) {
88
        k++;
89
    }
90
 
91
    fpga_init_b = IOC2 ? 200 : 100;
92
    fpga_bytes = 0;
93
    fpga_checksum = 0;
94
}
95
 
96
/* *********************************************************************
97
   ***** init_fpga_configuration ***************************************
98
   ********************************************************************* */
99
static void init_fpga_configuration () {
100
    {
101
        PRE_FPGA_RESET
102
    }
103
    reset_fpga();                       // reset FPGA
104
}
105
 
106
/* *********************************************************************
107
   ***** post_fpga_confog **********************************************
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 b;
118
    fpga_init_b += 22;
119
 
120
    for ( b=0; b<255; b++ ) {
121
        IOA1 = 1; IOA1 = 0;
122
    }
123
    IOA7 = 1;
124
    IOA4 = 1; IOA4 = 0;
125
    IOA4 = 1; IOA4 = 0;
126
    IOA4 = 1; IOA4 = 0;
127
    IOA4 = 1; IOA4 = 0;
128
 
129
    OEA &= ~(bmBIT1 | bmBIT2 | bmBIT3 | bmBIT7);
130
    OEC &= ~bmBIT3;
131
    OEE = 0xf0;
132
    if ( (IOE & config_mask_l) == config_mask_l ) {
133
        post_fpga_config();
134
    }
135
 
136
    IOE = select_mask | 0x0f;
137
    OEE = 0xff;
138
}
139
 
140
 
141
/* *********************************************************************
142
   ***** EP0 vendor request 0x30 ***************************************
143
   ********************************************************************* */
144
ADD_EP0_VENDOR_REQUEST((0x30,,          // get FPGA state
145
    MEM_COPY1(fpga_checksum,EP0BUF+1,6);
146
    OEE = 0xf0;
147
    if ( (IOE & config_mask_l) == config_mask_l )  {
148
        EP0BUF[0] = 0;                    // FPGA configured 
149
        IOE = select_mask | 0x0f;
150
        OEE = 0xff;
151
    }
152
    else {
153
        EP0BUF[0] = 1;                   // FPGA unconfigured 
154
        reset_fpga();                   // prepare FPGA for configuration
155
    }
156
 
157
    EP0BUF[7] = 0;                       // not used
158
    EP0BUF[8] = 0;                       // not used
159
 
160
    EP0BCH = 0;
161
    EP0BCL = 9;
162
,,));;
163
 
164
 
165
/* *********************************************************************
166
   ***** EP0 vendor command 0x31 ***************************************
167
   ********************************************************************* */
168
ADD_EP0_VENDOR_COMMAND((0x31,,init_fpga_configuration();,,));;  // reset FPGA
169
 
170
 
171
/* *********************************************************************
172
   ***** EP0 vendor command 0x32 ***************************************
173
   ********************************************************************* */
174
void fpga_send_ep0() {                  // send FPGA configuration data
175
    BYTE oOEB;
176
    oOEB = OEB;
177
    OEB = 255;
178
    fpga_bytes += ep0_payload_transfer;
179
    __asm
180
        mov     dptr,#_EP0BCL
181
        movx    a,@dptr
182
        jz      010000$
183
        mov     r2,a
184
        mov     _AUTOPTRL1,#(_EP0BUF)
185
        mov     _AUTOPTRH1,#(_EP0BUF >> 8)
186
        mov     _AUTOPTRSETUP,#0x07
187
        mov     dptr,#_fpga_checksum
188
        movx    a,@dptr
189
        mov     r1,a
190
        mov     dptr,#_XAUTODAT1
191
010001$:
192
        movx    a,@dptr                 // 2
193
        mov     _IOB,a                  // 2
194
        setb    _IOA1                   // 2
195
        add     a,r1                    // 1
196
        mov     r1,a                    // 1
197
        clr     _IOA1                   // 2
198
        djnz    r2, 010001$             // 4
199
 
200
        mov     dptr,#_fpga_checksum
201
        mov     a,r1
202
        movx    @dptr,a
203
 
204
010000$:
205
        __endasm;
206
    OEB = oOEB;
207
    if ( EP0BCL<64 ) {
208
        finish_fpga_configuration();
209
    }
210
}
211
 
212
ADD_EP0_VENDOR_COMMAND((0x32,,          // send FPGA configuration data
213
,,
214
    fpga_send_ep0();
215
));;
216
 
217
 
218
#ifdef[HS_FPGA_CONF_EP]
219
 
220
#ifeq[HS_FPGA_CONF_EP][2]
221
#elifeq[HS_FPGA_CONF_EP][4]
222
#elifeq[HS_FPGA_CONF_EP][6]
223
#elifneq[HS_FPGA_CONF_EP][8]
224
#error[`HS_FPGA_CONF_EP' is not defined correctly. Valid values are: `2', `4', `6', `8'.]
225
#endif
226
 
227
#define[@CAPABILITY_HS_FPGA;]
228
 
229
/* *********************************************************************
230
   ***** EP0 vendor request 0x33 ***************************************
231
   ********************************************************************* */
232
ADD_EP0_VENDOR_REQUEST((0x33,,          // get high speed fpga configuration endpoint and interface 
233
    EP0BUF[0] = HS_FPGA_CONF_EP; // endpoint
234
    EP0BUF[1] = EPHS_FPGA_CONF_EP_INTERFACE; // interface
235
    EP0BCH = 0;
236
    EP0BCL = 2;
237
,,));;
238
 
239
 
240
/* *********************************************************************
241
   ***** EP0 vendor command 0x34 ***************************************
242
   ********************************************************************* */
243
// FIFO write wave form
244
const char __xdata GPIF_WAVE_DATA_HSFPGA_24MHZ[32] =
245
{
246
/* LenBr */ 0x01,     0x88,     0x01,     0x01,     0x01,     0x01,     0x01,     0x07,
247
/* Opcode*/ 0x02,     0x07,     0x02,     0x02,     0x02,     0x02,     0x02,     0x00,
248
/* Output*/ 0x20,     0x00,     0x00,     0x00,     0x00,     0x00,     0x00,     0x20,
249
/* LFun  */ 0x00,     0x36,     0x00,     0x00,     0x00,     0x00,     0x00,     0x3F,
250
};
251
 
252
const char __xdata GPIF_WAVE_DATA_HSFPGA_12MHZ[32] =
253
{
254
/* LenBr */ 0x02,     0x01,     0x90,     0x01,     0x01,     0x01,     0x01,     0x07,
255
/* Opcode*/ 0x02,     0x02,     0x07,     0x02,     0x02,     0x02,     0x02,     0x00,
256
/* Output*/ 0x20,     0x00,     0x00,     0x00,     0x00,     0x00,     0x00,     0x20,
257
/* LFun  */ 0x00,     0x00,     0x36,     0x00,     0x00,     0x00,     0x00,     0x3F,
258
};
259
 
260
 
261
void init_cpld_fpga_configuration() {
262
    IFCONFIG = bmBIT7 | bmBIT6 | 2;     // Internal source, 48MHz, GPIF
263
//    IFCONFIG = bmBIT7 | 2;    // Internal source, 30MHz, GPIF
264
 
265
    GPIFREADYCFG = 0x0;
266
    GPIFCTLCFG = 0;
267
    GPIFIDLECS = 0;
268
    GPIFIDLECTL = 0x20;
269
    GPIFWFSELECT = 0x4E;
270
    GPIFREADYSTAT = 0;
271
 
272
    MEM_COPY1(GPIF_WAVE_DATA_HSFPGA_24MHZ,GPIF_WAVE3_DATA,32);
273
 
274
    FLOWSTATE = 0;
275
    FLOWLOGIC = 0x10;
276
    FLOWEQ0CTL = 0;
277
    FLOWEQ1CTL = 0;
278
    FLOWHOLDOFF = 0;
279
    FLOWSTB = 0;
280
    FLOWSTBEDGE = 0;
281
    FLOWSTBHPERIOD = 0;
282
 
283
    REVCTL = 0x1;                               // reset fifo
284
    SYNCDELAY;
285
    FIFORESET = 0x80;
286
    SYNCDELAY;
287
    FIFORESET = HS_FPGA_CONF_EP;
288
    SYNCDELAY;
289
    FIFORESET = 0x0;
290
    SYNCDELAY;
291
 
292
    EPHS_FPGA_CONF_EPFIFOCFG = 0;                // config fifo
293
    SYNCDELAY;
294
    EPHS_FPGA_CONF_EPFIFOCFG = bmBIT4;
295
    SYNCDELAY;
296
    EPHS_FPGA_CONF_EPGPIFFLGSEL = 1;
297
    SYNCDELAY;
298
 
299
    GPIFTCB3 = 1;                               // abort after at least 14*65536 transactions
300
    SYNCDELAY;
301
    GPIFTCB2 = 0;
302
    SYNCDELAY;
303
    GPIFTCB1 = 0;
304
    SYNCDELAY;
305
    GPIFTCB0 = 0;
306
    SYNCDELAY;
307
 
308
    EPHS_FPGA_CONF_EPGPIFTRIG = 0xff;           // arm fifos
309
    SYNCDELAY;
310
 
311
    IOA3 = 0;
312
}
313
 
314
 
315
ADD_EP0_VENDOR_COMMAND((0x34,,                  // init fpga configuration
316
    init_fpga_configuration();
317
 
318
    EPHS_FPGA_CONF_EPCS &= ~bmBIT0;             // clear stall bit
319
 
320
    GPIFABORT = 0xFF;                           // abort pendig 
321
 
322
    init_cpld_fpga_configuration();
323
,,));;
324
 
325
 
326
/* *********************************************************************
327
   ***** EP0 vendor command 0x35 ***************************************
328
   ********************************************************************* */
329
ADD_EP0_VENDOR_COMMAND((0x35,,          // finish fpga configuration
330
    IOA3 = 1;                           // disable GPIF mode of CPLD
331
 
332
    GPIFABORT = 0xFF;
333
    SYNCDELAY;
334
    IFCONFIG &= 0xf0;
335
    SYNCDELAY;
336
 
337
    finish_fpga_configuration();
338
,,));;
339
 
340
#endif  // HS_FPGA_CONF_EP
341
 
342
 
343
/* *********************************************************************
344
   ***** select_fpga ***************************************************
345
   ********************************************************************* */
346
void select_fpga ( BYTE fn )
347
{
348
    prev_select_num = select_num;
349
    select_num = fn & 3;
350
    select_mask = 0x10 << fn;
351
    config_mask_h = select_mask;
352
 
353
    IOE = 0x0f;
354
    {
355
        PRE_FPGA_SELECT
356
    }
357
    IOE = select_mask | 0x0f;
358
}
359
 
360
 
361
/* *********************************************************************
362
   ***** EP0 vendor request 0x50 ***************************************
363
   ********************************************************************* */
364
ADD_EP0_VENDOR_REQUEST((0x50,,          // Return multi-FPGA information
365
    EP0BUF[0] = 3;                       // 1 FPGA's
366
    EP0BUF[1] = select_num;             // select methods: any combination
367
    EP0BUF[2] = 0;                       // no parallel configuration support
368
    EP0BCH = 0;
369
    EP0BCL = 3;
370
,,));;
371
 
372
/* *********************************************************************
373
   ***** EP0 vendor command 0x51 ***************************************
374
   ********************************************************************* */
375
ADD_EP0_VENDOR_COMMAND((0x51,,          // select command
376
    if ( SETUPDAT[4] == 1 ) {
377
        config_mask_h = 0xf0;
378
    }
379
    else {
380
        select_fpga( SETUPDAT[2] );
381
    }
382
    config_mask_l = config_mask_h >> 4;
383
,,
384
    NOP;
385
));;
386
 
387
#endif  /*ZTEX_FPGA_H*/

powered by: WebSVN 2.1.0

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