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

powered by: WebSVN 2.1.0

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