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/] [fx3/] [ztex-ufm-2_18.c] - 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 FX3 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
   Board specific functions for ZTEX-USB FPGA Module 2.18
27
*/
28
 
29
#ifndef _ZTEX_CONF_UFM_2_18_C1_
30
#define _ZTEX_CONF_UFM_2_18_C1_
31
 
32
#include "cyu3pib.h"
33
 
34
// product ID's for ZTEX USB-FPGA Module 2.18 are 10.42.*.*
35
#undef ZTEX_PRODUCT_ID_0
36
#define ZTEX_PRODUCT_ID_0       (10)
37
#undef ZTEX_PRODUCT_ID_1
38
#define ZTEX_PRODUCT_ID_1       (42)
39
 
40
/*
41
   This macro defines the Product string. Limited to 31 characters.
42
*/
43
#undef ZTEX_PRODUCT_STRING
44
#define ZTEX_PRODUCT_STRING "ZTEX USB-FPGA Module 2.18"
45
 
46
#define ENABLE_SPORT0
47
 
48
// GPIO's
49
#define ZTEX_GPIO_MODE0 50
50
#define ZTEX_GPIO_MODE1 45
51
 
52
#define ZTEX_GPIO_LED   52
53
 
54
#define ZTEX_GPIO_FPGA_RESET 51
55
#define ZTEX_GPIO_FPGA_INIT_B 37
56
#define ZTEX_GPIO_FPGA_RDWR_B 38
57
#define ZTEX_GPIO_FPGA_CSI_B 39
58
#define ZTEX_GPIO_FPGA_DONE 40
59
 
60
#define ZTEX_GPIO_OTG_EN 57 
61
 
62
#undef ZTEX_GPIO_SIMPLE_BITMAP0
63
#undef ZTEX_GPIO_SIMPLE_BITMAP1
64
#define ZTEX_GPIO_SIMPLE_BITMAP0 0
65
#define ZTEX_GPIO_SIMPLE_BITMAP1 ( 1 << (ZTEX_GPIO_MODE0-32) | 1 << (ZTEX_GPIO_MODE1-32) | 1 << (ZTEX_GPIO_LED-32) \
66
        | 1 << (ZTEX_GPIO_FPGA_RESET-32) | 1 << (ZTEX_GPIO_FPGA_DONE-32) | 1 << (ZTEX_GPIO_FPGA_INIT_B-32) | 1 << (ZTEX_GPIO_FPGA_RDWR_B-32)  | 1 << (ZTEX_GPIO_FPGA_CSI_B-32)  \
67
        | 1 << (ZTEX_GPIO_OTG_EN-32) \
68
    )
69
 
70
#define ZTEX_FPGA_CONFIGURED ( ztex_gpio_get(ZTEX_GPIO_FPGA_DONE) )
71
 
72
#define _ZTEX_BOARD_
73
#define _ZTEX_FPGA_
74
 
75
void ztex_disable_flash();
76
 
77
#endif // _ZTEX_CONF_UFM_2_18_C1_
78
 
79
#ifdef _ZTEX_INCLUDE_2_
80
#ifndef _ZTEX_CONF_UFM_2_18_C2_
81
#define _ZTEX_CONF_UFM_2_18_C2_
82
 
83
#include "ztex-fpgaconf1.c"
84
 
85
/* USB system is restarted after FPGA reset and after successful FPGA configuration.
86
   (de)initialization code should be written to ztex_usb_stop() and ztex_usb_start().
87
   See ztex-default.c (Template for default firmware) for recommended usage.
88
*/
89
 
90
uint8_t ztex_fpga_cs = 0;                                // check sum
91
uint32_t ztex_fpga_bytes = 0;                            // transferred bytes
92
uint8_t ztex_fpga_init_b = 0;                            // init b 
93
 
94
uint8_t ztex_fpga_config_started = 0;
95
 
96
void ztex_usb_start_main();
97
void ztex_usb_stop_main();
98
 
99
/* *********************************************************************
100
   ***** ztex_cpld_set *************************************************
101
   ********************************************************************* */
102
void ztex_cpld_set( CyBool_t enable_flash, CyBool_t reset_fpga) {
103
    ztex_gpio_set(ZTEX_GPIO_MODE0, !enable_flash);
104
    ztex_gpio_set(ZTEX_GPIO_FPGA_RESET, !reset_fpga);
105
    ztex_gpio_set(ZTEX_GPIO_MODE1, CyFalse);
106
    if ( reset_fpga ) return;
107
    ztex_gpio_set(ZTEX_GPIO_MODE1, CyTrue);
108
}
109
 
110
/* *********************************************************************
111
   ***** ztex_disable_flash ********************************************
112
   ********************************************************************* */
113
// disables flash after soft reset
114
void ztex_disable_flash() {
115
    ztex_cpld_set(CyFalse, CyFalse);
116
}
117
 
118
/* *********************************************************************
119
   ***** ztex_enable_flash *********************************************
120
   ********************************************************************* */
121
// enables flash after soft reset
122
void ztex_enable_flash() {
123
    ztex_cpld_set(CyTrue, CyFalse);
124
}
125
 
126
/* *********************************************************************
127
   ***** ztex_spi_FX3_flash ********************************************
128
   ********************************************************************* */
129
// SPI Master: FX3, slave: Flash
130
void ztex_spi_FX3_flash() {
131
    ztex_cpld_set(CyTrue, CyFalse);
132
}
133
 
134
/* *********************************************************************
135
   ***** ztex_spi_FX3_FPGA *********************************************
136
   ********************************************************************* */
137
// SPI Master: FX3, slave: FPGA
138
void ztex_spi_FX3_FPGA() {
139
    ztex_gpio_set(ZTEX_GPIO_MODE0, CyFalse);
140
    ztex_gpio_set(ZTEX_GPIO_MODE0, CyTrue);
141
}
142
 
143
/* *********************************************************************
144
   ***** ztex_spi_FPGA_Flash *******************************************
145
   ********************************************************************* */
146
// SPI Master: FPGA, slave: Flash
147
void ztex_spi_FPGA_Flash() {
148
    ztex_gpio_set(ZTEX_GPIO_MODE0, CyTrue);
149
    ztex_gpio_set(ZTEX_GPIO_MODE0, CyFalse);
150
}
151
 
152
/* *********************************************************************
153
   ***** ztex_fpga_configured ******************************************
154
   ********************************************************************* */
155
CyBool_t ztex_fpga_configured() {
156
    return ztex_gpio_get(ZTEX_GPIO_FPGA_DONE);
157
}
158
 
159
/* *********************************************************************
160
   ***** ztex_fpga_reset ***********************************************
161
   ********************************************************************* */
162
void ztex_fpga_reset() {
163
    if ( ZTEX_FPGA_CONFIGURED || ztex_fpga_config_started ) { // restart USB and reset pib clock
164
        ZTEX_LOG("Info: Preparing USB for FPGA configuration");
165
        ztex_usb_stop_main();
166
        ztex_pib_clock2 = &ztex_fpgaconf1_pib_clock;
167
        ztex_cpld_set(CyTrue, CyTrue);
168
        ztex_usb_start_main();
169
        ztex_fpga_config_started = 0;
170
    }
171
 
172
    ztex_cpld_set(CyTrue, CyTrue);
173
 
174
    ztex_gpio_set_output(ZTEX_GPIO_FPGA_RDWR_B, CyFalse);
175
    ztex_gpio_set_output(ZTEX_GPIO_FPGA_CSI_B, CyFalse);
176
 
177
    CyU3PThreadSleep (20);
178
 
179
    ztex_cpld_set(CyTrue, CyFalse);
180
}
181
 
182
/* *********************************************************************
183
   ***** ztex_fpga_config_start ****************************************
184
   ********************************************************************* */
185
// socket should be 0 for configuration from CPU
186
void ztex_fpga_config_start(CyU3PDmaSocketId_t socket) {
187
    uint8_t mode = socket > 0 ? 1 : 2;
188
    if ( ztex_fpga_config_started == mode ) return;  // already started in correct mode
189
 
190
    ztex_fpga_reset();
191
 
192
    ztex_fpga_config_started = mode;
193
 
194
    ztex_fpgaconf1_start(socket);       // start gpif
195
 
196
    if ( socket > 0) {                   // start auto transfers 
197
        CyU3PDmaChannel* dma_p = CyU3PDmaChannelGetHandle(socket);
198
        if ( dma_p != NULL ) ZTEX_REC(CyU3PDmaChannelSetXfer (dma_p, 0));
199
    }
200
 
201
    uint8_t i = 0;
202
    while ( (!ztex_gpio_get(ZTEX_GPIO_FPGA_INIT_B)) && i<255 ) {
203
        CyU3PThreadSleep (1);
204
        i++;
205
    }
206
 
207
    ztex_fpga_init_b = ztex_gpio_get(ZTEX_GPIO_FPGA_INIT_B) ? 200 : 100;
208
    ztex_fpga_cs = 0;
209
    ztex_fpga_bytes = 0;
210
}
211
 
212
/* *********************************************************************
213
   ***** ztex_fpga_config_done *****************************************
214
   ********************************************************************* */
215
void ztex_fpga_config_done(CyBool_t fromFlash) {
216
    ztex_fpga_init_b += ztex_gpio_get(ZTEX_GPIO_FPGA_INIT_B) ? 22 : 11;
217
 
218
    if ( ztex_fpga_config_started == 2)  ztex_fpgaconf1_send(ztex_ep0buf,16); // some extra clock's
219
 
220
    ztex_fpgaconf1_stop();      // stop gpif
221
 
222
    ztex_fpga_config_started = 0;
223
 
224
    if ( fromFlash ) ZTEX_REC( CyU3PPibDeInit() );
225
 
226
    if ( ZTEX_FPGA_CONFIGURED ) {
227
        ztex_gpio_set_input(ZTEX_GPIO_FPGA_RDWR_B);
228
        ztex_gpio_set_input(ZTEX_GPIO_FPGA_CSI_B);
229
 
230
        ZTEX_LOG("Info: Preparing USB for application");
231
        if ( ! fromFlash ) ztex_usb_stop_main();        // restart USB and reset PIB clock
232
        ztex_pib_clock2 = &ztex_pib_clock;
233
        if ( ! fromFlash ) ztex_usb_start_main();
234
    }
235
}
236
 
237
/* *********************************************************************
238
   ***** vr_fpga_info **************************************************
239
   ********************************************************************* */
240
// VR 0x30
241
uint8_t vr_fpga_info(uint16_t value, uint16_t index, uint16_t length ) {
242
    ztex_ep0buf[0] = ZTEX_FPGA_CONFIGURED ? 0 : 1;
243
    ztex_ep0buf[1] = ztex_fpga_cs;
244
    ztex_ep0buf[2] = ztex_fpga_bytes;
245
    ztex_ep0buf[3] = ztex_fpga_bytes >> 8;
246
    ztex_ep0buf[4] = ztex_fpga_bytes >> 16;
247
    ztex_ep0buf[5] = ztex_fpga_bytes >> 24;
248
    ztex_ep0buf[6] = ztex_fpga_init_b;
249
    ztex_ep0buf[7] = 0;         // flash configuration result
250
    ztex_ep0buf[8] = 0;          // bit order = not swapped
251
    ZTEX_REC_RET( CyU3PUsbSendEP0Data( 9, ztex_ep0buf ) );
252
    return 0;
253
}
254
 
255
/* *********************************************************************
256
   ***** vc_fpga_reset *************************************************
257
   ********************************************************************* */
258
// VC 0x31
259
uint8_t vc_fpga_reset(uint16_t value, uint16_t index, uint16_t length ) {
260
    ztex_fpga_reset();
261
    CyU3PUsbAckSetup();
262
    return 0;
263
}
264
 
265
/* *********************************************************************
266
   ***** vc_fpga_send **************************************************
267
   ********************************************************************* */
268
// VC 0x32
269
uint8_t vc_fpga_send(uint16_t value, uint16_t index, uint16_t length ) {
270
    ztex_fpga_config_start(0);
271
 
272
    if ( length > 0 ) {
273
        ZTEX_REC_RET ( CyU3PUsbGetEP0Data (length, ztex_ep0buf, NULL) );
274
        for (uint16_t i = 0;  i<length; i++)
275
            ztex_fpga_cs += ztex_ep0buf[i];
276
        ztex_fpga_bytes += length;
277
        ztex_fpgaconf1_send(ztex_ep0buf,length);
278
    }
279
    else {
280
        CyU3PUsbAckSetup();
281
    }
282
 
283
    if ( length == 0 || ((length & 63) != 0)  ) ztex_fpga_config_done(CyFalse);
284
    return 0;
285
}
286
 
287
#ifdef ZTEX_FPGA_CONF_FAST_EP
288
// ZTEX_FPGA_CONF_FAST_IFACE and ZTEX_FPGA_CONF_FAST_SOCKET must be defined too
289
/* *********************************************************************
290
   ***** vc_fpga_fast_info *********************************************
291
   ********************************************************************* */
292
// VR 0x33
293
uint8_t vr_fpga_fast_info(uint16_t value, uint16_t index, uint16_t length ) {
294
    ztex_ep0buf[0] = ZTEX_FPGA_CONF_FAST_EP;
295
    ztex_ep0buf[1] = ZTEX_FPGA_CONF_FAST_IFACE;
296
    ZTEX_REC_RET( CyU3PUsbSendEP0Data( 2, ztex_ep0buf ) );
297
    return 0;
298
}
299
 
300
/* *********************************************************************
301
   ***** vc_fpga_fast_start ********************************************
302
   ********************************************************************* */
303
// VR 0x34
304
uint8_t vc_fpga_fast_start(uint16_t value, uint16_t index, uint16_t length ) {
305
    ztex_fpga_config_start(ZTEX_FPGA_CONF_FAST_SOCKET);
306
    CyU3PUsbAckSetup();
307
    return 0;
308
}
309
 
310
/* *********************************************************************
311
   ***** vc_fpga_fast_finish *******************************************
312
   ********************************************************************* */
313
// VR 0x35
314
uint8_t vc_fpga_fast_finish(uint16_t value, uint16_t index, uint16_t length ) {
315
    ztex_fpga_config_done(CyFalse);
316
    CyU3PUsbAckSetup();
317
    return 0;
318
}
319
#endif
320
 
321
 
322
/* *********************************************************************
323
   ***** ztex_board_init ***********************************************
324
   ********************************************************************* */
325
void ztex_board_init() {
326
 
327
    ztex_log ( "Info: Initializing USB-FPGA Module 2.18" );
328
 
329
    ztex_disable_flash_boot = ztex_disable_flash;
330
    ztex_fpga_config_started = 0;
331
 
332
    ztex_gpio_set_output(ZTEX_GPIO_LED, CyFalse);
333
    ztex_gpio_set_output(ZTEX_GPIO_MODE0, CyTrue);
334
    ztex_gpio_set_output(ZTEX_GPIO_MODE1, CyTrue);
335
 
336
    ztex_gpio_set_input(ZTEX_GPIO_FPGA_DONE);
337
    ztex_gpio_set_output(ZTEX_GPIO_FPGA_RESET, CyTrue);
338
    ztex_gpio_set_input(ZTEX_GPIO_FPGA_INIT_B);         CyU3PGpioSetIoMode(ZTEX_GPIO_FPGA_INIT_B, CY_U3P_GPIO_IO_MODE_WPU);
339
 
340
    ztex_gpio_set_output(ZTEX_GPIO_OTG_EN, CyFalse);
341
 
342
    ztex_enable_flash();
343
 
344
    ztex_register_vendor_req(0x30, vr_fpga_info);
345
    ztex_register_vendor_cmd(0x31, vc_fpga_reset);
346
    ztex_register_vendor_cmd(0x32, vc_fpga_send);
347
#ifdef ZTEX_FPGA_CONF_FAST_EP
348
    ztex_register_vendor_req(0x33, vr_fpga_fast_info);
349
    ztex_register_vendor_cmd(0x34, vc_fpga_fast_start);
350
    ztex_register_vendor_cmd(0x35, vc_fpga_fast_finish);
351
#endif    
352
 
353
    // select pib clock settings
354
    ztex_pib_clock2 = ZTEX_FPGA_CONFIGURED ? &ztex_pib_clock : &ztex_fpgaconf1_pib_clock;
355
}
356
 
357
/* *********************************************************************
358
   ***** ztex_flash_config *********************************************
359
   ********************************************************************* */
360
#ifndef DISABLE_FLASH_CONFIG
361
#define _ZTEX_FLASH_CONFIG_FUNC_ { ztex_flash_config(); }
362
void ztex_flash_config() {
363
    uint8_t buf[6];
364
    uint16_t bs_start, bs_size;
365
    if ( ZTEX_FPGA_CONFIGURED || !ztex_config_data_valid || !ztex_flash.enabled ) return;
366
    if ( ztex_mac_eeprom_read ( 26, buf, 6 ) ) return;
367
 
368
    bs_start = ((buf[4] + 15) & 0xf0) | (buf[5] << 8);          // in 4k sectors
369
    bs_size = buf[0] | (buf[1] << 8);                            // in 4k sectors
370
 
371
    if (bs_size == 0) return;
372
 
373
    ZTEX_REC( CyU3PPibInit(CyTrue, &ztex_fpgaconf1_pib_clock) ); // init PIB
374
    ztex_fpga_config_start(0);
375
 
376
    for (int i=0; i<bs_size; i++) {
377
        if ( ztex_flash_read(ztex_ep0buf, (bs_start+i)<<12, 4096) ) {
378
            ztex_log ( "Error uploading bitstream from Flash: Flash read error" );
379
            ztex_fpga_config_done(CyTrue);
380
            return;
381
        }
382
        if ( ztex_fpgaconf1_send(ztex_ep0buf, 4096) ) {
383
            ztex_log ( "Error uploading bitstream from Flash: Bitstream write error" );
384
            ztex_fpga_config_done(CyTrue);
385
            return;
386
        }
387
    }
388
 
389
    ztex_fpga_config_done(CyTrue);
390
 
391
    if ( ZTEX_FPGA_CONFIGURED ) {
392
        ztex_log ( "Info: Uploaded bitstream from Flash" );
393
    }
394
    else {
395
        ztex_log ( "Error uploading bitstream from Flash: Done pin does not go high" );
396
    }
397
}
398
#endif
399
 
400
/* *********************************************************************
401
   ***** ztex_board_stop ***********************************************
402
   ********************************************************************* */
403
void ztex_board_stop() {
404
    if ( ztex_fpga_config_started ) {   // USB is stopped during configuration
405
        ztex_fpgaconf1_stop();
406
        ztex_fpga_config_started = 0;
407
    }
408
}
409
 
410
/* *********************************************************************
411
   ***** ztex_enable_otg_supply ****************************************
412
   ********************************************************************* */
413
void ztex_enable_otg_supply() {
414
    ztex_gpio_set(ZTEX_GPIO_OTG_EN, CyTrue);
415
 
416
}
417
 
418
/* *********************************************************************
419
   ***** ztex_disable_otg_supply ***************************************
420
   ********************************************************************* */
421
void ztex_disable_otg_supply() {
422
    ztex_gpio_set(ZTEX_GPIO_OTG_EN, CyFalse);
423
}
424
 
425
#endif // _ZTEX_CONF_UFM_2_18_C2_
426
#endif // _ZTEX_INCLUDE_2_
427
 

powered by: WebSVN 2.1.0

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