NUC472_NUC442_BSP V3.03.005
The Board Support Package for NUC472/NUC442
usbd.h
Go to the documentation of this file.
1/**************************************************************************/
12#ifndef __USBD_H__
13#define __USBD_H__
14
15#ifdef __cplusplus
16extern "C"
17{
18#endif
19
20
33#define USBD_MAX_EP 12
34
35#define Maximum(a,b) (a)>(b) ? (a) : (b)
36#define Minimum(a,b) (a)<(b) ? (a) : (b)
37
38
39#define CEP 0xff
40#define EPA 0
41#define EPB 1
42#define EPC 2
43#define EPD 3
44#define EPE 4
45#define EPF 5
46#define EPG 6
47#define EPH 7
48#define EPI 8
49#define EPJ 9
50#define EPK 10
51#define EPL 11
53/* USB Request Type */
54#define REQ_STANDARD 0x00
55#define REQ_CLASS 0x20
56#define REQ_VENDOR 0x40
57
58/* USB Standard Request */
59#define GET_STATUS 0x00
60#define CLEAR_FEATURE 0x01
61#define SET_FEATURE 0x03
62#define SET_ADDRESS 0x05
63#define GET_DESCRIPTOR 0x06
64#define SET_DESCRIPTOR 0x07
65#define GET_CONFIGURATION 0x08
66#define SET_CONFIGURATION 0x09
67#define GET_INTERFACE 0x0A
68#define SET_INTERFACE 0x0B
69#define SYNC_FRAME 0x0C
70
71/* USB Descriptor Type */
72#define DESC_DEVICE 0x01
73#define DESC_CONFIG 0x02
74#define DESC_STRING 0x03
75#define DESC_INTERFACE 0x04
76#define DESC_ENDPOINT 0x05
77#define DESC_QUALIFIER 0x06
78#define DESC_OTHERSPEED 0x07
79#define DESC_IFPOWER 0x08
80#define DESC_OTG 0x09
81
82/* USB HID Descriptor Type */
83#define DESC_HID 0x21
84#define DESC_HID_RPT 0x22
85
86/* USB Descriptor Length */
87#define LEN_DEVICE 18
88#define LEN_QUALIFIER 10
89#define LEN_CONFIG 9
90#define LEN_INTERFACE 9
91#define LEN_ENDPOINT 7
92#define LEN_OTG 5
93#define LEN_HID 9
94
95/* USB Endpoint Type */
96#define EP_ISO 0x01
97#define EP_BULK 0x02
98#define EP_INT 0x03
99
100#define EP_INPUT 0x80
101#define EP_OUTPUT 0x00
102
103/* USB Feature Selector */
104#define FEATURE_DEVICE_REMOTE_WAKEUP 0x01
105#define FEATURE_ENDPOINT_HALT 0x00
107/********************* Bit definition of CEPCTL register **********************/
108#define USB_CEPCTL_NAKCLR ((uint32_t)0x00000000)
109#define USB_CEPCTL_STALL ((uint32_t)0x00000002)
110#define USB_CEPCTL_ZEROLEN ((uint32_t)0x00000004)
111#define USB_CEPCTL_FLUSH ((uint32_t)0x00000008)
113/********************* Bit definition of EPxRSPCTL register **********************/
114#define USB_EP_RSPCTL_FLUSH ((uint32_t)0x00000001)
115#define USB_EP_RSPCTL_MODE_AUTO ((uint32_t)0x00000000)
116#define USB_EP_RSPCTL_MODE_MANUAL ((uint32_t)0x00000002)
117#define USB_EP_RSPCTL_MODE_FLY ((uint32_t)0x00000004)
118#define USB_EP_RSPCTL_MODE_MASK ((uint32_t)0x00000006)
119#define USB_EP_RSPCTL_TOGGLE ((uint32_t)0x00000008)
120#define USB_EP_RSPCTL_HALT ((uint32_t)0x00000010)
121#define USB_EP_RSPCTL_ZEROLEN ((uint32_t)0x00000020)
122#define USB_EP_RSPCTL_SHORTTXEN ((uint32_t)0x00000040)
123#define USB_EP_RSPCTL_DISBUF ((uint32_t)0x00000080)
125/********************* Bit definition of EPxCFG register **********************/
126#define USB_EP_CFG_VALID ((uint32_t)0x00000001)
127#define USB_EP_CFG_TYPE_BULK ((uint32_t)0x00000002)
128#define USB_EP_CFG_TYPE_INT ((uint32_t)0x00000004)
129#define USB_EP_CFG_TYPE_ISO ((uint32_t)0x00000006)
130#define USB_EP_CFG_TYPE_MASK ((uint32_t)0x00000006)
131#define USB_EP_CFG_DIR_OUT ((uint32_t)0x00000000)
132#define USB_EP_CFG_DIR_IN ((uint32_t)0x00000008) /* end of group NUC472_442_USBD_EXPORTED_CONSTANTS */
136
142typedef struct USBD_CMD_STRUCT
143{
145 uint8_t bRequest;
146 uint16_t wValue;
147 uint16_t wIndex;
148 uint16_t wLength;
149
155typedef struct s_usbd_info
156{
157 uint8_t *gu8DevDesc;
158 uint8_t *gu8ConfigDesc;
159 uint8_t **gu8StringDesc;
160 uint8_t *gu8QualDesc;
165} S_USBD_INFO_T; /* end of group NUC472_442_USBD_EXPORTED_STRUCT */
169
171extern uint32_t g_u32EpStallLock;
172extern uint8_t g_usbd_Configured;
173extern uint8_t g_usbd_ShortPacket;
174extern uint8_t g_usbd_CtrlZero;
175extern uint8_t g_usbd_UsbAddr;
176extern uint8_t g_usbd_EpHalt[];
177extern uint32_t volatile g_usbd_DmaDone;
178extern uint32_t g_usbd_CtrlInSize;
179extern S_USBD_INFO_T gsInfo;
180extern S_USBD_CMD_T gUsbCmd;
182
187#define USBD_ENABLE_USB() ((uint32_t)(USBD->PHYCTL |= (USBD_PHYCTL_PHYEN_Msk|USBD_PHYCTL_DPPUEN_Msk)))
188#define USBD_DISABLE_USB() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk))
189#define USBD_ENABLE_PHY() ((uint32_t)(USBD->PHYCTL |= (USBD_PHYCTL_PHYEN_Msk|USBD_PHYCTL_DPPUEN_Msk)))
190#define USBD_DISABLE_PHY() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_PHYEN_Msk))
191#define USBD_SET_SE0() ((uint32_t)(USBD->PHYCTL &= ~USBD_PHYCTL_DPPUEN_Msk))
192#define USBD_CLR_SE0() ((uint32_t)(USBD->PHYCTL |= USBD_PHYCTL_DPPUEN_Msk))
193#define USBD_SET_ADDR(addr) (USBD->FADDR = (addr))
194#define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR))
195#define USBD_ENABLE_USB_INT(intr) (USBD->GINTEN = (intr))
196#define USBD_ENABLE_BUS_INT(intr) (USBD->BUSINTEN = (intr))
197#define USBD_GET_BUS_INT_FLAG() (USBD->BUSINTSTS)
198#define USBD_CLR_BUS_INT_FLAG(flag) (USBD->BUSINTSTS = flag)
199#define USBD_ENABLE_CEP_INT(intr) (USBD->CEPINTEN = (intr))
200#define USBD_CLR_CEP_INT_FLAG(flag) (USBD->CEPINTSTS = flag)
201#define USBD_SET_CEP_STATE(flag) (USBD->CEPCTL = flag)
202#define USBD_START_CEP_IN(size) (USBD->CEPTXCNT = size)
203#define USBD_SET_MAX_PAYLOAD(ep, size) (USBD->EP[ep].EPMPS = (size))
204#define USBD_ENABLE_EP_INT(ep, intr) (USBD->EP[ep].EPINTEN = (intr))
205#define USBD_GET_EP_INT_FLAG(ep) (USBD->EP[ep].EPINTSTS)
206#define USBD_CLR_EP_INT_FLAG(ep, flag) (USBD->EP[ep].EPINTSTS = (flag))
207#define USBD_SET_DMA_LEN(len) (USBD->DMACNT = len)
208#define USBD_SET_DMA_ADDR(addr) (USBD->DMAADDR = addr)
209#define USBD_SET_DMA_READ(epnum) (USBD->DMACTL = (USBD->DMACTL & ~USBD_DMACTL_EPNUM_Msk) | USBD_DMACTL_DMARD_Msk | epnum)
210#define USBD_SET_DMA_WRITE(epnum) (USBD->DMACTL = (USBD->DMACTL & ~(USBD_DMACTL_EPNUM_Msk | USBD_DMACTL_DMARD_Msk)) | epnum)
211#define USBD_ENABLE_DMA() (USBD->DMACTL |= USBD_DMACTL_DMAEN_Msk)
212#define USBD_IS_ATTACHED() ((uint32_t)(USBD->PHYCTL & USBD_PHYCTL_VBUSDET_Msk)) /* end of group NUC472_442_USBD_EXPORTED_MACROS */
215
226static __INLINE void USBD_MemCopy(uint8_t *u8Dst, uint8_t *u8Src, int32_t i32Size)
227{
228 while (i32Size--) *u8Dst++ = *u8Src++;
229}
230
236static __INLINE void USBD_ResetDMA(void)
237{
238 USBD->DMACNT = 0;
239 USBD->DMACTL = 0x80;
240 USBD->DMACTL = 0x00;
241}
249static __INLINE void USBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
250{
251 if (u32Ep == CEP)
252 {
253 USBD->CEPBUFSTART = u32Base;
254 USBD->CEPBUFEND = u32Base + u32Len - 1;
255 }
256 else
257 {
258 USBD->EP[u32Ep].EPBUFSTART = u32Base;
259 USBD->EP[u32Ep].EPBUFEND = u32Base + u32Len - 1;
260 }
261}
262
271static __INLINE void USBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
272{
273 if (u32EpType == USB_EP_CFG_TYPE_BULK)
274 USBD->EP[u32Ep].EPRSPCTL = (USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_AUTO);
275 else if (u32EpType == USB_EP_CFG_TYPE_INT)
276 USBD->EP[u32Ep].EPRSPCTL = (USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_MANUAL);
277 else if (u32EpType == USB_EP_CFG_TYPE_ISO)
278 USBD->EP[u32Ep].EPRSPCTL = (USB_EP_RSPCTL_FLUSH|USB_EP_RSPCTL_MODE_FLY);
279
280 USBD->EP[u32Ep].EPCFG = (u32EpType|u32EpDir|USB_EP_CFG_VALID|(u32EpNum << 4));
281}
282
289static __INLINE void USBD_SetEpStall(uint32_t u32Ep)
290{
291 if (u32Ep == CEP)
293 else
294 {
295 USBD->EP[u32Ep].EPRSPCTL = USBD->EP[u32Ep].EPRSPCTL & 0xf7 | USB_EP_RSPCTL_HALT;
296 }
297}
298
307static __INLINE void USBD_SetStall(uint32_t u32EpNum)
308{
309 int i;
310
311 if (u32EpNum == 0)
313 else
314 {
315 for (i=0; i<USBD_MAX_EP; i++)
316 {
317 if (((USBD->EP[i].EPCFG & 0xf0) >> 4) == u32EpNum)
318 {
319 USBD->EP[i].EPRSPCTL = USBD->EP[i].EPRSPCTL & 0xf7 | USB_EP_RSPCTL_HALT;
320 }
321 }
322 }
323}
324
331static __INLINE void USBD_ClearEpStall(uint32_t u32Ep)
332{
333 USBD->EP[u32Ep].EPRSPCTL = USB_EP_RSPCTL_TOGGLE;
334}
335
344static __INLINE void USBD_ClearStall(uint32_t u32EpNum)
345{
346 int i;
347
348 for (i=0; i<USBD_MAX_EP; i++)
349 {
350 if (((USBD->EP[i].EPCFG & 0xf0) >> 4) == u32EpNum)
351 {
352 USBD->EP[i].EPRSPCTL = USB_EP_RSPCTL_TOGGLE;
353 }
354 }
355}
356
364static __INLINE uint32_t USBD_GetEpStall(uint32_t u32Ep)
365{
366 return (USBD->EP[u32Ep].EPRSPCTL & USB_EP_RSPCTL_HALT);
367}
368
378static __INLINE uint32_t USBD_GetStall(uint32_t u32EpNum)
379{
380 int i;
381
382 for (i=0; i<USBD_MAX_EP; i++)
383 {
384 if (((USBD->EP[i].EPCFG & 0xf0) >> 4) == u32EpNum)
385 {
386 return (USBD->EP[i].EPRSPCTL & USB_EP_RSPCTL_HALT);
387 }
388 }
389 return 0;
390}
391
392
393/*-------------------------------------------------------------------------------------------*/
394typedef void (*VENDOR_REQ)(void);
395typedef void (*CLASS_REQ)(void);
396typedef void (*SET_INTERFACE_REQ)(uint32_t u32AltInterface);
398void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
399void USBD_Start(void);
400void USBD_ProcessSetupPacket(void);
401void USBD_StandardRequest(void);
402void USBD_UpdateDeviceState(void);
403void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size);
404void USBD_CtrlIn(void);
405void USBD_CtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
406void USBD_SwReset(void);
407void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
408
409
410 /* end of group NUC472_442_USBD_EXPORTED_FUNCTIONS */
412 /* end of group NUC472_442_USBD_Driver */
414 /* end of group NUC472_442_Device_Driver */
416
417#ifdef __cplusplus
418}
419#endif
420
421#endif //__USBD_H__
422
423/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
#define USBD
Definition: NUC472_442.h:28835
#define USB_EP_RSPCTL_TOGGLE
Definition: usbd.h:119
#define USB_EP_RSPCTL_FLUSH
Definition: usbd.h:114
#define USB_EP_RSPCTL_HALT
Definition: usbd.h:120
#define USB_EP_CFG_TYPE_BULK
Definition: usbd.h:127
#define USB_EP_RSPCTL_MODE_FLY
Definition: usbd.h:117
#define USB_EP_CFG_TYPE_ISO
Definition: usbd.h:129
#define USB_CEPCTL_STALL
Definition: usbd.h:109
#define USB_EP_CFG_VALID
Definition: usbd.h:126
#define USB_EP_RSPCTL_MODE_MANUAL
Definition: usbd.h:116
#define USB_EP_RSPCTL_MODE_AUTO
Definition: usbd.h:115
#define USB_EP_CFG_TYPE_INT
Definition: usbd.h:128
static __INLINE void USBD_ClearStall(uint32_t u32EpNum)
Clear USB endpoint stall state.
Definition: usbd.h:344
static __INLINE uint32_t USBD_GetEpStall(uint32_t u32Ep)
Get USB endpoint stall state.
Definition: usbd.h:364
static __INLINE void USBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
USBD_SetEpBufAddr, Set Endpoint buffer address.
Definition: usbd.h:249
void USBD_StandardRequest(void)
Process USB standard request.
Definition: usbd.c:274
static __INLINE void USBD_ResetDMA(void)
USBD_ResetDMA.
Definition: usbd.h:236
void(* VENDOR_REQ)(void)
Definition: usbd.h:394
void USBD_UpdateDeviceState(void)
Update Device State.
Definition: usbd.c:446
uint8_t bmRequestType
Definition: usbd.h:144
uint8_t ** gu8StringDesc
Definition: usbd.h:159
uint8_t ** gu8HidReportDesc
Definition: usbd.h:162
uint32_t * gu32HidReportSize
Definition: usbd.h:163
uint16_t wIndex
Definition: usbd.h:147
static __INLINE uint32_t USBD_GetStall(uint32_t u32EpNum)
Get USB endpoint stall state.
Definition: usbd.h:378
uint8_t * gu8OtherConfigDesc
Definition: usbd.h:161
void(* CLASS_REQ)(void)
Definition: usbd.h:395
void USBD_Start(void)
USBD Start.
Definition: usbd.c:102
void USBD_SwReset(void)
Clear all software flags.
Definition: usbd.c:595
uint16_t wLength
Definition: usbd.h:148
uint8_t bRequest
Definition: usbd.h:145
static __INLINE void USBD_MemCopy(uint8_t *u8Dst, uint8_t *u8Src, int32_t i32Size)
USBD_memcpy, Copy bytes hardware limitation.
Definition: usbd.h:226
void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size)
Prepare Control IN transaction.
Definition: usbd.c:513
static __INLINE void USBD_SetEpStall(uint32_t u32Ep)
Set USB endpoint stall state.
Definition: usbd.h:289
uint8_t * gu8ConfigDesc
Definition: usbd.h:158
void USBD_CtrlIn(void)
Start Control IN transfer.
Definition: usbd.c:530
static __INLINE void USBD_SetStall(uint32_t u32EpNum)
Set USB endpoint stall state.
Definition: usbd.h:307
void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq)
USBD Set Vendor Request.
Definition: usbd.c:616
uint8_t * gu8DevDesc
Definition: usbd.h:157
void USBD_ProcessSetupPacket(void)
Process Setup Packet.
Definition: usbd.c:116
static __INLINE void USBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
USBD_ConfigEp, Config Endpoint.
Definition: usbd.h:271
uint8_t * gu8QualDesc
Definition: usbd.h:160
void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface)
USBD Initial.
Definition: usbd.c:70
static __INLINE void USBD_ClearEpStall(uint32_t u32Ep)
Clear USB endpoint stall state.
Definition: usbd.h:331
void USBD_CtrlOut(uint8_t *pu8Buf, uint32_t u32Size)
Start Control OUT transaction.
Definition: usbd.c:570
void(* SET_INTERFACE_REQ)(uint32_t u32AltInterface)
Definition: usbd.h:396
uint16_t wValue
Definition: usbd.h:146
#define USBD_SET_CEP_STATE(flag)
Definition: usbd.h:201
struct s_usbd_info S_USBD_INFO_T
struct USBD_CMD_STRUCT S_USBD_CMD_T