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 |
|
|
USB setup functions.
|
27 |
|
|
*/
|
28 |
|
|
|
29 |
|
|
void ztex_usb_start_usb() {
|
30 |
|
|
CyU3PEpConfig_t epCfg;
|
31 |
|
|
CyU3PDmaChannelConfig_t dmaCfg;
|
32 |
|
|
CyU3PUSBSpeed_t usbSpeed = CyU3PUsbGetSpeed();
|
33 |
|
|
usbSpeed = usbSpeed; // makes the compiler happy if no endpoints are defined
|
34 |
|
|
|
35 |
|
|
ZTEX_REC( CyU3PPibInit(CyTrue, ztex_pib_clock2) ); // init PIB
|
36 |
|
|
|
37 |
|
|
CyU3PMemSet ((uint8_t *)&epCfg, 0, sizeof (epCfg));
|
38 |
|
|
epCfg.enable = CyTrue;
|
39 |
|
|
epCfg.streams = 0;
|
40 |
|
|
|
41 |
|
|
CyU3PMemSet ((uint8_t *)&dmaCfg, 0, sizeof (dmaCfg));
|
42 |
|
|
dmaCfg.dmaMode = CY_U3P_DMA_MODE_BYTE;
|
43 |
|
|
|
44 |
|
|
#define DIR_IN 128
|
45 |
|
|
#define DIR_OUT 0
|
46 |
|
|
#define TYPE_ISO 1
|
47 |
|
|
#define TYPE_BULK 2
|
48 |
|
|
#define TYPE_INT 3
|
49 |
|
|
#define IS_SUPER_SPEED ( usbSpeed == CY_U3P_SUPER_SPEED )
|
50 |
|
|
#define INTERFACE(num,body) body
|
51 |
|
|
#define EP(num,dir,type,epsize,burst,interval,body) \
|
52 |
|
|
switch (usbSpeed) { \
|
53 |
|
|
case CY_U3P_FULL_SPEED: \
|
54 |
|
|
epCfg.pcktSize = 64; \
|
55 |
|
|
break; \
|
56 |
|
|
case CY_U3P_HIGH_SPEED: \
|
57 |
|
|
epCfg.pcktSize = ((TYPE_##type==2) && (epsize>512)) ? 512 : epsize; \
|
58 |
|
|
break; \
|
59 |
|
|
default: \
|
60 |
|
|
epCfg.pcktSize = epsize; \
|
61 |
|
|
break; \
|
62 |
|
|
} \
|
63 |
|
|
epCfg.epType = TYPE_##type; \
|
64 |
|
|
epCfg.burstLen = usbSpeed == CY_U3P_SUPER_SPEED ? burst : 1; \
|
65 |
|
|
dmaCfg.size = ((epCfg.pcktSize*epCfg.burstLen + 0x0F) & ~0x0F); \
|
66 |
|
|
epCfg.isoPkts = 1; \
|
67 |
|
|
ZTEX_REC(CyU3PSetEpConfig(DIR_##dir | num, &epCfg)); \
|
68 |
|
|
ZTEX_REC(CyU3PUsbFlushEp(num)); \
|
69 |
|
|
ZTEX_REC(CyU3PUsbStall (num, CyFalse, CyTrue)); \
|
70 |
|
|
ZTEX_REC(CyU3PUsbResetEp(num)); \
|
71 |
|
|
dmaCfg.prodSckId = DIR_##dir ? 0xffff : CY_U3P_UIB_SOCKET_PROD_##num; \
|
72 |
|
|
dmaCfg.consSckId = DIR_##dir ? CY_U3P_UIB_SOCKET_CONS_##num : 0xffff; \
|
73 |
|
|
dmaCfg.notification = 0; \
|
74 |
|
|
dmaCfg.cb = 0; \
|
75 |
|
|
body
|
76 |
|
|
#define DMA(handle,dmatype,dmasize,dmacount,socket,body) \
|
77 |
|
|
if (dmaCfg.prodSckId==0xffff) dmaCfg.prodSckId=socket; \
|
78 |
|
|
if (dmaCfg.consSckId==0xffff) dmaCfg.consSckId=socket; \
|
79 |
|
|
dmaCfg.size = dmasize*1024; \
|
80 |
|
|
dmaCfg.count = dmacount; \
|
81 |
|
|
body \
|
82 |
|
|
ZTEX_REC(CyU3PDmaChannelCreate (&handle, dmatype, &dmaCfg));
|
83 |
|
|
#define CB(cbfunc,not) \
|
84 |
|
|
dmaCfg.notification = not; \
|
85 |
|
|
dmaCfg.cb = cbfunc;
|
86 |
|
|
EP_SETUP_ALL
|
87 |
|
|
#undef EP
|
88 |
|
|
#undef DMA
|
89 |
|
|
#undef CB
|
90 |
|
|
#undef IS_SUPER_SPEED
|
91 |
|
|
}
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
void ztex_usb_stop_usb() {
|
95 |
|
|
CyU3PEpConfig_t epCfg;
|
96 |
|
|
CyU3PMemSet ((uint8_t *)&epCfg, 0, sizeof (epCfg));
|
97 |
|
|
|
98 |
|
|
// first destry DMA channels
|
99 |
|
|
#define EP(num,dir,type,epsize,burst,interval,body) \
|
100 |
|
|
body \
|
101 |
|
|
ZTEX_REC(CyU3PUsbFlushEp(num)); \
|
102 |
|
|
ZTEX_REC(CyU3PSetEpConfig(num, &epCfg));
|
103 |
|
|
#define DMA(handle,dmatype,dmasize,dmacount,socket,body) \
|
104 |
|
|
ZTEX_REC(CyU3PDmaChannelDestroy(&handle));
|
105 |
|
|
EP_SETUP_ALL;
|
106 |
|
|
#undef EP
|
107 |
|
|
#undef DMA
|
108 |
|
|
ZTEX_REC( CyU3PPibDeInit() );
|
109 |
|
|
}
|