M4521_BSP V3.02.000
The Board Support Package for M4521
usbd.h
Go to the documentation of this file.
1/**************************************************************************/
11#ifndef __USBD_H__
12#define __USBD_H__
13
14
26typedef struct s_usbd_info
27{
28 const uint8_t *gu8DevDesc;
29 const uint8_t *gu8ConfigDesc;
30 const uint8_t **gu8StringDesc;
31 const uint8_t **gu8HidReportDesc;
32 const uint32_t *gu32HidReportSize;
33 const uint32_t *gu32ConfigHidDescIdx;
36
37extern const S_USBD_INFO_T gsInfo;
38 /* end of group USBD_EXPORTED_STRUCTS */
40
41
42
43
47#define USBD_BUF_BASE (USBD_BASE+0x100)
48#define USBD_MAX_EP 8
49
50#define EP0 0
51#define EP1 1
52#define EP2 2
53#define EP3 3
54#define EP4 4
55#define EP5 5
56#define EP6 6
57#define EP7 7
61#define REQ_STANDARD 0x00
62#define REQ_CLASS 0x20
63#define REQ_VENDOR 0x40
64
66#define GET_STATUS 0x00
67#define CLEAR_FEATURE 0x01
68#define SET_FEATURE 0x03
69#define SET_ADDRESS 0x05
70#define GET_DESCRIPTOR 0x06
71#define SET_DESCRIPTOR 0x07
72#define GET_CONFIGURATION 0x08
73#define SET_CONFIGURATION 0x09
74#define GET_INTERFACE 0x0A
75#define SET_INTERFACE 0x0B
76#define SYNC_FRAME 0x0C
77
79#define DESC_DEVICE 0x01
80#define DESC_CONFIG 0x02
81#define DESC_STRING 0x03
82#define DESC_INTERFACE 0x04
83#define DESC_ENDPOINT 0x05
84#define DESC_QUALIFIER 0x06
85#define DESC_OTHERSPEED 0x07
86
88#define DESC_HID 0x21
89#define DESC_HID_RPT 0x22
90
92#define LEN_DEVICE 18
93#define LEN_CONFIG 9
94#define LEN_INTERFACE 9
95#define LEN_ENDPOINT 7
96#define LEN_HID 9
97#define LEN_CCID 0x36
98
100#define EP_ISO 0x01
101#define EP_BULK 0x02
102#define EP_INT 0x03
103
104#define EP_INPUT 0x80
105#define EP_OUTPUT 0x00
106
108#define FEATURE_DEVICE_REMOTE_WAKEUP 0x01
109#define FEATURE_ENDPOINT_HALT 0x00
110
111/******************************************************************************/
112/* USB Specific Macros */
113/******************************************************************************/
114
115#define USBD_WAKEUP_EN USBD_INTEN_WKEN_Msk
116#define USBD_DRVSE0 USBD_SE0_SE0_Msk
118#define USBD_DPPU_EN USBD_ATTR_DPPUEN_Msk
119#define USBD_PWRDN USBD_ATTR_PWRDN_Msk
120#define USBD_PHY_EN USBD_ATTR_PHYEN_Msk
121#define USBD_USB_EN USBD_ATTR_USBEN_Msk
123#define USBD_INT_BUS USBD_INTEN_BUSIEN_Msk
124#define USBD_INT_USB USBD_INTEN_USBIEN_Msk
125#define USBD_INT_FLDET USBD_INTEN_VBDETIEN_Msk
126#define USBD_INT_WAKEUP (USBD_INTEN_NEVWKIEN_Msk | USBD_INTEN_WKEN_Msk)
128#define USBD_INTSTS_WAKEUP USBD_INTSTS_NEVWKIF_Msk
129#define USBD_INTSTS_FLDET USBD_INTSTS_VBDETIF_Msk
130#define USBD_INTSTS_BUS USBD_INTSTS_BUSIF_Msk
131#define USBD_INTSTS_USB USBD_INTSTS_USBIF_Msk
132#define USBD_INTSTS_SETUP USBD_INTSTS_SETUP_Msk
133#define USBD_INTSTS_EP0 USBD_INTSTS_EPEVT0_Msk
134#define USBD_INTSTS_EP1 USBD_INTSTS_EPEVT1_Msk
135#define USBD_INTSTS_EP2 USBD_INTSTS_EPEVT2_Msk
136#define USBD_INTSTS_EP3 USBD_INTSTS_EPEVT3_Msk
137#define USBD_INTSTS_EP4 USBD_INTSTS_EPEVT4_Msk
138#define USBD_INTSTS_EP5 USBD_INTSTS_EPEVT5_Msk
139#define USBD_INTSTS_EP6 USBD_INTSTS_EPEVT6_Msk
140#define USBD_INTSTS_EP7 USBD_INTSTS_EPEVT7_Msk
142#define USBD_STATE_USBRST USBD_ATTR_USBRST_Msk
143#define USBD_STATE_SUSPEND USBD_ATTR_SUSPEND_Msk
144#define USBD_STATE_RESUME USBD_ATTR_RESUME_Msk
145#define USBD_STATE_TIMEOUT USBD_ATTR_TOUT_Msk
147#define USBD_CFGP_SSTALL USBD_CFGP_SSTALL_Msk
148#define USBD_CFG_CSTALL USBD_CFG_CSTALL_Msk
150#define USBD_CFG_EPMODE_DISABLE (0ul << USBD_CFG_STATE_Pos)
151#define USBD_CFG_EPMODE_OUT (1ul << USBD_CFG_STATE_Pos)
152#define USBD_CFG_EPMODE_IN (2ul << USBD_CFG_STATE_Pos)
153#define USBD_CFG_TYPE_ISO (1ul << USBD_CFG_ISOCH_Pos) /* end of group USBD_EXPORTED_CONSTANTS */
158
159
174#define Maximum(a,b) ((a)>(b) ? (a) : (b))
175
176
187#define Minimum(a,b) ((a)<(b) ? (a) : (b))
188
189
200#define USBD_ENABLE_USB() ((uint32_t)(USBD->ATTR |= (USBD_USB_EN|USBD_PHY_EN)))
201
212#define USBD_DISABLE_USB() ((uint32_t)(USBD->ATTR &= ~USBD_USB_EN))
213
224#define USBD_ENABLE_PHY() ((uint32_t)(USBD->ATTR |= USBD_PHY_EN))
225
236#define USBD_DISABLE_PHY() ((uint32_t)(USBD->ATTR &= ~USBD_PHY_EN))
237
248#define USBD_SET_SE0() ((uint32_t)(USBD->SE0 |= USBD_DRVSE0))
249
260#define USBD_CLR_SE0() ((uint32_t)(USBD->SE0 &= ~USBD_DRVSE0))
261
272#define USBD_SET_ADDR(addr) (USBD->FADDR = (addr))
273
284#define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR))
285
299#define USBD_ENABLE_INT(intr) (USBD->INTEN |= (intr))
300
311#define USBD_GET_INT_FLAG() ((uint32_t)(USBD->INTSTS))
312
326#define USBD_CLR_INT_FLAG(flag) (USBD->INTSTS = (flag))
327
338#define USBD_GET_EP_FLAG() ((uint32_t)(USBD->EPSTS))
339
354#define USBD_GET_BUS_STATE() ((uint32_t)(USBD->ATTR & 0xf))
355
367#define USBD_IS_ATTACHED() ((uint32_t)(USBD->VBUSDET & USBD_VBUSDET_VBUSDET_Msk))
368
379#define USBD_STOP_TRANSACTION(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_CLRRDY_Msk)
380
392#define USBD_SET_DATA1(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DSQSYNC_Msk)
393
405#define USBD_SET_DATA0(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DSQSYNC_Msk))
406
419#define USBD_SET_PAYLOAD_LEN(ep, size) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))) = (size))
420
431#define USBD_GET_PAYLOAD_LEN(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))))
432
445#define USBD_CONFIG_EP(ep, config) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) = (config))
446
459#define USBD_SET_EP_BUF_ADDR(ep, offset) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))) = (offset))
460
471#define USBD_GET_EP_BUF_ADDR(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))))
472
483#define USBD_SET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_SSTALL_Msk)
484
494#define USBD_CLR_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) &= ~USBD_CFGP_SSTALL_Msk)
495
507#define USBD_GET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) & USBD_CFGP_SSTALL_Msk)
508
523static __INLINE void USBD_MemCopy(uint8_t *dest, uint8_t *src, int32_t size)
524{
525 while(size--) *dest++ = *src++;
526}
527
528
539static __INLINE void USBD_SetStall(uint8_t epnum)
540{
541 uint32_t u32CfgAddr;
542 uint32_t u32Cfg;
543 int i;
544
545 for(i = 0; i < USBD_MAX_EP; i++)
546 {
547 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
548 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
549
550 if((u32Cfg & 0xf) == epnum)
551 {
552 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
553 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
554
555 *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg | USBD_CFGP_SSTALL);
556 break;
557 }
558 }
559}
560
570static __INLINE void USBD_ClearStall(uint8_t epnum)
571{
572 uint32_t u32CfgAddr;
573 uint32_t u32Cfg;
574 int i;
575
576 for(i = 0; i < USBD_MAX_EP; i++)
577 {
578 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
579 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
580
581 if((u32Cfg & 0xf) == epnum)
582 {
583 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
584 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
585
586 *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg & ~USBD_CFGP_SSTALL);
587 break;
588 }
589 }
590}
591
603static __INLINE uint32_t USBD_GetStall(uint8_t epnum)
604{
605 uint32_t u32CfgAddr;
606 uint32_t u32Cfg;
607 int i;
608
609 for(i = 0; i < USBD_MAX_EP; i++)
610 {
611 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
612 u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
613
614 if((u32Cfg & 0xf) == epnum)
615 {
616 u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
617 break;
618 }
619 }
620
621 return ((*((__IO uint32_t *)(u32CfgAddr))) & USBD_CFGP_SSTALL);
622}
623
624
625extern volatile uint8_t g_usbd_RemoteWakeupEn;
626
627
628typedef void (*VENDOR_REQ)(void);
629typedef void (*CLASS_REQ)(void);
630typedef void (*SET_INTERFACE_REQ)(void);
631typedef void (*SET_CONFIG_CB)(void);
634/*--------------------------------------------------------------------*/
635void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
636void USBD_Start(void);
637void USBD_GetSetupPacket(uint8_t *buf);
638void USBD_ProcessSetupPacket(void);
639void USBD_StandardRequest(void);
640void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size);
641void USBD_CtrlIn(void);
642void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
643void USBD_CtrlOut(void);
644void USBD_SwReset(void);
645void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
646void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback);
647void USBD_LockEpStall(uint32_t u32EpBitmap);
648 /* end of group USBD_EXPORTED_FUNCTIONS */
650 /* end of group USBD_Driver */
652 /* end of group Standard_Driver */
654
655
656#endif //__USBD_H__
657
#define USBD
Definition: M4521.h:13841
#define USBD_MAX_EP
Definition: usbd.h:48
#define USBD_CFGP_SSTALL
Definition: usbd.h:147
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:81
void USBD_LockEpStall(uint32_t u32EpBitmap)
EP stall lock function to avoid stall clear by USB SET FEATURE request.
Definition: usbd.c:732
void USBD_StandardRequest(void)
Process standard request.
Definition: usbd.c:313
const uint32_t * gu32ConfigHidDescIdx
Definition: usbd.h:33
void(* VENDOR_REQ)(void)
Definition: usbd.h:628
volatile uint8_t g_usbd_RemoteWakeupEn
Definition: usbd.c:42
void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size)
Prepare the first Control OUT pipe.
Definition: usbd.c:618
static __INLINE void USBD_SetStall(uint8_t epnum)
Set USB endpoint stall state.
Definition: usbd.h:539
static __INLINE uint32_t USBD_GetStall(uint8_t epnum)
Get USB endpoint stall state.
Definition: usbd.h:603
void USBD_GetSetupPacket(uint8_t *buf)
Get the received SETUP packet.
Definition: usbd.c:128
void(* CLASS_REQ)(void)
Definition: usbd.h:629
static __INLINE void USBD_ClearStall(uint8_t epnum)
Clear USB endpoint stall state.
Definition: usbd.h:570
void USBD_Start(void)
This function makes USB host to recognize the device.
Definition: usbd.c:105
void USBD_SwReset(void)
Reset software flags.
Definition: usbd.c:672
void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size)
Prepare the first Control IN pipe.
Definition: usbd.c:528
void USBD_CtrlIn(void)
Repeat Control IN pipe.
Definition: usbd.c:561
void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback)
The callback function which called when get SET CONFIGURATION request.
Definition: usbd.c:716
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:523
void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq)
USBD Set Vendor Request.
Definition: usbd.c:702
void USBD_ProcessSetupPacket(void)
Process SETUP packet.
Definition: usbd.c:143
void(* SET_INTERFACE_REQ)(void)
Definition: usbd.h:630
const uint8_t * gu8ConfigDesc
Definition: usbd.h:29
const uint8_t * gu8DevDesc
Definition: usbd.h:28
const uint32_t * gu32HidReportSize
Definition: usbd.h:32
void(* SET_CONFIG_CB)(void)
Definition: usbd.h:631
const uint8_t ** gu8HidReportDesc
Definition: usbd.h:31
void USBD_CtrlOut(void)
Repeat Control OUT pipe.
Definition: usbd.c:636
const uint8_t ** gu8StringDesc
Definition: usbd.h:30
struct s_usbd_info S_USBD_INFO_T
const S_USBD_INFO_T gsInfo