| NUC472_NUC442_BSP V3.04.000 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
142 typedef struct USBD_CMD_STRUCT
143{
144 uint8_t bmRequestType;
145 uint8_t bRequest;
146 uint16_t wValue;
147 uint16_t wIndex;
148 uint16_t wLength;
149
150} S_USBD_CMD_T;
155 typedef struct s_usbd_info
156{
157 uint8_t *gu8DevDesc;
158 uint8_t *gu8ConfigDesc;
159 uint8_t **gu8StringDesc;
160 uint8_t *gu8QualDesc;
161 uint8_t *gu8OtherConfigDesc;
162 uint8_t **gu8HidReportDesc;
163 uint32_t *gu32HidReportSize;
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
226 static __INLINE void USBD_MemCopy(uint8_t *u8Dst, uint8_t *u8Src, int32_t i32Size)
227{
228 while (i32Size--) *u8Dst++ = *u8Src++;
229}
230
236 static __INLINE void USBD_ResetDMA(void)
237{
238 USBD->DMACNT = 0;
239 USBD->DMACTL = 0x80;
240 USBD->DMACTL = 0x00;
241}
249 static __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
271 static __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
289 static __INLINE void USBD_SetEpStall(uint32_t u32Ep)
290{
291 if (u32Ep == CEP)
292 USBD_SET_CEP_STATE(USB_CEPCTL_STALL);
293 else
294 {
295 USBD->EP[u32Ep].EPRSPCTL = (USBD->EP[u32Ep].EPRSPCTL & 0xf7) | USB_EP_RSPCTL_HALT;
296 }
297}
298
307 static __INLINE void USBD_SetStall(uint32_t u32EpNum)
308{
309 int i;
310
311 if (u32EpNum == 0)
312 USBD_SET_CEP_STATE(USB_CEPCTL_STALL);
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
331 static __INLINE void USBD_ClearEpStall(uint32_t u32Ep)
332{
333 USBD->EP[u32Ep].EPRSPCTL = USB_EP_RSPCTL_TOGGLE;
334}
335
344 static __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
364 static __INLINE uint32_t USBD_GetEpStall(uint32_t u32Ep)
365{
366 return (USBD->EP[u32Ep].EPRSPCTL & USB_EP_RSPCTL_HALT);
367}
368
378 static __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/*-------------------------------------------------------------------------------------------*/
394 typedef void (*VENDOR_REQ)(void);
395 typedef void (*CLASS_REQ)(void);
396 typedef 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. ***/
USBD
#define USBD
Definition: NUC472_442.h:28835
USB_EP_RSPCTL_TOGGLE
#define USB_EP_RSPCTL_TOGGLE
Definition: usbd.h:119
USB_EP_RSPCTL_FLUSH
#define USB_EP_RSPCTL_FLUSH
Definition: usbd.h:114
USB_EP_RSPCTL_HALT
#define USB_EP_RSPCTL_HALT
Definition: usbd.h:120
USB_EP_CFG_TYPE_BULK
#define USB_EP_CFG_TYPE_BULK
Definition: usbd.h:127
USB_EP_RSPCTL_MODE_FLY
#define USB_EP_RSPCTL_MODE_FLY
Definition: usbd.h:117
USB_EP_CFG_TYPE_ISO
#define USB_EP_CFG_TYPE_ISO
Definition: usbd.h:129
USB_CEPCTL_STALL
#define USB_CEPCTL_STALL
Definition: usbd.h:109
USB_EP_CFG_VALID
#define USB_EP_CFG_VALID
Definition: usbd.h:126
USB_EP_RSPCTL_MODE_MANUAL
#define USB_EP_RSPCTL_MODE_MANUAL
Definition: usbd.h:116
USB_EP_RSPCTL_MODE_AUTO
#define USB_EP_RSPCTL_MODE_AUTO
Definition: usbd.h:115
USB_EP_CFG_TYPE_INT
#define USB_EP_CFG_TYPE_INT
Definition: usbd.h:128
USBD_ClearStall
static __INLINE void USBD_ClearStall(uint32_t u32EpNum)
Clear USB endpoint stall state.
Definition: usbd.h:344
USBD_GetEpStall
static __INLINE uint32_t USBD_GetEpStall(uint32_t u32Ep)
Get USB endpoint stall state.
Definition: usbd.h:364
USBD_SetEpBufAddr
static __INLINE void USBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
USBD_SetEpBufAddr, Set Endpoint buffer address.
Definition: usbd.h:249
USBD_StandardRequest
void USBD_StandardRequest(void)
Process USB standard request.
Definition: usbd.c:274
USBD_ResetDMA
static __INLINE void USBD_ResetDMA(void)
USBD_ResetDMA.
Definition: usbd.h:236
VENDOR_REQ
void(* VENDOR_REQ)(void)
Definition: usbd.h:394
USBD_UpdateDeviceState
void USBD_UpdateDeviceState(void)
Update Device State.
Definition: usbd.c:446
USBD_CMD_STRUCT::bmRequestType
uint8_t bmRequestType
Definition: usbd.h:144
s_usbd_info::gu8StringDesc
uint8_t ** gu8StringDesc
Definition: usbd.h:159
s_usbd_info::gu8HidReportDesc
uint8_t ** gu8HidReportDesc
Definition: usbd.h:162
s_usbd_info::gu32HidReportSize
uint32_t * gu32HidReportSize
Definition: usbd.h:163
USBD_CMD_STRUCT::wIndex
uint16_t wIndex
Definition: usbd.h:147
USBD_GetStall
static __INLINE uint32_t USBD_GetStall(uint32_t u32EpNum)
Get USB endpoint stall state.
Definition: usbd.h:378
s_usbd_info::gu8OtherConfigDesc
uint8_t * gu8OtherConfigDesc
Definition: usbd.h:161
CLASS_REQ
void(* CLASS_REQ)(void)
Definition: usbd.h:395
USBD_Start
void USBD_Start(void)
USBD Start.
Definition: usbd.c:102
USBD_SwReset
void USBD_SwReset(void)
Clear all software flags.
Definition: usbd.c:595
USBD_CMD_STRUCT::wLength
uint16_t wLength
Definition: usbd.h:148
USBD_CMD_STRUCT::bRequest
uint8_t bRequest
Definition: usbd.h:145
USBD_MemCopy
static __INLINE void USBD_MemCopy(uint8_t *u8Dst, uint8_t *u8Src, int32_t i32Size)
USBD_memcpy, Copy bytes hardware limitation.
Definition: usbd.h:226
USBD_PrepareCtrlIn
void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size)
Prepare Control IN transaction.
Definition: usbd.c:513
USBD_SetEpStall
static __INLINE void USBD_SetEpStall(uint32_t u32Ep)
Set USB endpoint stall state.
Definition: usbd.h:289
s_usbd_info::gu8ConfigDesc
uint8_t * gu8ConfigDesc
Definition: usbd.h:158
USBD_CtrlIn
void USBD_CtrlIn(void)
Start Control IN transfer.
Definition: usbd.c:530
USBD_SetStall
static __INLINE void USBD_SetStall(uint32_t u32EpNum)
Set USB endpoint stall state.
Definition: usbd.h:307
USBD_SetVendorRequest
void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq)
USBD Set Vendor Request.
Definition: usbd.c:616
s_usbd_info::gu8DevDesc
uint8_t * gu8DevDesc
Definition: usbd.h:157
USBD_ProcessSetupPacket
void USBD_ProcessSetupPacket(void)
Process Setup Packet.
Definition: usbd.c:116
USBD_ConfigEp
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
s_usbd_info::gu8QualDesc
uint8_t * gu8QualDesc
Definition: usbd.h:160
USBD_Open
void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface)
USBD Initial.
Definition: usbd.c:70
USBD_ClearEpStall
static __INLINE void USBD_ClearEpStall(uint32_t u32Ep)
Clear USB endpoint stall state.
Definition: usbd.h:331
USBD_CtrlOut
void USBD_CtrlOut(uint8_t *pu8Buf, uint32_t u32Size)
Start Control OUT transaction.
Definition: usbd.c:570
SET_INTERFACE_REQ
void(* SET_INTERFACE_REQ)(uint32_t u32AltInterface)
Definition: usbd.h:396
USBD_CMD_STRUCT::wValue
uint16_t wValue
Definition: usbd.h:146
USBD_SET_CEP_STATE
#define USBD_SET_CEP_STATE(flag)
Definition: usbd.h:201
S_USBD_INFO_T
struct s_usbd_info S_USBD_INFO_T
S_USBD_CMD_T
struct USBD_CMD_STRUCT S_USBD_CMD_T
USBD_CMD_STRUCT
Definition: usbd.h:143
s_usbd_info
Definition: usbd.h:156