M480 BSP V3.0.7000
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)
78/*---------------------------------------------------------------------------------------------------------*/
79/* HSUSBD Define Error Code */
80/*---------------------------------------------------------------------------------------------------------*/
81#define HSUSBD_TIMEOUT SystemCoreClock
82#define HSUSBD_OK ( 0L)
83#define HSUSBD_ERR_FAIL (-1L)
84#define HSUSBD_ERR_TIMEOUT (-2L) /* end of group HSUSBD_EXPORTED_CONSTANTS */
88
94typedef struct HSUSBD_CMD_STRUCT
95{
97 uint8_t bRequest;
98 uint16_t wValue;
99 uint16_t wIndex;
100 uint16_t wLength;
101
107typedef struct s_hsusbd_info
108{
109 uint8_t *gu8DevDesc;
110 uint8_t *gu8ConfigDesc;
111 uint8_t **gu8StringDesc;
112 uint8_t *gu8QualDesc;
120} S_HSUSBD_INFO_T; /* end of group HSUSBD_EXPORTED_STRUCT */
124
126extern uint32_t g_u32HsEpStallLock;
127extern uint8_t volatile g_hsusbd_Configured;
128extern uint8_t g_hsusbd_ShortPacket;
129extern uint8_t g_hsusbd_CtrlZero;
130extern uint8_t g_hsusbd_UsbAddr;
131extern uint32_t volatile g_hsusbd_DmaDone;
132extern uint32_t g_hsusbd_CtrlInSize;
133extern S_HSUSBD_INFO_T gsHSInfo;
134extern S_HSUSBD_CMD_T gUsbCmd;
141#define HSUSBD_ENABLE_USB() ((uint32_t)(HSUSBD->PHYCTL |= (HSUSBD_PHYCTL_PHYEN_Msk|HSUSBD_PHYCTL_DPPUEN_Msk)))
142#define HSUSBD_DISABLE_USB() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk))
143#define HSUSBD_ENABLE_PHY() ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_PHYEN_Msk))
144#define HSUSBD_DISABLE_PHY() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_PHYEN_Msk))
145#define HSUSBD_SET_SE0() ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk))
146#define HSUSBD_CLR_SE0() ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_DPPUEN_Msk))
147#define HSUSBD_SET_ADDR(addr) (HSUSBD->FADDR = (addr))
148#define HSUSBD_GET_ADDR() ((uint32_t)(HSUSBD->FADDR))
149#define HSUSBD_ENABLE_USB_INT(intr) (HSUSBD->GINTEN = (intr))
150#define HSUSBD_ENABLE_BUS_INT(intr) (HSUSBD->BUSINTEN = (intr))
151#define HSUSBD_GET_BUS_INT_FLAG() (HSUSBD->BUSINTSTS)
152#define HSUSBD_CLR_BUS_INT_FLAG(flag) (HSUSBD->BUSINTSTS = (flag))
153#define HSUSBD_ENABLE_CEP_INT(intr) (HSUSBD->CEPINTEN = (intr))
154#define HSUSBD_CLR_CEP_INT_FLAG(flag) (HSUSBD->CEPINTSTS = (flag))
155#define HSUSBD_SET_CEP_STATE(flag) (HSUSBD->CEPCTL = (flag))
156#define HSUSBD_START_CEP_IN(size) (HSUSBD->CEPTXCNT = (size))
157#define HSUSBD_SET_MAX_PAYLOAD(ep, size) (HSUSBD->EP[(ep)].EPMPS = (size))
158#define HSUSBD_ENABLE_EP_INT(ep, intr) (HSUSBD->EP[(ep)].EPINTEN = (intr))
159#define HSUSBD_GET_EP_INT_FLAG(ep) (HSUSBD->EP[(ep)].EPINTSTS)
160#define HSUSBD_CLR_EP_INT_FLAG(ep, flag) (HSUSBD->EP[(ep)].EPINTSTS = (flag))
161#define HSUSBD_SET_DMA_LEN(len) (HSUSBD->DMACNT = (len))
162#define HSUSBD_SET_DMA_ADDR(addr) (HSUSBD->DMAADDR = (addr))
163#define HSUSBD_SET_DMA_READ(epnum) (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~HSUSBD_DMACTL_EPNUM_Msk) | HSUSBD_DMACTL_DMARD_Msk | (epnum) | 0x100)
164#define HSUSBD_SET_DMA_WRITE(epnum) (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~(HSUSBD_DMACTL_EPNUM_Msk | HSUSBD_DMACTL_DMARD_Msk | 0x100)) | (epnum))
165#define HSUSBD_ENABLE_DMA() (HSUSBD->DMACTL |= HSUSBD_DMACTL_DMAEN_Msk)
166#define HSUSBD_IS_ATTACHED() ((uint32_t)(HSUSBD->PHYCTL & HSUSBD_PHYCTL_VBUSDET_Msk))
175__STATIC_INLINE void HSUSBD_MemCopy(uint8_t u8Dst[], uint8_t u8Src[], uint32_t u32Size)
176{
177 uint32_t i = 0ul;
178
179 while (u32Size--)
180 {
181 u8Dst[i] = u8Src[i];
182 i++;
183 }
184}
185
191__STATIC_INLINE void HSUSBD_ResetDMA(void)
192{
193 HSUSBD->DMACNT = 0ul;
194 HSUSBD->DMACTL = 0x80ul;
195 HSUSBD->DMACTL = 0x00ul;
196}
204__STATIC_INLINE void HSUSBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
205{
206 if (u32Ep == CEP)
207 {
208 HSUSBD->CEPBUFST = u32Base;
209 HSUSBD->CEPBUFEND = u32Base + u32Len - 1ul;
210 }
211 else
212 {
213 HSUSBD->EP[u32Ep].EPBUFST = u32Base;
214 HSUSBD->EP[u32Ep].EPBUFEND = u32Base + u32Len - 1ul;
215 }
216}
217
226__STATIC_INLINE void HSUSBD_ConfigEp(uint32_t u32Ep, uint32_t u32EpNum, uint32_t u32EpType, uint32_t u32EpDir)
227{
228 if (u32EpType == HSUSBD_EP_CFG_TYPE_BULK)
229 {
231 }
232 else if (u32EpType == HSUSBD_EP_CFG_TYPE_INT)
233 {
235 }
236 else if (u32EpType == HSUSBD_EP_CFG_TYPE_ISO)
237 {
239 }
240
241 HSUSBD->EP[u32Ep].EPCFG = (u32EpType|u32EpDir|HSUSBD_EP_CFG_VALID|(u32EpNum << 4));
242}
243
250__STATIC_INLINE void HSUSBD_SetEpStall(uint32_t u32Ep)
251{
252 if (u32Ep == CEP)
253 {
255 }
256 else
257 {
258 HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD->EP[u32Ep].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT;
259 }
260}
261
270__STATIC_INLINE void HSUSBD_SetStall(uint32_t u32EpNum)
271{
272 uint32_t i;
273
274 if (u32EpNum == 0ul)
275 {
277 }
278 else
279 {
280 for (i=0ul; i<HSUSBD_MAX_EP; i++)
281 {
282 if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
283 {
284 HSUSBD->EP[i].EPRSPCTL = (HSUSBD->EP[i].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT;
285 }
286 }
287 }
288}
289
296__STATIC_INLINE void HSUSBD_ClearEpStall(uint32_t u32Ep)
297{
298 HSUSBD->EP[u32Ep].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE;
299}
300
309__STATIC_INLINE void HSUSBD_ClearStall(uint32_t u32EpNum)
310{
311 uint32_t i;
312
313 for (i=0ul; i<HSUSBD_MAX_EP; i++)
314 {
315 if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
316 {
317 HSUSBD->EP[i].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE;
318 }
319 }
320}
321
329__STATIC_INLINE uint32_t HSUSBD_GetEpStall(uint32_t u32Ep)
330{
331 return (HSUSBD->EP[u32Ep].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT);
332}
333
343__STATIC_INLINE uint32_t HSUSBD_GetStall(uint32_t u32EpNum)
344{
345 uint32_t i;
346 uint32_t val = 0ul;
347
348 for (i=0ul; i<HSUSBD_MAX_EP; i++)
349 {
350 if (((HSUSBD->EP[i].EPCFG & 0xf0ul) >> 4) == u32EpNum)
351 {
352 val = (HSUSBD->EP[i].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT);
353 break;
354 }
355 }
356 return val;
357}
358
359
360/*-------------------------------------------------------------------------------------------*/
361typedef void (*HSUSBD_VENDOR_REQ)(void);
362typedef void (*HSUSBD_CLASS_REQ)(void);
363typedef void (*HSUSBD_SET_INTERFACE_REQ)(uint32_t u32AltInterface);
365void HSUSBD_Open(S_HSUSBD_INFO_T *param, HSUSBD_CLASS_REQ pfnClassReq, HSUSBD_SET_INTERFACE_REQ pfnSetInterface);
366void HSUSBD_Start(void);
368void HSUSBD_StandardRequest(void);
369void HSUSBD_UpdateDeviceState(void);
370void HSUSBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size);
371void HSUSBD_CtrlIn(void);
372int32_t HSUSBD_CtrlOut(uint8_t pu8Buf[], uint32_t u32Size);
373void HSUSBD_SwReset(void);
375
376
377 /* end of group HSUSBD_EXPORTED_FUNCTIONS */
379 /* end of group HSUSBD_Driver */
381 /* end of group Standard_Driver */
383
384#ifdef __cplusplus
385}
386#endif
387
388#endif /*__HSUSBD_H__ */
389
390/*** (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:296
uint8_t * gu8FSOtherConfigDesc
Definition: hsusbd.h:115
uint8_t bRequest
Definition: hsusbd.h:97
uint16_t wIndex
Definition: hsusbd.h:99
uint8_t bmRequestType
Definition: hsusbd.h:96
void(* HSUSBD_CLASS_REQ)(void)
Definition: hsusbd.h:362
__STATIC_INLINE void HSUSBD_SetEpBufAddr(uint32_t u32Ep, uint32_t u32Base, uint32_t u32Len)
HSUSBD_SetEpBufAddr, Set Endpoint buffer address.
Definition: hsusbd.h:204
int32_t HSUSBD_CtrlOut(uint8_t pu8Buf[], uint32_t u32Size)
Start Control OUT transaction.
Definition: hsusbd.c:676
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:226
void HSUSBD_ProcessSetupPacket(void)
Process Setup Packet.
Definition: hsusbd.c:119
uint16_t wValue
Definition: hsusbd.h:98
__STATIC_INLINE void HSUSBD_MemCopy(uint8_t u8Dst[], uint8_t u8Src[], uint32_t u32Size)
HSUSBD_memcpy, Copy bytes hardware limitation.
Definition: hsusbd.h:175
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:309
__STATIC_INLINE uint32_t HSUSBD_GetStall(uint32_t u32EpNum)
Get USB endpoint stall state.
Definition: hsusbd.h:343
uint8_t ** gu8StringDesc
Definition: hsusbd.h:111
uint32_t * gu32ConfigHidDescIdx
Definition: hsusbd.h:118
uint8_t * gu8ConfigDesc
Definition: hsusbd.h:110
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:113
uint8_t * gu8DevDesc
Definition: hsusbd.h:109
__STATIC_INLINE void HSUSBD_SetStall(uint32_t u32EpNum)
Set USB endpoint stall state.
Definition: hsusbd.h:270
__STATIC_INLINE void HSUSBD_SetEpStall(uint32_t u32Ep)
Set USB endpoint stall state.
Definition: hsusbd.h:250
void(* HSUSBD_SET_INTERFACE_REQ)(uint32_t u32AltInterface)
Definition: hsusbd.h:363
__STATIC_INLINE void HSUSBD_ResetDMA(void)
HSUSBD_ResetDMA.
Definition: hsusbd.h:191
uint16_t wLength
Definition: hsusbd.h:100
uint8_t ** gu8HidReportDesc
Definition: hsusbd.h:116
void HSUSBD_SwReset(void)
Clear all software flags.
Definition: hsusbd.c:706
uint8_t * gu8HSOtherConfigDesc
Definition: hsusbd.h:114
#define HSUSBD_SET_CEP_STATE(flag)
Definition: hsusbd.h:155
void HSUSBD_SetVendorRequest(HSUSBD_VENDOR_REQ pfnVendorReq)
HSUSBD Set Vendor Request.
Definition: hsusbd.c:727
uint32_t * gu32HidReportSize
Definition: hsusbd.h:117
void(* HSUSBD_VENDOR_REQ)(void)
Definition: hsusbd.h:361
uint8_t * gu8QualDesc
Definition: hsusbd.h:112
__STATIC_INLINE uint32_t HSUSBD_GetEpStall(uint32_t u32Ep)
Get USB endpoint stall state.
Definition: hsusbd.h:329
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