M480 BSP V3.05.006
The Board Support Package for M480 Series
usbd.h
Go to the documentation of this file.
1/**************************************************************************/
9#ifndef __USBD_H__
10#define __USBD_H__
11
12#ifdef __cplusplus
13extern "C"
14{
15#endif
16
17
29typedef struct s_usbd_info
30{
31 uint8_t *gu8DevDesc;
32 uint8_t *gu8ConfigDesc;
33 uint8_t **gu8StringDesc;
34 uint8_t **gu8HidReportDesc;
35 uint8_t *gu8BosDesc;
41extern const S_USBD_INFO_T gsInfo;
42 /* end of group USBD_EXPORTED_STRUCT */
44
45
46
47
51#define USBD_BUF_BASE (USBD_BASE+0x100ul)
52#define USBD_MAX_EP 12ul
54#define EP0 0ul
55#define EP1 1ul
56#define EP2 2ul
57#define EP3 3ul
58#define EP4 4ul
59#define EP5 5ul
60#define EP6 6ul
61#define EP7 7ul
62#define EP8 8ul
63#define EP9 9ul
64#define EP10 10ul
65#define EP11 11ul
68/* USB Request Type */
69#define REQ_STANDARD 0x00ul
70#define REQ_CLASS 0x20ul
71#define REQ_VENDOR 0x40ul
72
73/* USB Standard Request */
74#define GET_STATUS 0x00ul
75#define CLEAR_FEATURE 0x01ul
76#define SET_FEATURE 0x03ul
77#define SET_ADDRESS 0x05ul
78#define GET_DESCRIPTOR 0x06ul
79#define SET_DESCRIPTOR 0x07ul
80#define GET_CONFIGURATION 0x08ul
81#define SET_CONFIGURATION 0x09ul
82#define GET_INTERFACE 0x0Aul
83#define SET_INTERFACE 0x0Bul
84#define SYNC_FRAME 0x0Cul
85
86/* USB Descriptor Type */
87#define DESC_DEVICE 0x01ul
88#define DESC_CONFIG 0x02ul
89#define DESC_STRING 0x03ul
90#define DESC_INTERFACE 0x04ul
91#define DESC_ENDPOINT 0x05ul
92#define DESC_QUALIFIER 0x06ul
93#define DESC_OTHERSPEED 0x07ul
94#define DESC_IFPOWER 0x08ul
95#define DESC_OTG 0x09ul
96#define DESC_BOS 0x0Ful
97#define DESC_CAPABILITY 0x10ul
98
99/* USB Device Capability Type */
100#define CAP_WIRELESS 0x01ul
101#define CAP_USB20_EXT 0x02ul
102
103/* USB HID Descriptor Type */
104#define DESC_HID 0x21ul
105#define DESC_HID_RPT 0x22ul
106
107/* USB Descriptor Length */
108#define LEN_DEVICE 18ul
109#define LEN_QUALIFIER 10ul
110#define LEN_CONFIG 9ul
111#define LEN_INTERFACE 9ul
112#define LEN_ENDPOINT 7ul
113#define LEN_OTG 5ul
114#define LEN_BOS 5ul
115#define LEN_HID 9ul
116#define LEN_CCID 0x36ul
117#define LEN_BOSCAP 7ul
118
119/* USB Endpoint Type */
120#define EP_ISO 0x01
121#define EP_BULK 0x02
122#define EP_INT 0x03
123
124#define EP_INPUT 0x80
125#define EP_OUTPUT 0x00
126
127/* USB Feature Selector */
128#define FEATURE_DEVICE_REMOTE_WAKEUP 0x01ul
129#define FEATURE_ENDPOINT_HALT 0x00ul
132/******************************************************************************/
133/* USB Specific Macros */
134/******************************************************************************/
135
136#define USBD_WAKEUP_EN USBD_INTEN_WKEN_Msk
137#define USBD_DRVSE0 USBD_SE0_SE0_Msk
139#define USBD_DPPU_EN USBD_ATTR_DPPUEN_Msk
140#define USBD_PWRDN USBD_ATTR_PWRDN_Msk
141#define USBD_PHY_EN USBD_ATTR_PHYEN_Msk
142#define USBD_USB_EN USBD_ATTR_USBEN_Msk
144#define USBD_INT_BUS USBD_INTEN_BUSIEN_Msk
145#define USBD_INT_USB USBD_INTEN_USBIEN_Msk
146#define USBD_INT_FLDET USBD_INTEN_VBDETIEN_Msk
147#define USBD_INT_WAKEUP (USBD_INTEN_NEVWKIEN_Msk | USBD_INTEN_WKEN_Msk)
149#define USBD_INTSTS_WAKEUP USBD_INTSTS_NEVWKIF_Msk
150#define USBD_INTSTS_FLDET USBD_INTSTS_VBDETIF_Msk
151#define USBD_INTSTS_BUS USBD_INTSTS_BUSIF_Msk
152#define USBD_INTSTS_USB USBD_INTSTS_USBIF_Msk
153#define USBD_INTSTS_SETUP USBD_INTSTS_SETUP_Msk
154#define USBD_INTSTS_EP0 USBD_INTSTS_EPEVT0_Msk
155#define USBD_INTSTS_EP1 USBD_INTSTS_EPEVT1_Msk
156#define USBD_INTSTS_EP2 USBD_INTSTS_EPEVT2_Msk
157#define USBD_INTSTS_EP3 USBD_INTSTS_EPEVT3_Msk
158#define USBD_INTSTS_EP4 USBD_INTSTS_EPEVT4_Msk
159#define USBD_INTSTS_EP5 USBD_INTSTS_EPEVT5_Msk
160#define USBD_INTSTS_EP6 USBD_INTSTS_EPEVT6_Msk
161#define USBD_INTSTS_EP7 USBD_INTSTS_EPEVT7_Msk
162#define USBD_INTSTS_EP8 USBD_INTSTS_EPEVT8_Msk
163#define USBD_INTSTS_EP9 USBD_INTSTS_EPEVT9_Msk
164#define USBD_INTSTS_EP10 USBD_INTSTS_EPEVT10_Msk
165#define USBD_INTSTS_EP11 USBD_INTSTS_EPEVT11_Msk
167#define USBD_STATE_USBRST USBD_ATTR_USBRST_Msk
168#define USBD_STATE_SUSPEND USBD_ATTR_SUSPEND_Msk
169#define USBD_STATE_RESUME USBD_ATTR_RESUME_Msk
170#define USBD_STATE_TIMEOUT USBD_ATTR_TOUT_Msk
172#define USBD_CFGP_SSTALL USBD_CFGP_SSTALL_Msk
173#define USBD_CFG_CSTALL USBD_CFG_CSTALL_Msk
175#define USBD_CFG_EPMODE_DISABLE (0ul << USBD_CFG_STATE_Pos)
176#define USBD_CFG_EPMODE_OUT (1ul << USBD_CFG_STATE_Pos)
177#define USBD_CFG_EPMODE_IN (2ul << USBD_CFG_STATE_Pos)
178#define USBD_CFG_TYPE_ISO (1ul << USBD_CFG_ISOCH_Pos) /* end of group USBD_EXPORTED_CONSTANTS */
183
184
199#define USBD_Maximum(a,b) ((a)>(b) ? (a) : (b))
200
201
213#define USBD_Minimum(a,b) ((a)<(b) ? (a) : (b))
214
215
226#define USBD_ENABLE_USB() ((uint32_t)(USBD->ATTR |= 0x7D0))
227
238#define USBD_DISABLE_USB() ((uint32_t)(USBD->ATTR &= ~USBD_USB_EN))
239
250#define USBD_ENABLE_PHY() ((uint32_t)(USBD->ATTR |= USBD_PHY_EN))
251
262#define USBD_DISABLE_PHY() ((uint32_t)(USBD->ATTR &= ~USBD_PHY_EN))
263
274#define USBD_SET_SE0() ((uint32_t)(USBD->SE0 |= USBD_DRVSE0))
275
286#define USBD_CLR_SE0() ((uint32_t)(USBD->SE0 &= ~USBD_DRVSE0))
287
298#define USBD_SET_ADDR(addr) (USBD->FADDR = (addr))
299
310#define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR))
311
325#define USBD_ENABLE_INT(intr) (USBD->INTEN |= (intr))
326
337#define USBD_GET_INT_FLAG() ((uint32_t)(USBD->INTSTS))
338
352#define USBD_CLR_INT_FLAG(flag) (USBD->INTSTS = (flag))
353
364#define USBD_GET_EP_FLAG() ((uint32_t)(USBD->EPSTS))
365
380#define USBD_GET_BUS_STATE() ((uint32_t)(USBD->ATTR & 0xf))
381
393#define USBD_IS_ATTACHED() ((uint32_t)(USBD->VBUSDET & USBD_VBUSDET_VBUSDET_Msk))
394
405#define USBD_STOP_TRANSACTION(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_CLRRDY_Msk)
406
418#define USBD_SET_DATA1(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DSQSYNC_Msk)
419
431#define USBD_SET_DATA0(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DSQSYNC_Msk))
432
445#define USBD_SET_PAYLOAD_LEN(ep, size) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))) = (size))
446
457#define USBD_GET_PAYLOAD_LEN(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))))
458
471#define USBD_CONFIG_EP(ep, config) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) = (config))
472
485#define USBD_SET_EP_BUF_ADDR(ep, offset) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))) = (offset))
486
497#define USBD_GET_EP_BUF_ADDR(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))))
498
509#define USBD_SET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0ul].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_SSTALL_Msk)
510
521#define USBD_CLR_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) &= ~USBD_CFGP_SSTALL_Msk)
522
534#define USBD_GET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) & USBD_CFGP_SSTALL_Msk)
535
550__STATIC_INLINE void USBD_MemCopy(uint8_t dest[], uint8_t src[], uint32_t size)
551{
552 uint32_t volatile i=0ul;
553
554 while(size--)
555 {
556 dest[i] = src[i];
557 i++;
558 }
559}
560
571__STATIC_INLINE void USBD_SetStall(uint8_t epnum)
572{
573 uint32_t u32CfgAddr;
574 uint32_t u32Cfg;
575 uint32_t i;
576
577 for(i = 0ul; i < USBD_MAX_EP; i++)
578 {
579 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
580 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
581
582 if((u32Cfg & 0xful) == epnum)
583 {
584 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
585 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
586
587 *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg | USBD_CFGP_SSTALL);
588 break;
589 }
590 }
591}
592
602__STATIC_INLINE void USBD_ClearStall(uint8_t epnum)
603{
604 uint32_t u32CfgAddr;
605 uint32_t u32Cfg;
606 uint32_t i;
607
608 for(i = 0ul; i < USBD_MAX_EP; i++)
609 {
610 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
611 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
612
613 if((u32Cfg & 0xful) == epnum)
614 {
615 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
616 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
617
618 *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg & ~USBD_CFGP_SSTALL);
619 break;
620 }
621 }
622}
623
635__STATIC_INLINE uint32_t USBD_GetStall(uint8_t epnum)
636{
637 uint32_t u32CfgAddr;
638 uint32_t u32Cfg;
639 uint32_t i;
640
641 for(i = 0ul; i < USBD_MAX_EP; i++)
642 {
643 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
644 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
645
646 if((u32Cfg & 0xful) == epnum)
647 {
648 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
649 break;
650 }
651 }
652
653 return ((*((__IO uint32_t *)(u32CfgAddr))) & USBD_CFGP_SSTALL);
654}
655
656
657extern volatile uint8_t g_usbd_RemoteWakeupEn;
658
659
660typedef void (*VENDOR_REQ)(void);
661typedef void (*CLASS_REQ)(void);
662typedef void (*SET_INTERFACE_REQ)(uint32_t u32AltInterface);
663typedef void (*SET_CONFIG_CB)(void);
666/*--------------------------------------------------------------------*/
667void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
668void USBD_Start(void);
669void USBD_GetSetupPacket(uint8_t *buf);
670void USBD_ProcessSetupPacket(void);
671void USBD_StandardRequest(void);
672void USBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size);
673void USBD_CtrlIn(void);
674void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
675void USBD_CtrlOut(void);
676void USBD_SwReset(void);
677void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
678void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback);
679void USBD_LockEpStall(uint32_t u32EpBitmap);
680 /* end of group USBD_EXPORTED_FUNCTIONS */
682 /* end of group USBD_Driver */
684 /* end of group Standard_Driver */
686
687#ifdef __cplusplus
688}
689#endif
690
691#endif /*__USBD_H__*/
692
693/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
#define USBD
Definition: M480.h:407
#define USBD_MAX_EP
Definition: usbd.h:52
#define USBD_CFGP_SSTALL
Definition: usbd.h:172
void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface)
This function makes USBD module to be ready to use.
Definition: usbd.c:72
void USBD_LockEpStall(uint32_t u32EpBitmap)
EP stall lock function to avoid stall clear by USB SET FEATURE request.
Definition: usbd.c:729
void USBD_StandardRequest(void)
Process standard request.
Definition: usbd.c:309
uint32_t * gu32ConfigHidDescIdx
Definition: usbd.h:37
void(* VENDOR_REQ)(void)
Definition: usbd.h:660
__STATIC_INLINE uint32_t USBD_GetStall(uint8_t epnum)
Get USB endpoint stall state.
Definition: usbd.h:635
volatile uint8_t g_usbd_RemoteWakeupEn
Definition: usbd.c:33
void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size)
Prepare the first Control OUT pipe.
Definition: usbd.c:611
uint8_t ** gu8StringDesc
Definition: usbd.h:33
uint8_t ** gu8HidReportDesc
Definition: usbd.h:34
uint32_t * gu32HidReportSize
Definition: usbd.h:36
void USBD_GetSetupPacket(uint8_t *buf)
Get the received SETUP packet.
Definition: usbd.c:119
void(* CLASS_REQ)(void)
Definition: usbd.h:661
void USBD_Start(void)
This function makes USB host to recognize the device.
Definition: usbd.c:96
void USBD_SwReset(void)
Reset software flags.
Definition: usbd.c:667
__STATIC_INLINE void USBD_SetStall(uint8_t epnum)
Set USB endpoint stall state.
Definition: usbd.h:571
uint8_t * gu8ConfigDesc
Definition: usbd.h:32
void USBD_CtrlIn(void)
Repeat Control IN pipe.
Definition: usbd.c:553
void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback)
The callback function which called when get SET CONFIGURATION request.
Definition: usbd.c:713
__STATIC_INLINE void USBD_MemCopy(uint8_t dest[], uint8_t src[], uint32_t size)
To support byte access between USB SRAM and system SRAM.
Definition: usbd.h:550
void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq)
USBD Set Vendor Request.
Definition: usbd.c:699
uint8_t * gu8DevDesc
Definition: usbd.h:31
void USBD_ProcessSetupPacket(void)
Process SETUP packet.
Definition: usbd.c:134
uint8_t * gu8BosDesc
Definition: usbd.h:35
void(* SET_CONFIG_CB)(void)
Definition: usbd.h:663
void(* SET_INTERFACE_REQ)(uint32_t u32AltInterface)
Definition: usbd.h:662
void USBD_CtrlOut(void)
Repeat Control OUT pipe.
Definition: usbd.c:629
void USBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size)
Prepare the first Control IN pipe.
Definition: usbd.c:518
__STATIC_INLINE void USBD_ClearStall(uint8_t epnum)
Clear USB endpoint stall state.
Definition: usbd.h:602
struct s_usbd_info S_USBD_INFO_T
const S_USBD_INFO_T gsInfo