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.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
    Main include file. See the examples for usage.
27
*/
28
 
29
#include "cyu3system.h"
30
#include "cyu3os.h"
31
#include "cyu3dma.h"
32
#include "cyu3error.h"
33
#include "cyu3usb.h"
34
#include "cyu3uart.h"
35
#include "cyu3pib.h"
36
 
37
#define ZTEX_VENDOR_REQ_MAX     50                      // maximum amount of vendor requests
38
#define ZTEX_VENDOR_CMD_MAX     50                      // maximum amount of vendor commands
39
 
40
typedef uint8_t (*ztex_vendor_func) (uint16_t value, uint16_t index, uint16_t length );
41
 
42
// global configuration
43
uint32_t ztex_app_thread_stack = 0x1000;        // stack size of application thread
44
uint32_t ztex_app_thread_prio = 8;              // priority of application thread, should be 7..15
45
void (*ztex_app_thread_run)() = 0;
46
void (*ztex_usb_start)() = 0;                    // called when USB connection is started
47
void (*ztex_usb_stop)() = 0;                     // called when USB connection is stopped
48
 
49
CyBool_t ztex_allow_lpm = CyFalse;              // whether to allow low power mode transitions
50
 
51
// strings for interfaces 0..7, can be overwritten by user
52
char* ztex_interface_string[] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
53
 
54
/*
55
   PIB clock settings for user application.
56
*/
57
CyU3PPibClock_t ztex_pib_clock = {
58
    .clkDiv = 6,                // 69.33 MHz @ 26 MHz external clock
59
    .clkSrc = CY_U3P_SYS_CLK,
60
    .isHalfDiv = CyFalse,
61
    .isDllEnable = CyTrue
62
};
63
 
64
/*
65
   PIB clock settings which are currently used.
66
   They may be under control of a board specific module, e.g. to load
67
   different clock settings for FPGA configuration.
68
*/
69
CyU3PPibClock_t *ztex_pib_clock2 = &ztex_pib_clock;
70
 
71
// called to clean up a transfer when a clear stall request is received, see 
72
void ep_cleanup_default_handler(uint16_t);
73
void (*ztex_ep_cleanup_handler)(uint16_t) = ep_cleanup_default_handler;
74
 
75
// system stuff
76
uint8_t ztex_usb_is_connected = 0;
77
 
78
// super speed error counters
79
#define ZTEX_USB3_SND_ERROR_COUNT (*((uint16_t*) 0xe0033014))
80
#define ZTEX_USB3_RCV_ERROR_COUNT (*((uint16_t*) 0xe0033016))
81
 
82
#ifndef EP_SETUP_FPGACONF
83
#define EP_SETUP_FPGACONF
84
#endif
85
 
86
#define EP_SETUP_ALL EP_SETUP EP_SETUP_FPGACONF
87
 
88
#include "ztex-descriptors.c"
89
#include "ztex-debug.c"
90
#include "ztex-usb.c"
91
#include "ztex-ep0.c"
92
#include "ztex-gpio.c"
93
 
94
 
95
// SPI Flash support
96
#ifdef ENABLE_SPI_FLASH
97
#define ENABLE_SPI
98
#include "ztex-flash.c"
99
#endif
100
 
101
// SD Flash support
102
#ifdef ENABLE_SD_FLASH
103
#include "ztex-sd.c"
104
#endif
105
 
106
#ifdef ENABLE_I2C
107
#include "ztex-i2c.c"
108
#endif
109
 
110
 
111
#define _ZTEX_INCLUDE_2_
112
#ifdef _ZTEX_CONF_UFM_2_02_C1_
113
#include "ztex-ufm-2_02.c"
114
#endif
115
#ifdef _ZTEX_CONF_UFM_2_14_C1_
116
#include "ztex-ufm-2_14.c"
117
#endif
118
#ifdef _ZTEX_CONF_UFM_2_18_C1_
119
#include "ztex-ufm-2_18.c"
120
#endif
121
#ifdef _ZTEX_LSI_
122
#include "ztex-lsi.c"
123
#endif
124
 
125
 
126
 
127
void ztex_usb_start_main() {
128
    ztex_usb_start_usb();
129
 
130
    if ( ztex_usb_start != 0)
131
        ztex_usb_start();
132
 
133
    CyU3PUSBSpeed_t usbSpeed = CyU3PUsbGetSpeed();
134
    ZTEX_LOG ("Info: USB setup finished: %s", usbSpeed == CY_U3P_SUPER_SPEED ? "super speed" : usbSpeed == CY_U3P_HIGH_SPEED ? "high speed" : usbSpeed == CY_U3P_FULL_SPEED ? "full speed" : "not connected" );
135
 
136
    ztex_usb_is_connected = 1;
137
}
138
 
139
void ztex_usb_stop_main() {
140
    ztex_usb_is_connected = 0;
141
 
142
    if ( ztex_usb_stop != 0)
143
        ztex_usb_stop();
144
 
145
#ifdef ENABLE_SPI_FLASH
146
    ztex_usb_stop_flash();
147
#endif
148
#ifdef ENABLE_SD_FLASH
149
    ztex_usb_stop_sd();
150
#endif
151
#ifdef _ZTEX_BOARD_
152
    ztex_board_stop();
153
#endif    
154
 
155
    ztex_usb_stop_usb();
156
 
157
    ztex_log ("Info: USB disconnected.");
158
}
159
 
160
/*
161
   Default handler to clean up a transfer. Since we do no known how endpoints
162
   are associated we reset the whole connection. This may no be what is
163
   expected by the host.
164
*/
165
void ep_cleanup_default_handler(uint16_t ep) {
166
    ztex_usb_stop_main();
167
    CyU3PThreadSleep (1);  // Give a chance for the main thread loop to run
168
    ztex_usb_start_main();
169
};
170
 
171
// USB event handler
172
void ztex_usb_event_handler ( CyU3PUsbEventType_t evtype, uint16_t evdata) {
173
//    ZTEX_LOG("Event: %d",evtype);
174
    switch (evtype)
175
    {
176
        case CY_U3P_USB_EVENT_SETCONF:
177
            // stop the connection before restarting
178
            if (ztex_usb_is_connected) {
179
                ztex_usb_stop_main();
180
            }
181
            // start the connection
182
            ztex_usb_start_main();
183
            break;
184
 
185
        case CY_U3P_USB_EVENT_RESET:
186
        case CY_U3P_USB_EVENT_DISCONNECT:
187
            // stops the connection
188
            if (ztex_usb_is_connected) {
189
                ztex_usb_stop_main();
190
            }
191
            break;
192
 
193
        default:
194
            break;
195
    }
196
}
197
 
198
 
199
 
200
// LPM (link power management request). Return value CyTrue allows transitions to low power modes.
201
CyBool_t ztex_lpm_handler ( CyU3PUsbLinkPowerMode link_mode)
202
{
203
    return ztex_allow_lpm;
204
}
205
 
206
 
207
CyU3PThread ztex_app_thread;                            // ztex application thread structure 
208
 
209
// entry function for the application thread
210
void ztex_app_thread_entry (uint32_t input)
211
{
212
    ztex_debug_init();
213
 
214
    ZTEX_REC(CyU3PUsbStart());                                          // start USB functionality
215
 
216
    CyU3PUsbRegisterSetupCallback(ztex_ep0_handler, CyTrue);            // register EP0 handler
217
 
218
    CyU3PUsbRegisterLPMRequestCallback(ztex_lpm_handler);               // register link power management handler
219
 
220
    CyU3PUsbRegisterEventCallback(ztex_usb_event_handler);              // register USB event handler
221
 
222
    // Set the USB registers
223
    ZTEX_REC( CyU3PUsbSetDesc(CY_U3P_USB_SET_SS_DEVICE_DESCR, 0, (uint8_t *)ztex_usb3_device_descriptor) );              // Super speed device descriptor
224
    ZTEX_REC( CyU3PUsbSetDesc(CY_U3P_USB_SET_HS_DEVICE_DESCR, 0, (uint8_t *)ztex_usb2_device_descriptor) );              // High speed device descriptor
225
    ZTEX_REC( CyU3PUsbSetDesc(CY_U3P_USB_SET_SS_BOS_DESCR, 0, (uint8_t *)ztex_bos_descriptor) );                         // BOS descriptor
226
    ZTEX_REC( CyU3PUsbSetDesc(CY_U3P_USB_SET_DEVQUAL_DESCR, 0, (uint8_t *)ztex_device_qualifier_descriptor) );           // Device qualifier descriptor
227
    ZTEX_REC( CyU3PUsbSetDesc(CY_U3P_USB_SET_SS_CONFIG_DESCR, 0, (uint8_t *)ztex_usb3_config_descriptor) );      // Super speed configuration descriptor
228
    ZTEX_REC( CyU3PUsbSetDesc(CY_U3P_USB_SET_HS_CONFIG_DESCR, 0, (uint8_t *)ztex_usb2_config_descriptor) );      // High speed configuration descriptor
229
    ZTEX_REC( CyU3PUsbSetDesc(CY_U3P_USB_SET_FS_CONFIG_DESCR, 0, (uint8_t *)ztex_usb1_config_descriptor) );              // Full speed configuration descriptor
230
    CyU3PUsbSetDesc(CY_U3P_USB_SET_STRING_DESCR, 0, (uint8_t *)ztex_lang_string_descriptor);                             // String descriptor 0 must not be handled by ep0_handler for an undocumented reason
231
 
232
    ztex_gpio_init();
233
 
234
#ifdef _ZTEX_BOARD_
235
    ztex_board_init();
236
#endif    
237
 
238
#ifdef ENABLE_SPI_FLASH
239
    ztex_flash_init();
240
#endif    
241
#ifdef ENABLE_SD_FLASH
242
    ztex_sd_init();
243
#endif    
244
#ifdef ENABLE_I2C
245
    ztex_i2c_init();
246
#endif    
247
#ifdef _ZTEX_LSI_
248
    ztex_lsi_init();
249
#endif    
250
#ifdef _ZTEX_FLASH_CONFIG_FUNC_
251
    _ZTEX_FLASH_CONFIG_FUNC_
252
#endif
253
 
254
    // Connect the USB Pins with super speed operation enabled.
255
    ZTEX_REC( CyU3PConnectState(CyTrue, CyTrue) );
256
 
257
    if ( ztex_app_thread_run != 0)
258
        ztex_app_thread_run();
259
 
260
    for (;;)
261
    {
262
        CyU3PThreadSleep (1000);
263
    }
264
}
265
 
266
/* This function is expected and called by OS. */
267
void CyFxApplicationDefine()
268
{
269
    /* create and start the application thread */
270
    if ( CyU3PThreadCreate(&ztex_app_thread,            // ztex application thread structure 
271
                           "21:ztex_app_thread",        // thread ID and name
272
                          ztex_app_thread_entry,        // entry function
273
                          0,                             // no input parameter
274
                          CyU3PMemAlloc(ztex_app_thread_stack), // allocate stack memory
275
                          ztex_app_thread_stack,        // stack size
276
                          ztex_app_thread_prio,         // thread priority
277
                          ztex_app_thread_prio,         // preempt threshold            
278
                          CYU3P_NO_TIME_SLICE,          // no time slice as recommended
279
                          CYU3P_AUTO_START              // start the thread immediately
280
                          ) != 0
281
        ) ztex_ec = 105;
282
}
283
 
284
/*
285
 * Main function. Configures the IO-Matrix and starts the OS
286
 */
287
void ztex_main (void)
288
{
289
    ztex_ec = 101;
290
 
291
    /* Initialize the device */
292
    CyU3PSysClockConfig_t clock_cfg;
293
    clock_cfg.setSysClk400 = CyFalse;
294
    clock_cfg.cpuClkDiv = 2;
295
    clock_cfg.dmaClkDiv = 2;
296
    clock_cfg.mmioClkDiv = 2;
297
    clock_cfg.useStandbyClk = CyFalse;
298
    clock_cfg.clkSrc = CY_U3P_SYS_CLK;
299
    if ( CyU3PDeviceInit(&clock_cfg) != CY_U3P_SUCCESS ) ztex_ec = 102;
300
 
301
    /* Initialize the caches. Enable both Instruction and Data Caches. */
302
    if ( CyU3PDeviceCacheControl(CyTrue, CyTrue, CyTrue) != CY_U3P_SUCCESS ) ztex_ec = 103;
303
 
304
    CyU3PIoMatrixConfig_t io_cfg;
305
    CyU3PMemSet((uint8_t *)&io_cfg, 0, sizeof(io_cfg));
306
    io_cfg.isDQ32Bit = CyFalse;
307
#ifdef ENABLE_SPORT0
308
    io_cfg.s0Mode = CY_U3P_SPORT_4BIT;
309
#else    
310
    io_cfg.s0Mode = CY_U3P_SPORT_INACTIVE;
311
#endif
312
    io_cfg.s1Mode = CY_U3P_SPORT_INACTIVE;
313
#ifdef ENABLE_UART    
314
    io_cfg.useUart = CyTrue;
315
#else    
316
    io_cfg.useUart = CyFalse;
317
#endif    
318
#ifdef ENABLE_I2C
319
    io_cfg.useI2C = CyTrue;
320
#else
321
    io_cfg.useI2C = CyFalse;
322
#endif
323
    io_cfg.useI2S = CyFalse;
324
#ifdef ENABLE_SPI
325
    io_cfg.useSpi = CyTrue;
326
#else
327
    io_cfg.useSpi = CyFalse;
328
#endif
329
    io_cfg.lppMode = CY_U3P_IO_MATRIX_LPP_DEFAULT;
330
    /* No GPIOs are enabled. */
331
    io_cfg.gpioSimpleEn[0] = ( ZTEX_GPIO_SIMPLE_BITMAP0 | GPIO_SIMPLE_BITMAP0 ) & ( ~(ZTEX_GPIO_COMPLEX_BITMAP0 | GPIO_COMPLEX_BITMAP0 ) );
332
    io_cfg.gpioSimpleEn[1] = ( ZTEX_GPIO_SIMPLE_BITMAP1 | GPIO_SIMPLE_BITMAP1 ) & ( ~(ZTEX_GPIO_COMPLEX_BITMAP1 | GPIO_COMPLEX_BITMAP1 ) );
333
    io_cfg.gpioComplexEn[0] = ZTEX_GPIO_COMPLEX_BITMAP0 | GPIO_COMPLEX_BITMAP0;
334
    io_cfg.gpioComplexEn[1] = ZTEX_GPIO_COMPLEX_BITMAP1 | GPIO_COMPLEX_BITMAP1;
335
    if (CyU3PDeviceConfigureIOMatrix (&io_cfg) != CY_U3P_SUCCESS) ztex_ec = 104;
336
 
337
    /* This is a non returnable call for initializing the RTOS kernel */
338
    ztex_ec = 0;
339
    CyU3PKernelEntry ();
340
}
341
 

powered by: WebSVN 2.1.0

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