M480 BSP V3.05.006
The Board Support Package for M480 Series
hsusbd.h
Go to the documentation of this file.
1/**************************************************************************/
9#ifndef __HSUSBD_H__
10#define __HSUSBD_H__
11
12#ifdef __cplusplus
13extern "C"
14{
15#endif
16
17
30#define HSUSBD_MAX_EP 12ul
31
32#define Maximum(a,b) (a)>(b) ? (a) : (b)
33#define Minimum(a,b) (((a)<(b)) ? (a) : (b))
34
35
36#define CEP 0xfful
37#define EPA 0ul
38#define EPB 1ul
39#define EPC 2ul
40#define EPD 3ul
41#define EPE 4ul
42#define EPF 5ul
43#define EPG 6ul
44#define EPH 7ul
45#define EPI 8ul
46#define EPJ 9ul
47#define EPK 10ul
48#define EPL 11ul
51/********************* Bit definition of CEPCTL register **********************/
52#define HSUSBD_CEPCTL_NAKCLR ((uint32_t)0x00000000ul)
53#define HSUSBD_CEPCTL_STALL ((uint32_t)0x00000002ul)
54#define HSUSBD_CEPCTL_ZEROLEN ((uint32_t)0x00000004ul)
55#define HSUSBD_CEPCTL_FLUSH ((uint32_t)0x00000008ul)
57/********************* Bit definition of EPxRSPCTL register **********************/
58#define HSUSBD_EP_RSPCTL_FLUSH ((uint32_t)0x00000001ul)
59#define HSUSBD_EP_RSPCTL_MODE_AUTO ((uint32_t)0x00000000ul)
60#define HSUSBD_EP_RSPCTL_MODE_MANUAL ((uint32_t)0x00000002ul)
61#define HSUSBD_EP_RSPCTL_MODE_FLY ((uint32_t)0x00000004ul)
62#define HSUSBD_EP_RSPCTL_MODE_MASK ((uint32_t)0x00000006ul)
63#define HSUSBD_EP_RSPCTL_TOGGLE ((uint32_t)0x00000008ul)
64#define HSUSBD_EP_RSPCTL_HALT ((uint32_t)0x00000010ul)
65#define HSUSBD_EP_RSPCTL_ZEROLEN ((uint32_t)0x00000020ul)
66#define HSUSBD_EP_RSPCTL_SHORTTXEN ((uint32_t)0x00000040ul)
67#define HSUSBD_EP_RSPCTL_DISBUF ((uint32_t)0x00000080ul)
69/********************* Bit definition of EPxCFG register **********************/
70#define HSUSBD_EP_CFG_VALID ((uint32_t)0x00000001ul)
71#define HSUSBD_EP_CFG_TYPE_BULK ((uint32_t)0x00000002ul)
72#define HSUSBD_EP_CFG_TYPE_INT ((uint32_t)0x00000004ul)
73#define HSUSBD_EP_CFG_TYPE_ISO ((uint32_t)0x00000006ul)
74#define HSUSBD_EP_CFG_TYPE_MASK ((uint32_t)0x00000006ul)
75#define HSUSBD_EP_CFG_DIR_OUT ((uint32_t)0x00000000ul)
76#define HSUSBD_EP_CFG_DIR_IN ((uint32_t)0x00000008ul) /* end of group HSUSBD_EXPORTED_CONSTANTS */
80
86typedef struct HSUSBD_CMD_STRUCT
87{
89 uint8_t bRequest;
90 uint16_t wValue;
91 uint16_t wIndex;
92 uint16_t wLength;
93
99typedef struct s_hsusbd_info
100{
101 uint8_t *gu8DevDesc;
102 uint8_t *gu8ConfigDesc;
103 uint8_t **gu8StringDesc;
104 uint8_t *gu8QualDesc;
112} S_HSUSBD_INFO_T; /* end of group HSUSBD_EXPORTED_STRUCT */
116
118extern uint32_t g_u32HsEpStallLock;
119extern uint8_t volatile g_hsusbd_Configured;
120extern uint8_t g_hsusbd_ShortPacket;
121extern uint8_t g_hsusbd_CtrlZero;
122extern uint8_t g_hsusbd_UsbAddr;
123extern uint32_t volatile g_hsusbd_DmaDone;
124extern uint32_t g_hsusbd_CtrlInSize;
125extern S_HSUSBD_INFO_T gsHSInfo;
126extern S_HSUSBD_CMD_T gUsbCmd;
133#define HSUSBD_ENABLE_USB() ((uint32_t)(HSUSBD->PHYCTL |= (HSUSBD_PHYCTL_PHYEN_Msk|HSUSBD_PHYCTL_DPPUEN_Msk)))
134#define HSUSBD_DISABLE_USB() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk))
135#define HSUSBD_ENABLE_PHY() ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_PHYEN_Msk))
136#define HSUSBD_DISABLE_PHY() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_PHYEN_Msk))
137#define HSUSBD_SET_SE0() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk))
138#define HSUSBD_CLR_SE0() ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_DPPUEN_Msk))
139#define HSUSBD_SET_ADDR(addr) (HSUSBD->FADDR = (addr))
140#define HSUSBD_GET_ADDR() ((uint32_t)(HSUSBD->FADDR))
141#define HSUSBD_ENABLE_USB_INT(intr) (HSUSBD->GINTEN = (intr))
142#define HSUSBD_ENABLE_BUS_INT(intr) (HSUSBD->BUSINTEN = (intr))
143#define HSUSBD_GET_BUS_INT_FLAG() (HSUSBD->BUSINTSTS)
144#define HSUSBD_CLR_BUS_INT_FLAG(flag) (HSUSBD->BUSINTSTS = (flag))
145#define HSUSBD_ENABLE_CEP_INT(intr) (HSUSBD->CEPINTEN = (intr))
146#define HSUSBD_CLR_CEP_INT_FLAG(flag) (HSUSBD->CEPINTSTS = (flag))
147#define HSUSBD_SET_CEP_STATE(flag) (HSUSBD->CEPCTL = (flag))
148#define HSUSBD_START_CEP_IN(size) (HSUSBD->CEPTXCNT = (size))
149#define HSUSBD_SET_MAX_PAYLOAD(ep, size) (HSUSBD->EP[(ep)].EPMPS = (size))
150#define HSUSBD_ENABLE_EP_INT(ep, intr) (HSUSBD->EP[(ep)].EPINTEN = (intr))
151#define HSUSBD_GET_EP_INT_FLAG(ep) (HSUSBD->EP[(ep)].EPINTSTS)
152#define HSUSBD_CLR_EP_INT_FLAG(ep, flag) (HSUSBD->EP[(ep)].EPINTSTS = (flag))
153#define HSUSBD_SET_DMA_LEN(len) (HSUSBD->DMACNT = (len))
154#define HSUSBD_SET_DMA_ADDR(addr) (HSUSBD->DMAADDR = (addr))
155#define HSUSBD_SET_DMA_READ(epnum) (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~HSUSBD_DMACTL_EPNUM_Msk) | HSUSBD_DMACTL_DMARD_Msk | (epnum) | 0x100)
156#define HSUSBD_SET_DMA_WRITE(epnum) (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~(HSUSBD_DMACTL_EPNUM_Msk | HSUSBD_DMACTL_DMARD_Msk | 0x100)) | (epnum))
157#define HSUSBD_ENABLE_DMA() (HSUSBD->DMACTL |= HSUSBD_DMACTL_DMAEN_Msk)
158#define HSUSBD_IS_ATTACHED() ((uint32_t)(HSUSBD->PHYCTL & HSUSBD_PHYCTL_VBUSDET_Msk))
167__STATIC_INLINE void HSUSBD_MemCopy(uint8_t u8Dst[], uint8_t u8Src[], uint32_t u32Size)
168{
169 uint32_t i = 0ul;
170
171 while (u32Size--)
172 {
173 u8Dst[i] = u8Src[i];
174 i++;
175 }
176}
177
183__STATIC_INLINE void HSUSBD_ResetDMA(void)
184{
185 HSUSBD->DMACNT = 0ul;
186 HSUSBD->DMACTL = 0x80ul;
187 HSUSBD->DMACTL = 0x00ul;
188}
196__STATIC_INLINE void HSUSBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
197{
198 if (u32Ep == CEP)
199 {
200 HSUSBD->CEPBUFST = u32Base;
201 HSUSBD->CEPBUFEND = u32Base + u32Len - 1ul;
202 }
203 else
204 {
205 HSUSBD->EP[u32Ep].EPBUFST = u32Base;
206 HSUSBD->EP[u32Ep].EPBUFEND = u32Base + u32Len - 1ul;
207 }
208}
209
218__STATIC_INLINE void HSUSBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
219{
220 if (u32EpType == HSUSBD_EP_CFG_TYPE_BULK)
221 {
223 }
224 else if (u32EpType == HSUSBD_EP_CFG_TYPE_INT)
225 {
227 }
228 else if (u32EpType == HSUSBD_EP_CFG_TYPE_ISO)
229 {
231 }
232
233 HSUSBD->EP[u32Ep].EPCFG = (u32EpType|u32EpDir|HSUSBD_EP_CFG_VALID|(u32EpNum << 4));
234}
235
242__STATIC_INLINE void HSUSBD_SetEpStall(uint32_t u32Ep)
243{
244 if (u32Ep == CEP)
245 {
247 }
248 else
249 {
250 HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD->EP[u32Ep].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT;
251 }
252}
253
262__STATIC_INLINE void HSUSBD_SetStall(uint32_t u32EpNum)
263{
264 uint32_t i;
265
266 if (u32EpNum == 0ul)
267 {
269 }
270 else
271 {
272 for (i=0ul; i<HSUSBD_MAX_EP; i++)
273 {
274 if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
275 {
276 HSUSBD->EP[i].EPRSPCTL = (HSUSBD->EP[i].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT;
277 }
278 }
279 }
280}
281
288__STATIC_INLINE void HSUSBD_ClearEpStall(uint32_t u32Ep)
289{
290 HSUSBD->EP[u32Ep].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE;
291}
292
301__STATIC_INLINE void HSUSBD_ClearStall(uint32_t u32EpNum)
302{
303 uint32_t i;
304
305 for (i=0ul; i<HSUSBD_MAX_EP; i++)
306 {
307 if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
308 {
309 HSUSBD->EP[i].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE;
310 }
311 }
312}
313
321__STATIC_INLINE uint32_t HSUSBD_GetEpStall(uint32_t u32Ep)
322{
323 return (HSUSBD->EP[u32Ep].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT);
324}
325
335__STATIC_INLINE uint32_t HSUSBD_GetStall(uint32_t u32EpNum)
336{
337 uint32_t i;
338 uint32_t val = 0ul;
339
340 for (i=0ul; i<HSUSBD_MAX_EP; i++)
341 {
342 if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
343 {
344 val = (HSUSBD->EP[i].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT);
345 break;
346 }
347 }
348 return val;
349}
350
351
352/*-------------------------------------------------------------------------------------------*/
353typedef void (*HSUSBD_VENDOR_REQ)(void);
354typedef void (*HSUSBD_CLASS_REQ)(void);
355typedef void (*HSUSBD_SET_INTERFACE_REQ)(uint32_t u32AltInterface);
357void HSUSBD_Open(S_HSUSBD_INFO_T *param, HSUSBD_CLASS_REQ pfnClassReq, HSUSBD_SET_INTERFACE_REQ pfnSetInterface);
358void HSUSBD_Start(void);
360void HSUSBD_StandardRequest(void);
361void HSUSBD_UpdateDeviceState(void);
362void HSUSBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size);
363void HSUSBD_CtrlIn(void);
364void HSUSBD_CtrlOut(uint8_t pu8Buf[], uint32_t u32Size);
365void HSUSBD_SwReset(void);
367
368
369 /* end of group HSUSBD_EXPORTED_FUNCTIONS */
371 /* end of group HSUSBD_Driver */
373 /* end of group Standard_Driver */
375
376#ifdef __cplusplus
377}
378#endif
379
380#endif /*__HSUSBD_H__ */
381
382/*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
#define HSUSBD_EP_CFG_TYPE_INT
Definition: hsusbd.h:72
#define HSUSBD_EP_RSPCTL_TOGGLE
Definition: hsusbd.h:63
#define HSUSBD_EP_RSPCTL_FLUSH
Definition: hsusbd.h:58
#define HSUSBD_EP_CFG_TYPE_ISO
Definition: hsusbd.h:73
#define HSUSBD_EP_RSPCTL_MODE_FLY
Definition: hsusbd.h:61
#define HSUSBD_EP_RSPCTL_MODE_AUTO
Definition: hsusbd.h:59
#define HSUSBD_EP_CFG_VALID
Definition: hsusbd.h:70
#define HSUSBD_EP_RSPCTL_HALT
Definition: hsusbd.h:64
#define HSUSBD_EP_CFG_TYPE_BULK
Definition: hsusbd.h:71
#define HSUSBD_CEPCTL_STALL
Definition: hsusbd.h:53
#define HSUSBD_EP_RSPCTL_MODE_MANUAL
Definition: hsusbd.h:60
__STATIC_INLINE void HSUSBD_ClearEpStall(uint32_t u32Ep)
Clear USB endpoint stall state.
Definition: hsusbd.h:288
uint8_t * gu8FSOtherConfigDesc
Definition: hsusbd.h:107
uint8_t bRequest
Definition: hsusbd.h:89
uint16_t wIndex
Definition: hsusbd.h:91
uint8_t bmRequestType
Definition: hsusbd.h:88
void(* HSUSBD_CLASS_REQ)(void)
Definition: hsusbd.h:354
__STATIC_INLINE void HSUSBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
HSUSBD_SetEpBufAddr, Set Endpoint buffer address.
Definition: hsusbd.h:196
void HSUSBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size)
Prepare Control IN transaction.
Definition: hsusbd.c:617
__STATIC_INLINE void HSUSBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
HSUSBD_ConfigEp, Config Endpoint.
Definition: hsusbd.h:218
void HSUSBD_ProcessSetupPacket(void)
Process Setup Packet.
Definition: hsusbd.c:119
uint16_t wValue
Definition: hsusbd.h:90
__STATIC_INLINE void HSUSBD_MemCopy(uint8_t u8Dst[], uint8_t u8Src[], uint32_t u32Size)
HSUSBD_memcpy, Copy bytes hardware limitation.
Definition: hsusbd.h:167
void HSUSBD_Start(void)
HSUSBD Start.
Definition: hsusbd.c:104
__STATIC_INLINE void HSUSBD_ClearStall(uint32_t u32EpNum)
Clear USB endpoint stall state.
Definition: hsusbd.h:301
__STATIC_INLINE uint32_t HSUSBD_GetStall(uint32_t u32EpNum)
Get USB endpoint stall state.
Definition: hsusbd.h:335
uint8_t ** gu8StringDesc
Definition: hsusbd.h:103
uint32_t * gu32ConfigHidDescIdx
Definition: hsusbd.h:110
uint8_t * gu8ConfigDesc
Definition: hsusbd.h:102
void HSUSBD_Open(S_HSUSBD_INFO_T *param, HSUSBD_CLASS_REQ pfnClassReq, HSUSBD_SET_INTERFACE_REQ pfnSetInterface)
HSUSBD Initial.
Definition: hsusbd.c:70
void HSUSBD_StandardRequest(void)
Process USB standard request.
Definition: hsusbd.c:332
uint8_t * gu8FullConfigDesc
Definition: hsusbd.h:105
uint8_t * gu8DevDesc
Definition: hsusbd.h:101
__STATIC_INLINE void HSUSBD_SetStall(uint32_t u32EpNum)
Set USB endpoint stall state.
Definition: hsusbd.h:262
__STATIC_INLINE void HSUSBD_SetEpStall(uint32_t u32Ep)
Set USB endpoint stall state.
Definition: hsusbd.h:242
void(* HSUSBD_SET_INTERFACE_REQ)(uint32_t u32AltInterface)
Definition: hsusbd.h:355
__STATIC_INLINE void HSUSBD_ResetDMA(void)
HSUSBD_ResetDMA.
Definition: hsusbd.h:183
uint16_t wLength
Definition: hsusbd.h:92
void HSUSBD_CtrlOut(uint8_t pu8Buf[], uint32_t u32Size)
Start Control OUT transaction.
Definition: hsusbd.c:676
uint8_t ** gu8HidReportDesc
Definition: hsusbd.h:108
void HSUSBD_SwReset(void)
Clear all software flags.
Definition: hsusbd.c:702
uint8_t * gu8HSOtherConfigDesc
Definition: hsusbd.h:106
#define HSUSBD_SET_CEP_STATE(flag)
Definition: hsusbd.h:147
void HSUSBD_SetVendorRequest(HSUSBD_VENDOR_REQ pfnVendorReq)
HSUSBD Set Vendor Request.
Definition: hsusbd.c:723
uint32_t * gu32HidReportSize
Definition: hsusbd.h:109
void(* HSUSBD_VENDOR_REQ)(void)
Definition: hsusbd.h:353
uint8_t * gu8QualDesc
Definition: hsusbd.h:104
__STATIC_INLINE uint32_t HSUSBD_GetEpStall(uint32_t u32Ep)
Get USB endpoint stall state.
Definition: hsusbd.h:321
void HSUSBD_CtrlIn(void)
Start Control IN transfer.
Definition: hsusbd.c:634
void HSUSBD_UpdateDeviceState(void)
Update Device State.
Definition: hsusbd.c:524
struct s_hsusbd_info S_HSUSBD_INFO_T
struct HSUSBD_CMD_STRUCT S_HSUSBD_CMD_T
#define HSUSBD
Definition: M480.h:409