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-fpga3.h] - Blame information for rev 4

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

Line No. Rev Author Line
1 4 ZTEX
/*!
2
   ZTEX Firmware Kit for EZ-USB Microcontrollers
3
   Copyright (C) 2009-2010 ZTEX e.K.
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.10
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
 
43
    OEA = bmBIT1 | bmBIT3 | bmBIT4 | bmBIT5 | bmBIT6 | bmBIT7;
44
    IOA = bmBIT7 | mode;
45
    wait(10);
46
 
47
    OEC &= ~bmBIT3;
48
 
49
    IOA = bmBIT1 | mode;                                // ready for configuration
50
    k=0;
51
    while (!IOA0 && k<65535)
52
        k++;
53
 
54
    fpga_init_b = IOA0 ? 200 : 100;
55
    fpga_bytes = 0;
56
    fpga_checksum = 0;
57
}
58
 
59
static void reset_fpga () {
60
    reset_fpga_int(bmBIT5);
61
}
62
 
63
static void reset_fpga_flash () {
64
    reset_fpga_int(bmBIT5 | bmBIT6);
65
}
66
 
67
/* *********************************************************************
68
   ***** init_fpga_configuration ***************************************
69
   ********************************************************************* */
70
static void init_fpga_configuration () {
71
    {
72
        PRE_FPGA_RESET
73
    }
74
    reset_fpga();                       // reset FPGA
75
}
76
 
77
/* *********************************************************************
78
   ***** post_fpga_confog **********************************************
79
   ********************************************************************* */
80
static void post_fpga_config () {
81
    POST_FPGA_CONFIG
82
}
83
 
84
/* *********************************************************************
85
   ***** finish_fpga_configuration *************************************
86
   ********************************************************************* */
87
static void finish_fpga_configuration () {
88
    WORD w;
89
    fpga_init_b += IOA0 ? 20 : 10;
90
 
91
    for ( w=0; w<65535; w++ ) {
92
        IOA3 = 1; IOA3 = 0;
93
    }
94
 
95
    IOA3 = 1; IOA3 = 0;
96
    IOA3 = 1; IOA3 = 0;
97
    IOA3 = 1; IOA3 = 0;
98
    IOA3 = 1; IOA3 = 0;
99
    IOA7 = 1;
100
    IOA3 = 1; IOA3 = 0;
101
    IOA3 = 1; IOA3 = 0;
102
    IOA3 = 1; IOA3 = 0;
103
    IOA3 = 1; IOA3 = 0;
104
 
105
    OEA = 0;
106
    fpga_init_b += IOA0 ? 2 : 1;
107
    if ( IOA1 )  {
108
        IOA1 = 1;
109
        post_fpga_config();
110
    }
111
 
112
    IOA1 = 1;
113
    OEA |= bmBIT1;
114
}
115
 
116
 
117
/* *********************************************************************
118
   ***** EP0 vendor request 0x30 ***************************************
119
   ********************************************************************* */
120
ADD_EP0_VENDOR_REQUEST((0x30,,          // get FPGA state
121
    MEM_COPY1(fpga_checksum,EP0BUF+1,7);
122
    OEA &= ~bmBIT1;
123
    if ( IOA1 )  {
124
        EP0BUF[0] = 0;                    // FPGA configured 
125
        IOA1 = 1;
126
        OEA |= bmBIT1;
127
    }
128
    else {
129
        EP0BUF[0] = 1;                   // FPGA unconfigured 
130
        reset_fpga();                   // prepare FPGA for configuration
131
    }
132
    EP0BUF[8] = 1;                      // bit order for bitstream in Flash memory: swapped
133
 
134
    EP0BCH = 0;
135
    EP0BCL = 9;
136
,,));;
137
 
138
 
139
/* *********************************************************************
140
   ***** EP0 vendor command 0x31 ***************************************
141
   ********************************************************************* */
142
ADD_EP0_VENDOR_COMMAND((0x31,,init_fpga_configuration();,,));;  // reset FPGA
143
 
144
 
145
/* *********************************************************************
146
   ***** EP0 vendor command 0x32 ***************************************
147
   ********************************************************************* */
148
void fpga_send_ep0() {
149
    BYTE oOED;
150
    oOED = OED;
151
    OED = 255;
152
    fpga_bytes += ep0_payload_transfer;
153
    _asm
154
        mov     dptr,#_EP0BCL
155
        movx    a,@dptr
156
        jz      010000$
157
        mov     r2,a
158
        mov     _AUTOPTRL1,#(_EP0BUF)
159
        mov     _AUTOPTRH1,#(_EP0BUF >> 8)
160
        mov     _AUTOPTRSETUP,#0x07
161
        mov     dptr,#_fpga_checksum
162
        movx    a,@dptr
163
        mov     r1,a
164
        mov     dptr,#_XAUTODAT1
165
010001$:
166
        movx    a,@dptr
167
        mov     _IOD,a
168
        setb    _IOA3
169
        add     a,r1
170
        mov     r1,a
171
        clr     _IOA3
172
        djnz    r2, 010001$
173
 
174
        mov     dptr,#_fpga_checksum
175
        mov     a,r1
176
        movx    @dptr,a
177
 
178
010000$:
179
        _endasm;
180
    OED = oOED;
181
    if ( EP0BCL<64 ) {
182
        finish_fpga_configuration();
183
    }
184
}
185
 
186
ADD_EP0_VENDOR_COMMAND((0x32,,          // send FPGA configuration data
187
,,
188
    fpga_send_ep0();
189
));;
190
 
191
 
192
#ifeq[FLASH_BITSTREAM_ENABLED][1]
193
#ifeq[FLASH_ENABLED][1]
194
 
195
/* *********************************************************************
196
   ***** fpga_send_bitstream_from_flash ********************************
197
   ********************************************************************* */
198
void fpga_send_bitstream_from_flash (WORD size) {
199
        size;                   // this avoids stupid warnings
200
_asm
201
        push    _OED
202
        mov     _OED,#0
203
 
204
        mov     r5,dpl          // = size
205
        mov     r6,dph
206
 
207
        // fpga_bytes+=size
208
        mov     dptr,#_fpga_bytes
209
        movx    a,@dptr
210
        mov     r1,a
211
        inc     dptr
212
        movx    a,@dptr
213
        mov     r2,a
214
        inc     dptr
215
        movx    a,@dptr
216
        mov     r3,a
217
        inc     dptr
218
        movx    a,@dptr
219
        mov     r4,a
220
 
221
        mov     dptr,#_fpga_bytes
222
        mov     a,r5
223
        add     a,r1
224
        movx    @dptr,a
225
        mov     a,r6
226
        addc    a,r2
227
        inc     dptr
228
        movx    @dptr,a
229
        mov     a,#0
230
        addc    a,r3
231
        inc     dptr
232
        movx    @dptr,a
233
        mov     a,#0
234
        addc    a,r4
235
        inc     dptr
236
        movx    @dptr,a
237
 
238
010003$:
239
        cjne    r5,#0x00,010002$
240
        cjne    r6,#0x00,010002$
241
        pop     _OED
242
        ret
243
010002$:
244
        setb    _IOA3
245
        setb    _IOC6
246
        clr     _IOA3
247
        clr     _IOC6
248
 
249
        setb    _IOA3
250
        setb    _IOC6
251
        clr     _IOA3
252
        clr     _IOC6
253
 
254
        setb    _IOA3
255
        setb    _IOC6
256
        clr     _IOA3
257
        clr     _IOC6
258
 
259
        setb    _IOA3
260
        setb    _IOC6
261
        clr     _IOA3
262
        clr     _IOC6
263
 
264
        setb    _IOA3
265
        setb    _IOC6
266
        clr     _IOA3
267
        clr     _IOC6
268
 
269
        setb    _IOA3
270
        setb    _IOC6
271
        clr     _IOA3
272
        clr     _IOC6
273
 
274
        setb    _IOA3
275
        setb    _IOC6
276
        clr     _IOA3
277
        clr     _IOC6
278
 
279
        setb    _IOA3
280
        setb    _IOC6
281
        clr     _IOA3
282
        clr     _IOC6
283
 
284
        dec     r5
285
        cjne    r5,#0xff,010003$
286
        dec     r6
287
        sjmp    010003$
288
_endasm;
289
}
290
 
291
#include[ztex-fpga-flash.h]
292
 
293
#else
294
#warning[Flash interface is not enabled but required for FPGA configuration using a bitstream from Flash meomory]
295
#define[FLASH_BITSTREAM_ENABLED][0]
296
#endif
297
#endif
298
 
299
#endif  /*ZTEX_FPGA_H*/

powered by: WebSVN 2.1.0

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