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-default.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
   Template for default firmware
28
*/
29
 
30
// define capability 
31
#define[@CAPABILITY_LSI;]
32
 
33
// enable Flash support
34
ENABLE_FLASH;
35
ENABLE_FLASH_BITSTREAM;
36
 
37
// configure endpoint 2, in, quad buffered, 512 bytes, interface 0
38
EP_CONFIG(2,0,BULK,IN,512,4);
39
 
40
// configure endpoint 6, out, double buffered, 512 bytes, interface 0
41
EP_CONFIG(6,0,BULK,OUT,512,4);
42
ENABLE_HS_FPGA_CONF(6);
43
 
44
__xdata BYTE gpio_out = 0;
45
 
46
void reset_fifo () {
47
    OEA = (OEA | OEA_MASK ) & ~OEA_UMASK;
48
    OEC = (OEC | OEC_MASK ) & ~OEC_UMASK;
49
    OEE = (OEE | OEE_MASK ) & ~OEE_UMASK;
50
    D_RESET = 1;
51
    GPIO_DIR = 0;                        // disables FPGA driving GPIO_DAT
52
    gpio_out = 0;
53
 
54
    EP2CS &= ~bmBIT0;                   // clear stall bit
55
    EP6CS &= ~bmBIT0;                   // clear stall bit
56
 
57
    IFCONFIG = bmBIT7 | bmBIT6 | bmBIT5 | 3;  // internal 48MHz clock, drive IFCLK output, slave FIFO interface
58
    SYNCDELAY;
59
 
60
    REVCTL = 0x1;
61
    SYNCDELAY;
62
 
63
    // IN EP 2
64
    FIFORESET = 0x80;                   // NAK-ALL
65
    SYNCDELAY;
66
    FIFORESET = 2;                      // reset EP2, undocumented: no NAK in order to clear EP memories
67
    SYNCDELAY;
68
    FIFORESET = 0x00;                   // stop NAK-ALL
69
    SYNCDELAY;
70
    EP2FIFOCFG = bmBIT0;                // manu-in
71
    SYNCDELAY;
72
    EP2FIFOCFG = bmBIT3 | bmBIT2 | bmBIT0;  // EP2: auto-in (0->1 transition arms FIFO), allow ZLP, WORDWIDE
73
    SYNCDELAY;
74
    EP2AUTOINLENH = 2;                  // 512 bytes 
75
    SYNCDELAY;
76
    EP2AUTOINLENL = 0;
77
    SYNCDELAY;
78
 
79
    // OUT EP 6
80
    FIFORESET = 0x80;                   // NAK-ALL
81
    SYNCDELAY;
82
    FIFORESET = 6;                      // reset EP6, EP memories should be cleared too
83
    SYNCDELAY;
84
    EP6FIFOCFG = bmBIT0;                // manu-out
85
    SYNCDELAY;
86
    EP6FIFOCFG = bmBIT4 | bmBIT0;       // auto-out (0 -> 1 transition arms FIFO), WORDWIDE
87
    SYNCDELAY;
88
    FIFORESET = 0x00;                   // stop NAK-ALL
89
    SYNCDELAY;
90
 
91
    FIFOPINPOLAR = 0;
92
    SYNCDELAY;
93
    PINFLAGSAB = 0xca;                  // FLAGA: EP6: EF; FLAGB: EP2 FF
94
    SYNCDELAY;
95
 
96
    wait(2);
97
    D_RESET = 0;
98
}
99
 
100
/* *********************************************************************
101
   ***** EP0 vendor command 0x60 ***************************************
102
   ********************************************************************* */
103
// VC 0x60
104
// value != 0: reset signal is left active 
105
ADD_EP0_VENDOR_COMMAND((0x60,,
106
    D_RESET = 1;
107
    if ( SETUPDAT[2] == 0 ) {
108
        // reset FIFO's
109
        if ( SETUPDAT[4] ) {
110
        }
111
        wait(2);
112
        D_RESET = 0;
113
    }
114
,,
115
));;
116
 
117
/* *********************************************************************
118
   ***** EP0 vendor request 0x61 ***************************************
119
   ********************************************************************* */
120
// index: mask
121
// value: value
122
ADD_EP0_VENDOR_REQUEST((0x61,,          // GPIO CTL
123
    GPIO_CLK = 0;
124
 
125
    gpio_out = ( gpio_out & ~SETUPDAT[4] ) | (SETUPDAT[2] & SETUPDAT[4] );
126
 
127
    // writing
128
    GPIO_DIR = 0;        // MOSI
129
    GPIO_WE;
130
    __asm
131
        // min clk -> data hold: 8 clock cycles
132
        // load index
133
        mov     dptr, #_gpio_out
134
        movx    a,@dptr
135
        // gpio[0]
136
        rrc     a
137
        mov     _GPIO_DAT,c
138
        setb    _GPIO_CLK
139
        // gpio[1]
140
        nop
141
        rrc     a
142
        mov     _GPIO_DAT,c
143
        clr     _GPIO_CLK
144
        // gpio[2]
145
        nop
146
        rrc     a
147
        mov     _GPIO_DAT,c
148
        setb    _GPIO_CLK
149
        // gpio[3]
150
        nop
151
        rrc     a
152
        mov     _GPIO_DAT,c
153
        clr     _GPIO_CLK
154
    __endasm;
155
 
156
    // reading
157
    GPIO_RE;
158
    GPIO_DIR = 1;       // 0->1 latches input and starts MISO
159
    __asm
160
        // give FPGA some time to generate output
161
        nop     // some time
162
        nop
163
        nop
164
        nop
165
        nop
166
        nop
167
        nop
168
        nop
169
        // max clk -> data delay: 8 cycles
170
        mov     a, 0
171
        // gpio[3]
172
        mov     c, _GPIO_DAT
173
        setb    _GPIO_CLK
174
        rlc     a
175
        nop
176
        // gpio[2]
177
        mov     c, _GPIO_DAT
178
        clr     _GPIO_CLK
179
        rlc     a
180
        nop
181
        // gpio[1]
182
        mov     c, _GPIO_DAT
183
        setb    _GPIO_CLK
184
        rlc     a
185
        nop
186
        // gpio[0]
187
        mov     c, _GPIO_DAT
188
        rlc     a
189
        // write to EP0BUF
190
        mov     dptr, #_EP0BUF
191
        movx    @dptr,a
192
    __endasm;
193
    GPIO_DIR = 0;        // MOSI
194
    EP0BCH = 0;
195
    EP0BCL = 1;
196
,,));;
197
 
198
 
199
/* *********************************************************************
200
   ***** EP0 vendor command 0x62 ***************************************
201
   ********************************************************************* */
202
void lsi_write_byte (BYTE b) {  // b is in dpl
203
    b;                          // makes the compiler happy
204
    __asm
205
        // min clk -> data hold: 8 clock cycles
206
        // LSI_CLK must be 0
207
        mov     a,dpl
208
        // 0
209
        rrc     a
210
        mov     _LSI_MOSI,c
211
        setb    _LSI_CLK
212
        nop
213
        // 1
214
        rrc     a
215
        mov     _LSI_MOSI,c
216
        clr     _LSI_CLK
217
        nop
218
        // 2
219
        rrc     a
220
        mov     _LSI_MOSI,c
221
        setb    _LSI_CLK
222
        nop
223
        // 3
224
        rrc     a
225
        mov     _LSI_MOSI,c
226
        clr     _LSI_CLK
227
        nop
228
        // 4
229
        rrc     a
230
        mov     _LSI_MOSI,c
231
        setb    _LSI_CLK
232
        nop
233
        // 5
234
        rrc     a
235
        mov     _LSI_MOSI,c
236
        clr     _LSI_CLK
237
        nop
238
        // 6
239
        rrc     a
240
        mov     _LSI_MOSI,c
241
        setb    _LSI_CLK
242
        nop
243
        // 7
244
        rrc     a
245
        mov     _LSI_MOSI,c
246
        clr     _LSI_CLK
247
    __endasm;
248
 
249
}
250
 
251
__xdata BYTE lsi_write_cnt;     // modulo 5 byte count
252
 
253
void ep0_lsi_write() {
254
    BYTE b;
255
    for (b=0; b<EP0BCL; b++) {
256
        lsi_write_byte(EP0BUF[b]);
257
        if ( lsi_write_cnt == 4 ) {
258
            LSI_MOSI = 0;
259
            LSI_STOP = 1;
260
            LSI_CLK = 1;
261
            lsi_write_cnt=0;
262
            LSI_STOP = 0;
263
            LSI_CLK = 0;
264
        }
265
        else {
266
            lsi_write_cnt++;
267
        }
268
    }
269
}
270
 
271
ADD_EP0_VENDOR_COMMAND((0x62,,          // send FPGA configuration data
272
    lsi_write_cnt = 0;
273
    LSI_STOP = 0;
274
    LSI_CLK = 0;
275
,,
276
    ep0_lsi_write();
277
));;
278
 
279
 
280
/* *********************************************************************
281
   ***** EP0 vendor request 0x63 ***************************************
282
   ********************************************************************* */
283
BYTE lsi_read_byte () {
284
    __asm
285
        // max clk -> data delay: 8 cycles
286
        // LSI_CLK must be 0
287
        // 0
288
        mov     c, _LSI_MISO
289
        clr     _LSI_CLK
290
        nop
291
        rrc     a
292
        // 1
293
        mov     c, _LSI_MISO
294
        setb    _LSI_CLK
295
        nop
296
        rrc     a
297
        // 2
298
        mov     c, _LSI_MISO
299
        clr     _LSI_CLK
300
        nop
301
        rrc     a
302
        // 3
303
        mov     c, _LSI_MISO
304
        setb    _LSI_CLK
305
        nop
306
        rrc     a
307
        // 4
308
        mov     c, _LSI_MISO
309
        clr     _LSI_CLK
310
        nop
311
        rrc     a
312
        // 5
313
        mov     c, _LSI_MISO
314
        setb    _LSI_CLK
315
        nop
316
        rrc     a
317
        // 6
318
        mov     c, _LSI_MISO
319
        clr     _LSI_CLK
320
        nop
321
        rrc     a
322
        // 7
323
        mov     c, _LSI_MISO
324
        setb    _LSI_CLK
325
        nop
326
        rrc     a
327
        mov     dpl,a           // result stored in dpl
328
        ret
329
__endasm;
330
        return 0;                // never called, makes the compiler happy
331
}
332
 
333
__xdata BYTE lsi_read_adr;      // 
334
__xdata BYTE lsi_read_cnt;      // modulo 4 byte count
335
 
336
void ep0_lsi_read() {
337
    BYTE b, c;
338
    c=ep0_payload_transfer;
339
    for (b=0; b<c; b++) {
340
        if ( lsi_read_cnt == 0 ) {
341
            LSI_STOP = 0;
342
            LSI_CLK = 0;
343
            lsi_write_byte(lsi_read_adr);
344
            LSI_MOSI = 1;
345
            LSI_STOP = 1;
346
            LSI_CLK = 1;
347
            lsi_read_adr++;
348
        }
349
        lsi_read_cnt = (lsi_read_cnt+1) & 3;
350
        EP0BUF[b]=lsi_read_byte();
351
    }
352
    EP0BCH = 0;
353
    EP0BCL = c;
354
}
355
 
356
ADD_EP0_VENDOR_REQUEST((0x63,,          // get info about default firmware
357
    lsi_read_cnt = 0;
358
    lsi_read_adr = SETUPDAT[4];
359
    LSI_CLK = 0;
360
    ep0_lsi_read();
361
,,
362
    ep0_lsi_read();
363
));;
364
 
365
 
366
/* *********************************************************************
367
   ***** EP0 vendor request 0x64 ***************************************
368
   ********************************************************************* */
369
ADD_EP0_VENDOR_REQUEST((0x64,,          // get info about default firmware
370
    EP0BUF[0] = 1;                       // version
371
    EP0BUF[1] = 6;                      // output endpoint
372
    EP0BUF[2] = 2;                      // input endpoint
373
    EP0BUF[3] = 4;                      // sub version
374
    EP0BUF[4] = 0;                       // reserved for future use
375
    EP0BUF[5] = 0;                       // reserved for future use
376
    EP0BUF[6] = 0;                       // reserved for future use
377
    EP0BUF[7] = 0;                       // reserved for future use
378
    EP0BCH = 0;
379
    if ( ep0_payload_transfer > 8 ) {
380
        EP0BCL = 8;
381
    }
382
    else {
383
        EP0BCL = ep0_payload_transfer;
384
    }
385
,,));;
386
 
387
// this is called automatically after FPGA configuration
388
#define[POST_FPGA_CONFIG][POST_FPGA_CONFIG
389
    reset_fifo ();
390
]
391
 
392
// include the main part of the firmware kit, define the descriptors, ...
393
#include[ztex.h]
394
 
395
void main(void)
396
{
397
    init_USB();
398
 
399
    while (1) { }                                       //  twiddle thumbs
400
}

powered by: WebSVN 2.1.0

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