M471M/R1/S BSP V3.01.000
The Board Support Package for M4521
usbd.h
Go to the documentation of this file.
1/**************************************************************************/
8#ifndef __USBD_H__
9#define __USBD_H__
10
11
23typedef struct s_usbd_info
24{
25 const uint8_t *gu8DevDesc;
26 const uint8_t *gu8ConfigDesc;
27 const uint8_t **gu8StringDesc;
28 const uint8_t **gu8HidReportDesc;
29 const uint32_t *gu32HidReportSize;
30 const uint32_t *gu32ConfigHidDescIdx;
33
34extern const S_USBD_INFO_T gsInfo;
35 /* end of group USBD_EXPORTED_STRUCTS */
37
38
39
40
44#define USBD_BUF_BASE (USBD_BASE+0x100)
45#define USBD_MAX_EP 8
46
47#define EP0 0
48#define EP1 1
49#define EP2 2
50#define EP3 3
51#define EP4 4
52#define EP5 5
53#define EP6 6
54#define EP7 7
58#define REQ_STANDARD 0x00
59#define REQ_CLASS 0x20
60#define REQ_VENDOR 0x40
61
63#define GET_STATUS 0x00
64#define CLEAR_FEATURE 0x01
65#define SET_FEATURE 0x03
66#define SET_ADDRESS 0x05
67#define GET_DESCRIPTOR 0x06
68#define SET_DESCRIPTOR 0x07
69#define GET_CONFIGURATION 0x08
70#define SET_CONFIGURATION 0x09
71#define GET_INTERFACE 0x0A
72#define SET_INTERFACE 0x0B
73#define SYNC_FRAME 0x0C
74
76#define DESC_DEVICE 0x01
77#define DESC_CONFIG 0x02
78#define DESC_STRING 0x03
79#define DESC_INTERFACE 0x04
80#define DESC_ENDPOINT 0x05
81#define DESC_QUALIFIER 0x06
82#define DESC_OTHERSPEED 0x07
83
85#define DESC_HID 0x21
86#define DESC_HID_RPT 0x22
87
89#define LEN_DEVICE 18
90#define LEN_CONFIG 9
91#define LEN_INTERFACE 9
92#define LEN_ENDPOINT 7
93#define LEN_HID 9
94#define LEN_CCID 0x36
95
97#define EP_ISO 0x01
98#define EP_BULK 0x02
99#define EP_INT 0x03
100
101#define EP_INPUT 0x80
102#define EP_OUTPUT 0x00
103
105#define FEATURE_DEVICE_REMOTE_WAKEUP 0x01
106#define FEATURE_ENDPOINT_HALT 0x00
107
108/******************************************************************************/
109/* USB Specific Macros */
110/******************************************************************************/
111
112#define USBD_WAKEUP_EN USBD_INTEN_WKEN_Msk
113#define USBD_DRVSE0 USBD_SE0_SE0_Msk
115#define USBD_DPPU_EN USBD_ATTR_DPPUEN_Msk
116#define USBD_PWRDN USBD_ATTR_PWRDN_Msk
117#define USBD_PHY_EN USBD_ATTR_PHYEN_Msk
118#define USBD_USB_EN USBD_ATTR_USBEN_Msk
120#define USBD_INT_BUS USBD_INTEN_BUSIEN_Msk
121#define USBD_INT_USB USBD_INTEN_USBIEN_Msk
122#define USBD_INT_FLDET USBD_INTEN_VBDETIEN_Msk
123#define USBD_INT_WAKEUP (USBD_INTEN_NEVWKIEN_Msk | USBD_INTEN_WKEN_Msk)
125#define USBD_INTSTS_WAKEUP USBD_INTSTS_NEVWKIF_Msk
126#define USBD_INTSTS_FLDET USBD_INTSTS_VBDETIF_Msk
127#define USBD_INTSTS_BUS USBD_INTSTS_BUSIF_Msk
128#define USBD_INTSTS_USB USBD_INTSTS_USBIF_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
136#define USBD_INTSTS_EP6 USBD_INTSTS_EPEVT6_Msk
137#define USBD_INTSTS_EP7 USBD_INTSTS_EPEVT7_Msk
139#define USBD_STATE_USBRST USBD_ATTR_USBRST_Msk
140#define USBD_STATE_SUSPEND USBD_ATTR_SUSPEND_Msk
141#define USBD_STATE_RESUME USBD_ATTR_RESUME_Msk
142#define USBD_STATE_TIMEOUT USBD_ATTR_TOUT_Msk
144#define USBD_CFGP_SSTALL USBD_CFGP_SSTALL_Msk
145#define USBD_CFG_CSTALL USBD_CFG_CSTALL_Msk
147#define USBD_CFG_EPMODE_DISABLE (0ul << USBD_CFG_STATE_Pos)
148#define USBD_CFG_EPMODE_OUT (1ul << USBD_CFG_STATE_Pos)
149#define USBD_CFG_EPMODE_IN (2ul << USBD_CFG_STATE_Pos)
150#define USBD_CFG_TYPE_ISO (1ul << USBD_CFG_ISOCH_Pos) /* end of group USBD_EXPORTED_CONSTANTS */
155
156
171#define Maximum(a,b) ((a)>(b) ? (a) : (b))
172
173
184#define Minimum(a,b) ((a)<(b) ? (a) : (b))
185
186
197#define USBD_ENABLE_USB() ((uint32_t)(USBD->ATTR |= (USBD_USB_EN|USBD_PHY_EN)))
198
209#define USBD_DISABLE_USB() ((uint32_t)(USBD->ATTR &= ~USBD_USB_EN))
210
221#define USBD_ENABLE_PHY() ((uint32_t)(USBD->ATTR |= USBD_PHY_EN))
222
233#define USBD_DISABLE_PHY() ((uint32_t)(USBD->ATTR &= ~USBD_PHY_EN))
234
245#define USBD_SET_SE0() ((uint32_t)(USBD->SE0 |= USBD_DRVSE0))
246
257#define USBD_CLR_SE0() ((uint32_t)(USBD->SE0 &= ~USBD_DRVSE0))
258
269#define USBD_SET_ADDR(addr) (USBD->FADDR = (addr))
270
281#define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR))
282
296#define USBD_ENABLE_INT(intr) (USBD->INTEN |= (intr))
297
308#define USBD_GET_INT_FLAG() ((uint32_t)(USBD->INTSTS))
309
323#define USBD_CLR_INT_FLAG(flag) (USBD->INTSTS = (flag))
324
335#define USBD_GET_EP_FLAG() ((uint32_t)(USBD->EPSTS))
336
351#define USBD_GET_BUS_STATE() ((uint32_t)(USBD->ATTR & 0xf))
352
364#define USBD_IS_ATTACHED() ((uint32_t)(USBD->VBUSDET & USBD_VBUSDET_VBUSDET_Msk))
365
376#define USBD_STOP_TRANSACTION(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_CLRRDY_Msk)
377
389#define USBD_SET_DATA1(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DSQSYNC_Msk)
390
402#define USBD_SET_DATA0(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DSQSYNC_Msk))
403
416#define USBD_SET_PAYLOAD_LEN(ep, size) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))) = (size))
417
428#define USBD_GET_PAYLOAD_LEN(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))))
429
442#define USBD_CONFIG_EP(ep, config) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) = (config))
443
456#define USBD_SET_EP_BUF_ADDR(ep, offset) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))) = (offset))
457
468#define USBD_GET_EP_BUF_ADDR(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))))
469
480#define USBD_SET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_SSTALL_Msk)
481
491#define USBD_CLR_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) &= ~USBD_CFGP_SSTALL_Msk)
492
504#define USBD_GET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) & USBD_CFGP_SSTALL_Msk)
505
520static __INLINE void USBD_MemCopy(uint8_t *dest, uint8_t *src, int32_t size)
521{
522 while(size--) *dest++ = *src++;
523}
524
525
536static __INLINE void USBD_SetStall(uint8_t epnum)
537{
538 uint32_t u32CfgAddr;
539 uint32_t u32Cfg;
540 int i;
541
542 for(i = 0; i < USBD_MAX_EP; i++)
543 {
544 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
545 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
546
547 if((u32Cfg & 0xf) == epnum)
548 {
549 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
550 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
551
552 *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg | USBD_CFGP_SSTALL);
553 break;
554 }
555 }
556}
557
567static __INLINE void USBD_ClearStall(uint8_t epnum)
568{
569 uint32_t u32CfgAddr;
570 uint32_t u32Cfg;
571 int i;
572
573 for(i = 0; i < USBD_MAX_EP; i++)
574 {
575 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
576 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
577
578 if((u32Cfg & 0xf) == epnum)
579 {
580 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
581 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
582
583 *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg & ~USBD_CFGP_SSTALL);
584 break;
585 }
586 }
587}
588
600static __INLINE uint32_t USBD_GetStall(uint8_t epnum)
601{
602 uint32_t u32CfgAddr;
603 uint32_t u32Cfg;
604 int i;
605
606 for(i = 0; i < USBD_MAX_EP; i++)
607 {
608 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
609 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
610
611 if((u32Cfg & 0xf) == epnum)
612 {
613 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
614 break;
615 }
616 }
617
618 return ((*((__IO uint32_t *)(u32CfgAddr))) & USBD_CFGP_SSTALL);
619}
620
621
622extern volatile uint8_t g_usbd_RemoteWakeupEn;
623
624
625typedef void (*VENDOR_REQ)(void);
626typedef void (*CLASS_REQ)(void);
627typedef void (*SET_INTERFACE_REQ)(void);
628typedef void (*SET_CONFIG_CB)(void);
631/*--------------------------------------------------------------------*/
632void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
633void USBD_Start(void);
634void USBD_GetSetupPacket(uint8_t *buf);
635void USBD_ProcessSetupPacket(void);
636void USBD_StandardRequest(void);
637void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size);
638void USBD_CtrlIn(void);
639void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
640void USBD_CtrlOut(void);
641void USBD_SwReset(void);
642void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
643void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback);
644void USBD_LockEpStall(uint32_t u32EpBitmap);
645 /* end of group USBD_EXPORTED_FUNCTIONS */
647 /* end of group USBD_Driver */
649 /* end of group Standard_Driver */
651
652
653#endif //__USBD_H__
654
#define USBD
Definition: M471M_R1_S.h:13837
#define USBD_MAX_EP
Definition: usbd.h:45
#define USBD_CFGP_SSTALL
Definition: usbd.h:144
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:78
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:310
const uint32_t * gu32ConfigHidDescIdx
Definition: usbd.h:30
void(* VENDOR_REQ)(void)
Definition: usbd.h:625
volatile uint8_t g_usbd_RemoteWakeupEn
Definition: usbd.c:39
void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size)
Prepare the first Control OUT pipe.
Definition: usbd.c:615
static __INLINE void USBD_SetStall(uint8_t epnum)
Set USB endpoint stall state.
Definition: usbd.h:536
static __INLINE uint32_t USBD_GetStall(uint8_t epnum)
Get USB endpoint stall state.
Definition: usbd.h:600
void USBD_GetSetupPacket(uint8_t *buf)
Get the received SETUP packet.
Definition: usbd.c:125
void(* CLASS_REQ)(void)
Definition: usbd.h:626
static __INLINE void USBD_ClearStall(uint8_t epnum)
Clear USB endpoint stall state.
Definition: usbd.h:567
void USBD_Start(void)
This function makes USB host to recognize the device.
Definition: usbd.c:102
void USBD_SwReset(void)
Reset software flags.
Definition: usbd.c:669
void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size)
Prepare the first Control IN pipe.
Definition: usbd.c:525
void USBD_CtrlIn(void)
Repeat Control IN pipe.
Definition: usbd.c:558
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, int32_t size)
To support byte access between USB SRAM and system SRAM.
Definition: usbd.h:520
void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq)
USBD Set Vendor Request.
Definition: usbd.c:699
void USBD_ProcessSetupPacket(void)
Process SETUP packet.
Definition: usbd.c:140
void(* SET_INTERFACE_REQ)(void)
Definition: usbd.h:627
const uint8_t * gu8ConfigDesc
Definition: usbd.h:26
const uint8_t * gu8DevDesc
Definition: usbd.h:25
const uint32_t * gu32HidReportSize
Definition: usbd.h:29
void(* SET_CONFIG_CB)(void)
Definition: usbd.h:628
const uint8_t ** gu8HidReportDesc
Definition: usbd.h:28
void USBD_CtrlOut(void)
Repeat Control OUT pipe.
Definition: usbd.c:633
const uint8_t ** gu8StringDesc
Definition: usbd.h:27
struct s_usbd_info S_USBD_INFO_T
const S_USBD_INFO_T gsInfo