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-fpga7.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 2.01 and 2.04
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
__xdata BYTE fpga_conf_initialized; // 123 if initialized
34
__xdata BYTE OOEA;
35
 
36
/* *********************************************************************
37
   ***** reset_fpga ****************************************************
38
   ********************************************************************* */
39
static void reset_fpga () {
40
    OEE = (OEE & ~bmBIT6) | bmBIT7;
41
    IOE = IOE & ~bmBIT7;
42
    wait(1);
43
    IOE = IOE | bmBIT7;
44
    fpga_conf_initialized = 0;
45
}
46
 
47
/* *********************************************************************
48
   ***** init_fpga *****************************************************
49
   ********************************************************************* */
50
static void init_fpga () {
51
    IOE = IOE | bmBIT7;
52
    OEE = (OEE & ~bmBIT6) | bmBIT7;
53
    if ( ! (IOE & bmBIT6) ) {
54
        // ensure that FPGA is in a proper configuration mode
55
        IOE = IOE & ~bmBIT7;                    // PROG_B = 0
56
        OEA = (OEA & bmBIT2 ) | bmBIT4 | bmBIT5 | bmBIT6;
57
        IOA = (IOA & bmBIT2 ) | bmBIT5;
58
        wait(1);
59
        IOE = IOE | bmBIT7;                     // PROG_B = 1
60
 
61
    }
62
    fpga_conf_initialized = 0;
63
}
64
 
65
/* *********************************************************************
66
   ***** init_fpga_configuration ***************************************
67
   ********************************************************************* */
68
static void init_fpga_configuration () {
69
    unsigned short k;
70
 
71
    {
72
        PRE_FPGA_RESET
73
    }
74
 
75
    IFCONFIG = bmBIT7;
76
    SYNCDELAY;
77
    PORTACFG = 0;
78
    PORTCCFG = 0;
79
 
80
    OOEA = OEA;
81
    fpga_conf_initialized = 123;
82
 
83
    OEA &= bmBIT2;                      // only unsed PA bit
84
 
85
    OEE = (OEE & ~bmBIT6) | bmBIT7;
86
    IOE = IOE & ~bmBIT7;                // PROG_B = 0
87
 
88
    //     CSI      M0       M1       RDWR
89
    OEA |= bmBIT1 | bmBIT4 | bmBIT5 | bmBIT6;
90
    IOA = ( IOA & bmBIT2 ) | bmBIT1 | bmBIT5;
91
    wait(5);
92
 
93
    IOE = IOE | bmBIT7;                 // PROG_B = 1
94
    IOA1 = 0;                            // CS = 0
95
 
96
    k=0;
97
    while (!IOA7 && k<65535)
98
        k++;
99
 
100
    //     CCLK 
101
    OEA |= bmBIT0;                      // ready for configuration
102
 
103
    fpga_init_b = IOA7 ? 200 : 100;
104
    fpga_bytes = 0;
105
    fpga_checksum = 0;
106
}
107
 
108
/* *********************************************************************
109
   ***** post_fpga_confog **********************************************
110
   ********************************************************************* */
111
static void post_fpga_config () {
112
    POST_FPGA_CONFIG
113
}
114
 
115
/* *********************************************************************
116
   ***** finish_fpga_configuration *************************************
117
   ********************************************************************* */
118
static void finish_fpga_configuration () {
119
    BYTE w;
120
    fpga_init_b += IOA7 ? 22 : 11;
121
 
122
    for ( w=0; w<64; w++ ) {
123
        IOA0 = 1; IOA0 = 0;
124
    }
125
    IOA1 = 1;
126
    IOA0 = 1; IOA0 = 0;
127
    IOA0 = 1; IOA0 = 0;
128
    IOA0 = 1; IOA0 = 0;
129
    IOA0 = 1; IOA0 = 0;
130
 
131
    OEA = OOEA;
132
    if ( IOE & bmBIT6 )  {
133
        post_fpga_config();
134
    }
135
}
136
 
137
 
138
/* *********************************************************************
139
   ***** EP0 vendor request 0x30 ***************************************
140
   ********************************************************************* */
141
ADD_EP0_VENDOR_REQUEST((0x30,,          // get FPGA state
142
    MEM_COPY1(fpga_checksum,EP0BUF+1,7);
143
 
144
    OEE = (OEE & ~bmBIT6) | bmBIT7;
145
    if ( IOE & bmBIT6 )  {
146
        EP0BUF[0] = 0;                    // FPGA configured 
147
    }
148
    else {
149
        EP0BUF[0] = 1;                   // FPGA unconfigured 
150
        reset_fpga();                   // prepare FPGA for configuration
151
     }
152
//    EP0BUF[8] = 0;                    // bit order for bitstream in Flash memory: non-swapped
153
    EP0BUF[8] = 1;                      // bit order for bitstream in Flash memory: swapped
154
 
155
    EP0BCH = 0;
156
    EP0BCL = 9;
157
,,));;
158
 
159
 
160
/* *********************************************************************
161
   ***** EP0 vendor command 0x31 ***************************************
162
   ********************************************************************* */
163
ADD_EP0_VENDOR_COMMAND((0x31,,reset_fpga();,,));;       // reset FPGA
164
 
165
 
166
/* *********************************************************************
167
   ***** EP0 vendor command 0x32 ***************************************
168
   ********************************************************************* */
169
void fpga_send_ep0() {
170
    BYTE oOEC;
171
    oOEC = OEC;
172
    OEC = 255;
173
    fpga_bytes += ep0_payload_transfer;
174
    __asm
175
        mov     dptr,#_EP0BCL
176
        movx    a,@dptr
177
        jz      010000$
178
        mov     r2,a
179
        mov     _AUTOPTRL1,#(_EP0BUF)
180
        mov     _AUTOPTRH1,#(_EP0BUF >> 8)
181
        mov     _AUTOPTRSETUP,#0x07
182
        mov     dptr,#_fpga_checksum
183
        movx    a,@dptr
184
        mov     r1,a
185
        mov     dptr,#_XAUTODAT1
186
010001$:
187
        movx    a,@dptr                 // 2
188
        mov     _IOC,a                  // 2
189
        setb    _IOA0                   // 2
190
        add     a,r1                    // 1
191
        mov     r1,a                    // 1
192
        clr     _IOA0                   // 2
193
        djnz    r2, 010001$             // 4
194
 
195
        mov     dptr,#_fpga_checksum
196
        mov     a,r1
197
        movx    @dptr,a
198
 
199
010000$:
200
        __endasm;
201
    OEC = oOEC;
202
    if ( EP0BCL<64 ) {
203
        finish_fpga_configuration();
204
    }
205
}
206
 
207
ADD_EP0_VENDOR_COMMAND((0x32,,          // send FPGA configuration data
208
    if ( fpga_conf_initialized != 123 )
209
        init_fpga_configuration();
210
,,
211
    fpga_send_ep0();
212
));;
213
 
214
 
215
#ifeq[FLASH_BITSTREAM_ENABLED][1]
216
/* *********************************************************************
217
   ***** fpga_configure_from_flash *************************************
218
   ********************************************************************* */
219
/*
220
    Configure the FPGA using a bitstream from flash.
221
    If force == 0 a already configured FPGA is not re-configured.
222
    Return values:
223
 
224
        1 : FPGA already configured
225
        4 : Configuration error
226
*/
227
BYTE fpga_configure_from_flash( BYTE force ) {
228
//    BYTE c,d;
229
    WORD i;
230
 
231
    if ( ( force == 0 ) && ( IOE & bmBIT6 ) ) {
232
        fpga_flash_result = 1;
233
        return 1;
234
    }
235
 
236
    fpga_flash_result = 0;
237
 
238
    IFCONFIG = bmBIT7;
239
    SYNCDELAY;
240
    PORTACFG = 0;
241
    PORTCCFG = 0;
242
 
243
//    c = OEA;
244
    OEA &= bmBIT2;                      // only unsed PA bit
245
 
246
//    d = OEC;
247
    OEC &= ~bmBIT0;
248
 
249
    OEE = (OEE & ~bmBIT6) | bmBIT7;
250
    IOE = IOE & ~bmBIT7;                // PROG_B = 0
251
 
252
    //     M0       M1
253
    OEA |= bmBIT4 | bmBIT5;
254
    IOA = ( IOA & bmBIT2 ) | bmBIT4;
255
    wait(1);
256
 
257
    IOE = IOE | bmBIT7;                 // PROG_B = 1
258
 
259
// wait up to 4s for CS going high
260
    wait(20);
261
    for (i=0; IOA7 && (!IOA1) && i<4000; i++ ) {
262
        wait(1);
263
    }
264
 
265
    wait(1);
266
 
267
    if ( IOE & bmBIT6 )  {
268
//      IOA = ( IOA & bmBIT2 ) | bmBIT3;
269
        post_fpga_config();
270
//      OEC = d;
271
//      OEA = c;
272
    }
273
    else {
274
        init_fpga();
275
        fpga_flash_result = 4;
276
    }
277
 
278
    return fpga_flash_result;
279
}
280
 
281
#include[ztex-fpga-flash2.h]
282
 
283
#endif
284
 
285
#endif  /*ZTEX_FPGA_H*/

powered by: WebSVN 2.1.0

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