Nano100AN Series BSP  V3.02.002
The Board Support Package for Nano100AN Series
usbd.h
Go to the documentation of this file.
1 /**************************************************************************/
11 #ifndef __USBD_H__
12 #define __USBD_H__
13 
14 
26 typedef struct s_usbd_info
27 {
28  uint8_t *gu8DevDesc;
29  uint8_t *gu8ConfigDesc;
30  uint8_t **gu8StringDesc;
31  uint8_t **gu8HidReportDesc;
32  uint32_t *gu32HidReportSize;
35 
37 extern S_USBD_INFO_T gsInfo;
39  /* end of group NANO100_USBD_EXPORTED_STRUCTS */
41 
45 #define USBD_BUF_BASE (USBD_BASE+0x100)
46 
47 #define USBD_MAX_EP 6
48 
49 #define EP0 0
50 #define EP1 1
51 #define EP2 2
52 #define EP3 3
53 #define EP4 4
54 #define EP5 5
56 extern volatile uint32_t g_usbd_UsbConfig;
58 
60 #define REQ_STANDARD 0x00
61 #define REQ_CLASS 0x20
62 #define REQ_VENDOR 0x40
63 
65 #define GET_STATUS 0x00
66 #define CLEAR_FEATURE 0x01
67 #define SET_FEATURE 0x03
68 #define SET_ADDRESS 0x05
69 #define GET_DESCRIPTOR 0x06
70 #define SET_DESCRIPTOR 0x07
71 #define GET_CONFIGURATION 0x08
72 #define SET_CONFIGURATION 0x09
73 #define GET_INTERFACE 0x0A
74 #define SET_INTERFACE 0x0B
75 #define SYNC_FRAME 0x0C
76 
78 #define DESC_DEVICE 0x01
79 #define DESC_CONFIG 0x02
80 #define DESC_STRING 0x03
81 #define DESC_INTERFACE 0x04
82 #define DESC_ENDPOINT 0x05
83 #define DESC_QUALIFIER 0x06
84 #define DESC_OTHERSPEED 0x07
85 
87 #define DESC_HID 0x21
88 #define DESC_HID_RPT 0x22
89 
91 #define LEN_DEVICE 18
92 #define LEN_CONFIG 9
93 #define LEN_INTERFACE 9
94 #define LEN_ENDPOINT 7
95 #define LEN_HID 9
96 #define LEN_CCID 0x36
97 
99 #define EP_ISO 0x01
100 #define EP_BULK 0x02
101 #define EP_INT 0x03
102 
103 #define EP_INPUT 0x80
104 #define EP_OUTPUT 0x00
105 
107 #define FEATURE_DEVICE_REMOTE_WAKEUP 0x01
108 #define FEATURE_ENDPOINT_HALT 0x00
109 
111 
112 #define USBD_WAKEUP_EN USBD_CTL_WAKEUP_EN_Msk
113 #define USBD_DRVSE0 USBD_CTL_DRVSE0_Msk
115 #define USBD_DPPU_EN USBD_CTL_DPPU_EN_Msk
116 #define USBD_PWRDN USBD_CTL_PWRDB_Msk
117 #define USBD_PHY_EN USBD_CTL_PHY_EN_Msk
118 #define USBD_USB_EN USBD_CTL_USB_EN_Msk
120 #define USBD_INT_BUS USBD_INTEN_BUSEVT_IE_Msk
121 #define USBD_INT_USB USBD_INTEN_USBEVT_IE_Msk
122 #define USBD_INT_FLDET USBD_INTEN_FLDET_IE_Msk
123 #define USBD_INT_WAKEUP USBD_INTEN_WAKEUP_IE_Msk
125 #define USBD_INTSTS_WAKEUP USBD_INTSTS_WKEUP_STS_Msk
126 #define USBD_INTSTS_FLDET USBD_INTSTS_FLD_STS_Msk
127 #define USBD_INTSTS_BUS USBD_INTSTS_BUS_STS_Msk
128 #define USBD_INTSTS_USB USBD_INTSTS_USB_STS_Msk
129 #define USBD_INTSTS_SETUP USBD_INTSTS_SETUP_Msk
130 #define USBD_INTSTS_EP0 USBD_INTSTS_EPEVT0_Msk
131 #define USBD_INTSTS_EP1 USBD_INTSTS_EPEVT1_Msk
132 #define USBD_INTSTS_EP2 USBD_INTSTS_EPEVT2_Msk
133 #define USBD_INTSTS_EP3 USBD_INTSTS_EPEVT3_Msk
134 #define USBD_INTSTS_EP4 USBD_INTSTS_EPEVT4_Msk
135 #define USBD_INTSTS_EP5 USBD_INTSTS_EPEVT5_Msk
137 #define USBD_STATE_USBRST USBD_BUSSTS_USBRST_Msk
138 #define USBD_STATE_SUSPEND USBD_BUSSTS_SUSPEND_Msk
139 #define USBD_STATE_RESUME USBD_BUSSTS_RESUME_Msk
140 #define USBD_STATE_TIMEOUT USBD_BUSSTS_TIMEOUT_Msk
142 #define USBD_CFG_SSTALL USBD_CFG_SSTALL_Msk
143 #define USBD_CFG_CSTALL USBD_CFG_CSTALL_Msk
145 #define USBD_CFG_EPMODE_DISABLE (0ul << USBD_CFG_EPMODE_Pos)
146 #define USBD_CFG_EPMODE_OUT (1ul << USBD_CFG_EPMODE_Pos)
147 #define USBD_CFG_EPMODE_IN (2ul << USBD_CFG_EPMODE_Pos)
148 #define USBD_CFG_TYPE_ISO (1ul << USBD_CFG_ISOCH_Pos)
151  /* end of group NANO100_USBD_EXPORTED_CONSTANTS */
152 
153 
168 #define Maximum(a,b) ((a)>(b) ? (a) : (b))
169 
170 
181 #define Minimum(a,b) ((a)<(b) ? (a) : (b))
182 
188 #define USBD_ENABLE_USB() ((uint32_t)(USBD->CTL |= 0xF))
189 
195 #define USBD_DISABLE_USB() ((uint32_t)(USBD->CTL &= ~USBD_USB_EN))
196 
202 #define USBD_ENABLE_PHY() ((uint32_t)(USBD->CTL |= USBD_PHY_EN))
203 
209 #define USBD_DISABLE_PHY() ((uint32_t)(USBD->CTL &= ~USBD_PHY_EN))
210 
216 #define USBD_SET_SE0() ((uint32_t)(USBD->CTL |= USBD_DRVSE0))
217 
223 #define USBD_CLR_SE0() ((uint32_t)(USBD->CTL &= ~USBD_DRVSE0))
224 
230 #define USBD_SET_ADDR(addr) (USBD->FADDR = (addr))
231 
237 #define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR))
238 
244 #define USBD_ENABLE_INT(intr) (USBD->INTEN |= (intr))
245 
251 #define USBD_GET_INT_FLAG() ((uint32_t)(USBD->INTSTS))
252 
258 #define USBD_CLR_INT_FLAG(flag) (USBD->INTSTS = flag)
259 
265 #define USBD_GET_EP_FLAG() ((uint32_t)(USBD->EPSTS))
266 
272 #define USBD_GET_BUS_STATE() ((uint32_t)(USBD->BUSSTS & 0xf))
273 
279 #define USBD_IS_ATTACHED() ((uint32_t)(USBD->BUSSTS & USBD_BUSSTS_FLDET_Msk))
280 
286 #define USBD_STOP_TRANSACTION(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_CLRRDY_Msk)
287 
293 #define USBD_SET_DATA1(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DSQ_SYNC_Msk)
294 
300 #define USBD_SET_DATA0(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DSQ_SYNC_Msk))
301 
308 #define USBD_SET_PAYLOAD_LEN(ep, size) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))) = (size))
309 
315 #define USBD_GET_PAYLOAD_LEN(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))))
316 
323 #define USBD_CONFIG_EP(ep, config) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) = (config))
324 
331 #define USBD_SET_EP_BUF_ADDR(ep, offset) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))) = (offset))
332 
338 #define USBD_GET_EP_BUF_ADDR(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))))
339 
350 #define USBD_SET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_SSTALL_Msk)
351 
361 #define USBD_CLR_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= ~USBD_CFG_SSTALL_Msk)
362 
374 #define USBD_GET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) & USBD_CFG_SSTALL_Msk)
375 
390 static __INLINE void USBD_MemCopy(uint8_t *dest, uint8_t *src, int32_t size)
391 {
392  while (size--) *dest++ = *src++;
393 }
394 
395 
404 static __INLINE void USBD_SetStall(uint8_t epnum)
405 {
406  uint32_t u32CfgAddr;
407  uint32_t u32Cfg;
408  int i;
409 
410  for (i = 0; i < USBD_MAX_EP; i++)
411  {
412  u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
413  u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
414 
415  if ((u32Cfg & 0xf) == epnum)
416  {
417  *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg | USBD_CFG_SSTALL);
418  break;
419  }
420  }
421 }
422 
431 static __INLINE void USBD_ClearStall(uint8_t epnum)
432 {
433  uint32_t u32CfgAddr;
434  uint32_t u32Cfg;
435  int i;
436 
437  for (i = 0; i < USBD_MAX_EP; i++)
438  {
439  u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
440  u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
441 
442  if ((u32Cfg & 0xf) == epnum)
443  {
444  *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg & ~USBD_CFG_SSTALL);
445  break;
446  }
447  }
448 }
449 
459 static __INLINE uint32_t USBD_GetStall(uint8_t epnum)
460 {
461  uint32_t u32CfgAddr;
462  uint32_t u32Cfg;
463  int i;
464 
465  for (i = 0; i < USBD_MAX_EP; i++)
466  {
467  u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
468  u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
469 
470  if ((u32Cfg & 0xf) == epnum)
471  break;
472  }
473 
474  return (u32Cfg & USBD_CFG_SSTALL);
475 }
476 
477 
478 /*--------------------------------------------------------------------*/
479 extern volatile uint8_t g_usbd_RemoteWakeupEn;
480 
481 typedef void (*VENDOR_REQ)(void);
482 typedef void (*CLASS_REQ)(void);
483 typedef void (*SET_INTERFACE_REQ)(uint32_t u32AltInterface);
484 typedef void (*SET_CONFIG_CB)(void);
486 /*--------------------------------------------------------------------*/
487 void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
488 void USBD_Start(void);
489 void USBD_GetSetupPacket(uint8_t *buf);
490 void USBD_ProcessSetupPacket(void);
491 void USBD_StandardRequest(void);
492 void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size);
493 void USBD_CtrlIn(void);
494 void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
495 void USBD_CtrlOut(void);
496 void USBD_SwReset(void);
497 void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
498 void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback);
499 void USBD_LockEpStall(uint32_t u32EpBitmap);
500 
501  /* end of group NANO100_USBD_EXPORTED_FUNCTIONS */
503  /* end of group NANO100_USBD_Driver */
505  /* end of group NANO100_Device_Driver */
507 
508 
509 #endif //__USBD_H__
510 
511 /*** (C) COPYRIGHT 2013~2014 Nuvoton Technology Corp. ***/
void USBD_StandardRequest(void)
Process USB standard request.
Definition: usbd.c:284
void(* SET_INTERFACE_REQ)(uint32_t u32AltInterface)
Definition: usbd.h:483
#define USBD
Pointer to USBD register structure.
void USBD_Start(void)
USBD Start.
Definition: usbd.c:79
#define USBD_CFG_SSTALL
Definition: usbd.h:142
static __INLINE void USBD_ClearStall(uint8_t epnum)
Clear USB endpoint stall state.
Definition: usbd.h:431
uint8_t * gu8DevDesc
Definition: usbd.h:28
void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size)
Prepare Control IN transaction.
Definition: usbd.c:480
void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback)
The callback function which called when get SET CONFIGURATION request.
Definition: usbd.c:654
void USBD_Open(S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface)
USBD Initial, Enable clock and reset USB.
Definition: usbd.c:54
void(* CLASS_REQ)(void)
Definition: usbd.h:482
volatile uint8_t g_usbd_RemoteWakeupEn
Definition: usbd.c:19
void USBD_SwReset(void)
Clear all software flags.
Definition: usbd.c:611
uint32_t * gu32HidReportSize
Definition: usbd.h:32
uint8_t ** gu8HidReportDesc
Definition: usbd.h:31
uint8_t * gu8ConfigDesc
Definition: usbd.h:29
static __INLINE void USBD_SetStall(uint8_t epnum)
Set USB endpoint stall state.
Definition: usbd.h:404
void USBD_GetSetupPacket(uint8_t *buf)
Get Setup Packet.
Definition: usbd.c:96
void USBD_CtrlOut(void)
Start Control OUT transfer.
Definition: usbd.c:580
#define USBD_MAX_EP
Definition: usbd.h:47
static __INLINE uint32_t USBD_GetStall(uint8_t epnum)
Get USB endpoint stall state.
Definition: usbd.h:459
struct s_usbd_info S_USBD_INFO_T
void USBD_LockEpStall(uint32_t u32EpBitmap)
Definition: usbd.c:659
void USBD_CtrlIn(void)
Start Control IN transfer.
Definition: usbd.c:511
void USBD_ProcessSetupPacket(void)
Process Setup Packet.
Definition: usbd.c:110
void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq)
USBD Set Vendor Request.
Definition: usbd.c:641
void(* SET_CONFIG_CB)(void)
Definition: usbd.h:484
void(* VENDOR_REQ)(void)
Definition: usbd.h:481
uint8_t ** gu8StringDesc
Definition: usbd.h:30
uint32_t * gu32ConfigHidDescIdx
Definition: usbd.h:33
void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size)
Prepare Control OUT transaction.
Definition: usbd.c:563
static __INLINE void USBD_MemCopy(uint8_t *dest, uint8_t *src, int32_t size)
To support byte access between USB SRAM and system SRAM.
Definition: usbd.h:390