1 /**************************************************************************//**
2  * @file     hsusbd.h
3  * @version  V1.00
4  * @brief    M480 HSUSBD driver header file
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  * @copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
8  *****************************************************************************/
9 #ifndef __HSUSBD_H__
10 #define __HSUSBD_H__
11 
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16 
17 
18 /** @addtogroup Standard_Driver Standard Driver
19   @{
20 */
21 
22 /** @addtogroup HSUSBD_Driver HSUSBD Driver
23   @{
24 */
25 
26 /** @addtogroup HSUSBD_EXPORTED_CONSTANTS HSUSBD Exported Constants
27   @{
28 */
29 /** @cond HIDDEN_SYMBOLS */
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    /*!< Control Endpoint  \hideinitializer */
37 #define EPA     0ul       /*!< Endpoint A  \hideinitializer */
38 #define EPB     1ul       /*!< Endpoint B  \hideinitializer */
39 #define EPC     2ul       /*!< Endpoint C  \hideinitializer */
40 #define EPD     3ul       /*!< Endpoint D  \hideinitializer */
41 #define EPE     4ul       /*!< Endpoint E  \hideinitializer */
42 #define EPF     5ul       /*!< Endpoint F  \hideinitializer */
43 #define EPG     6ul       /*!< Endpoint G  \hideinitializer */
44 #define EPH     7ul       /*!< Endpoint H  \hideinitializer */
45 #define EPI     8ul       /*!< Endpoint I  \hideinitializer */
46 #define EPJ     9ul       /*!< Endpoint J  \hideinitializer */
47 #define EPK     10ul      /*!< Endpoint K  \hideinitializer */
48 #define EPL     11ul      /*!< Endpoint L  \hideinitializer */
49 
50 /** @endcond HIDDEN_SYMBOLS */
51 /********************* Bit definition of CEPCTL register **********************/
52 #define HSUSBD_CEPCTL_NAKCLR               ((uint32_t)0x00000000ul)      /*!<NAK clear  \hideinitializer */
53 #define HSUSBD_CEPCTL_STALL                ((uint32_t)0x00000002ul)      /*!<Stall  \hideinitializer */
54 #define HSUSBD_CEPCTL_ZEROLEN              ((uint32_t)0x00000004ul)      /*!<Zero length packet  \hideinitializer */
55 #define HSUSBD_CEPCTL_FLUSH                ((uint32_t)0x00000008ul)      /*!<CEP flush  \hideinitializer */
56 
57 /********************* Bit definition of EPxRSPCTL register **********************/
58 #define HSUSBD_EP_RSPCTL_FLUSH             ((uint32_t)0x00000001ul)      /*!<Buffer Flush  \hideinitializer */
59 #define HSUSBD_EP_RSPCTL_MODE_AUTO         ((uint32_t)0x00000000ul)      /*!<Auto-Validate Mode  \hideinitializer */
60 #define HSUSBD_EP_RSPCTL_MODE_MANUAL       ((uint32_t)0x00000002ul)      /*!<Manual-Validate Mode  \hideinitializer */
61 #define HSUSBD_EP_RSPCTL_MODE_FLY          ((uint32_t)0x00000004ul)      /*!<Fly Mode  \hideinitializer */
62 #define HSUSBD_EP_RSPCTL_MODE_MASK         ((uint32_t)0x00000006ul)      /*!<Mode Mask  \hideinitializer */
63 #define HSUSBD_EP_RSPCTL_TOGGLE            ((uint32_t)0x00000008ul)      /*!<Clear Toggle bit  \hideinitializer */
64 #define HSUSBD_EP_RSPCTL_HALT              ((uint32_t)0x00000010ul)      /*!<Endpoint halt  \hideinitializer */
65 #define HSUSBD_EP_RSPCTL_ZEROLEN           ((uint32_t)0x00000020ul)      /*!<Zero length packet IN  \hideinitializer */
66 #define HSUSBD_EP_RSPCTL_SHORTTXEN         ((uint32_t)0x00000040ul)      /*!<Packet end  \hideinitializer */
67 #define HSUSBD_EP_RSPCTL_DISBUF            ((uint32_t)0x00000080ul)      /*!<Disable buffer  \hideinitializer */
68 
69 /********************* Bit definition of EPxCFG register **********************/
70 #define HSUSBD_EP_CFG_VALID                ((uint32_t)0x00000001ul)      /*!<Endpoint Valid  \hideinitializer */
71 #define HSUSBD_EP_CFG_TYPE_BULK            ((uint32_t)0x00000002ul)      /*!<Endpoint type - bulk  \hideinitializer */
72 #define HSUSBD_EP_CFG_TYPE_INT             ((uint32_t)0x00000004ul)      /*!<Endpoint type - interrupt  \hideinitializer */
73 #define HSUSBD_EP_CFG_TYPE_ISO             ((uint32_t)0x00000006ul)      /*!<Endpoint type - isochronous  \hideinitializer */
74 #define HSUSBD_EP_CFG_TYPE_MASK            ((uint32_t)0x00000006ul)      /*!<Endpoint type mask  \hideinitializer */
75 #define HSUSBD_EP_CFG_DIR_OUT              ((uint32_t)0x00000000ul)      /*!<OUT endpoint  \hideinitializer */
76 #define HSUSBD_EP_CFG_DIR_IN               ((uint32_t)0x00000008ul)      /*!<IN endpoint  \hideinitializer */
77 
78 
79 /*@}*/ /* end of group HSUSBD_EXPORTED_CONSTANTS */
80 
81 /** @addtogroup HSUSBD_EXPORTED_STRUCT HSUSBD Exported Struct
82   @{
83 */
84 
85 
86 typedef struct HSUSBD_CMD_STRUCT
87 {
88     uint8_t  bmRequestType;
89     uint8_t  bRequest;
90     uint16_t wValue;
91     uint16_t wIndex;
92     uint16_t wLength;
93 
94 } S_HSUSBD_CMD_T; /*!<USB Setup Packet Structure */
95 
96 
97 
98 
99 typedef struct s_hsusbd_info
100 {
101     uint8_t *gu8DevDesc;            /*!< Device descriptor */
102     uint8_t *gu8ConfigDesc;         /*!< Config descriptor */
103     uint8_t **gu8StringDesc;        /*!< Pointer for USB String Descriptor pointers */
104     uint8_t *gu8QualDesc;           /*!< Qualifier descriptor */
105     uint8_t *gu8FullConfigDesc;     /*!< Full Speed Config descriptor */
106     uint8_t *gu8HSOtherConfigDesc;  /*!< Other Speed Config descriptor */
107     uint8_t *gu8FSOtherConfigDesc;  /*!< Other Speed Config descriptor */
108     uint8_t **gu8HidReportDesc;     /*!< Pointer for HID Report descriptor */
109     uint32_t *gu32HidReportSize;    /*!< Pointer for HID Report descriptor Size */
110     uint32_t *gu32ConfigHidDescIdx; /*!< Pointer for HID Descriptor start index */
111 
112 } S_HSUSBD_INFO_T; /*!<USB Information Structure */
113 
114 
115 /*@}*/ /* end of group HSUSBD_EXPORTED_STRUCT */
116 
117 /** @cond HIDDEN_SYMBOLS */
118 extern uint32_t g_u32HsEpStallLock;
119 extern uint8_t volatile g_hsusbd_Configured;
120 extern uint8_t g_hsusbd_ShortPacket;
121 extern uint8_t g_hsusbd_CtrlZero;
122 extern uint8_t g_hsusbd_UsbAddr;
123 extern uint32_t volatile g_hsusbd_DmaDone;
124 extern uint32_t g_hsusbd_CtrlInSize;
125 extern S_HSUSBD_INFO_T gsHSInfo;
126 extern S_HSUSBD_CMD_T gUsbCmd;
127 /** @endcond HIDDEN_SYMBOLS */
128 
129 /** @addtogroup HSUSBD_EXPORTED_FUNCTIONS HSUSBD Exported Functions
130   @{
131 */
132 
133 #define HSUSBD_ENABLE_USB()               ((uint32_t)(HSUSBD->PHYCTL |= (HSUSBD_PHYCTL_PHYEN_Msk|HSUSBD_PHYCTL_DPPUEN_Msk))) /*!<Enable USB  \hideinitializer */
134 #define HSUSBD_DISABLE_USB()              ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk)) /*!<Disable USB  \hideinitializer */
135 #define HSUSBD_ENABLE_PHY()               ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_PHYEN_Msk)) /*!<Enable PHY  \hideinitializer */
136 #define HSUSBD_DISABLE_PHY()              ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_PHYEN_Msk)) /*!<Disable PHY  \hideinitializer */
137 #define HSUSBD_SET_SE0()                  ((uint32_t)(HSUSBD->PHYCTL &= ~HSUSBD_PHYCTL_DPPUEN_Msk)) /*!<Enable SE0, Force USB PHY Transceiver to Drive SE0  \hideinitializer */
138 #define HSUSBD_CLR_SE0()                  ((uint32_t)(HSUSBD->PHYCTL |= HSUSBD_PHYCTL_DPPUEN_Msk)) /*!<Disable SE0  \hideinitializer */
139 #define HSUSBD_SET_ADDR(addr)             (HSUSBD->FADDR = (addr)) /*!<Set USB address  \hideinitializer */
140 #define HSUSBD_GET_ADDR()                 ((uint32_t)(HSUSBD->FADDR)) /*!<Get USB address  \hideinitializer */
141 #define HSUSBD_ENABLE_USB_INT(intr)       (HSUSBD->GINTEN = (intr)) /*!<Enable USB Interrupt  \hideinitializer */
142 #define HSUSBD_ENABLE_BUS_INT(intr)       (HSUSBD->BUSINTEN = (intr)) /*!<Enable BUS Interrupt  \hideinitializer */
143 #define HSUSBD_GET_BUS_INT_FLAG()         (HSUSBD->BUSINTSTS)        /*!<Get Bus interrupt flag  \hideinitializer */
144 #define HSUSBD_CLR_BUS_INT_FLAG(flag)     (HSUSBD->BUSINTSTS = (flag)) /*!<Clear Bus interrupt flag  \hideinitializer */
145 #define HSUSBD_ENABLE_CEP_INT(intr)       (HSUSBD->CEPINTEN = (intr)) /*!<Enable CEP Interrupt  \hideinitializer */
146 #define HSUSBD_CLR_CEP_INT_FLAG(flag)     (HSUSBD->CEPINTSTS = (flag)) /*!<Clear CEP interrupt flag  \hideinitializer */
147 #define HSUSBD_SET_CEP_STATE(flag)        (HSUSBD->CEPCTL = (flag)) /*!<Set CEP state  \hideinitializer */
148 #define HSUSBD_START_CEP_IN(size)         (HSUSBD->CEPTXCNT = (size)) /*!<Start CEP IN Transfer  \hideinitializer */
149 #define HSUSBD_SET_MAX_PAYLOAD(ep, size)  (HSUSBD->EP[(ep)].EPMPS = (size)) /*!<Set EPx Maximum Packet Size  \hideinitializer */
150 #define HSUSBD_ENABLE_EP_INT(ep, intr)    (HSUSBD->EP[(ep)].EPINTEN = (intr)) /*!<Enable EPx Interrupt  \hideinitializer */
151 #define HSUSBD_GET_EP_INT_FLAG(ep)        (HSUSBD->EP[(ep)].EPINTSTS) /*!<Get EPx interrupt flag  \hideinitializer */
152 #define HSUSBD_CLR_EP_INT_FLAG(ep, flag)  (HSUSBD->EP[(ep)].EPINTSTS = (flag)) /*!<Clear EPx interrupt flag  \hideinitializer */
153 #define HSUSBD_SET_DMA_LEN(len)           (HSUSBD->DMACNT = (len)) /*!<Set DMA transfer length  \hideinitializer */
154 #define HSUSBD_SET_DMA_ADDR(addr)         (HSUSBD->DMAADDR = (addr)) /*!<Set DMA transfer address  \hideinitializer */
155 #define HSUSBD_SET_DMA_READ(epnum)        (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~HSUSBD_DMACTL_EPNUM_Msk) | HSUSBD_DMACTL_DMARD_Msk | (epnum) | 0x100) /*!<Set DMA transfer type to read \hideinitializer */
156 #define HSUSBD_SET_DMA_WRITE(epnum)       (HSUSBD->DMACTL = (HSUSBD->DMACTL & ~(HSUSBD_DMACTL_EPNUM_Msk | HSUSBD_DMACTL_DMARD_Msk | 0x100)) | (epnum)) /*!<Set DMA transfer type to write \hideinitializer */
157 #define HSUSBD_ENABLE_DMA()               (HSUSBD->DMACTL |= HSUSBD_DMACTL_DMAEN_Msk) /*!<Enable DMA transfer  \hideinitializer */
158 #define HSUSBD_IS_ATTACHED()              ((uint32_t)(HSUSBD->PHYCTL & HSUSBD_PHYCTL_VBUSDET_Msk)) /*!<Check cable connect state  \hideinitializer */
159 
160 /**
161   * @brief  HSUSBD_memcpy, Copy bytes hardware limitation
162   * @param[in]  u8Dst   Destination pointer.
163   * @param[in]  u8Src   Source pointer.
164   * @param[in]  u32Size Copy size.
165   * @retval None.
166   */
HSUSBD_MemCopy(uint8_t u8Dst[],uint8_t u8Src[],uint32_t u32Size)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 
178 /**
179   * @brief  HSUSBD_ResetDMA
180   * @param  None
181   * @retval None.
182   */
HSUSBD_ResetDMA(void)183 __STATIC_INLINE void HSUSBD_ResetDMA(void)
184 {
185     HSUSBD->DMACNT = 0ul;
186     HSUSBD->DMACTL = 0x80ul;
187     HSUSBD->DMACTL = 0x00ul;
188 }
189 /**
190   * @brief  HSUSBD_SetEpBufAddr, Set Endpoint buffer address
191   * @param[in]  u32Ep      Endpoint Number
192   * @param[in]  u32Base    Buffer Start Address
193   * @param[in]  u32Len     Buffer length
194   * @retval None.
195   */
HSUSBD_SetEpBufAddr(uint32_t u32Ep,uint32_t u32Base,uint32_t u32Len)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 
210 /**
211   * @brief  HSUSBD_ConfigEp, Config Endpoint
212   * @param[in]  u32Ep      USB endpoint
213   * @param[in]  u32EpNum   Endpoint number
214   * @param[in]  u32EpType  Endpoint type
215   * @param[in]  u32EpDir   Endpoint direction
216   * @retval None.
217   */
HSUSBD_ConfigEp(uint32_t u32Ep,uint32_t u32EpNum,uint32_t u32EpType,uint32_t u32EpDir)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     {
222         HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH|HSUSBD_EP_RSPCTL_MODE_AUTO);
223     }
224     else if (u32EpType == HSUSBD_EP_CFG_TYPE_INT)
225     {
226         HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH|HSUSBD_EP_RSPCTL_MODE_MANUAL);
227     }
228     else if (u32EpType == HSUSBD_EP_CFG_TYPE_ISO)
229     {
230         HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD_EP_RSPCTL_FLUSH|HSUSBD_EP_RSPCTL_MODE_FLY);
231     }
232 
233     HSUSBD->EP[u32Ep].EPCFG = (u32EpType|u32EpDir|HSUSBD_EP_CFG_VALID|(u32EpNum << 4));
234 }
235 
236 /**
237   * @brief       Set USB endpoint stall state
238   * @param[in]   u32Ep  The USB endpoint ID.
239   * @return      None
240   * @details     Set USB endpoint stall state for the specified endpoint ID. Endpoint will respond STALL token automatically.
241   */
HSUSBD_SetEpStall(uint32_t u32Ep)242 __STATIC_INLINE void HSUSBD_SetEpStall(uint32_t u32Ep)
243 {
244     if (u32Ep == CEP)
245     {
246         HSUSBD_SET_CEP_STATE(HSUSBD_CEPCTL_STALL);
247     }
248     else
249     {
250         HSUSBD->EP[u32Ep].EPRSPCTL = (HSUSBD->EP[u32Ep].EPRSPCTL & 0xf7ul) | HSUSBD_EP_RSPCTL_HALT;
251     }
252 }
253 
254 /**
255  * @brief       Set USB endpoint stall state
256  *
257  * @param[in]   u32EpNum         USB endpoint
258  * @return      None
259  *
260  * @details     Set USB endpoint stall state, endpoint will return STALL token.
261  */
HSUSBD_SetStall(uint32_t u32EpNum)262 __STATIC_INLINE void HSUSBD_SetStall(uint32_t u32EpNum)
263 {
264     uint32_t i;
265 
266     if (u32EpNum == 0ul)
267     {
268         HSUSBD_SET_CEP_STATE(HSUSBD_CEPCTL_STALL);
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 
282 /**
283   * @brief       Clear USB endpoint stall state
284   * @param[in]   u32Ep  The USB endpoint ID.
285   * @return      None
286   * @details     Clear USB endpoint stall state for the specified endpoint ID. Endpoint will respond ACK/NAK token.
287   */
HSUSBD_ClearEpStall(uint32_t u32Ep)288 __STATIC_INLINE void  HSUSBD_ClearEpStall(uint32_t u32Ep)
289 {
290     HSUSBD->EP[u32Ep].EPRSPCTL = HSUSBD_EP_RSPCTL_TOGGLE;
291 }
292 
293 /**
294  * @brief       Clear USB endpoint stall state
295  *
296  * @param[in]   u32EpNum         USB endpoint
297  * @return      None
298  *
299  * @details     Clear USB endpoint stall state, endpoint will return ACK/NAK token.
300  */
HSUSBD_ClearStall(uint32_t u32EpNum)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 
314 /**
315   * @brief       Get USB endpoint stall state
316   * @param[in]   u32Ep  The USB endpoint ID.
317   * @retval      0      USB endpoint is not stalled.
318   * @retval      Others USB endpoint is stalled.
319   * @details     Get USB endpoint stall state of the specified endpoint ID.
320   */
HSUSBD_GetEpStall(uint32_t u32Ep)321 __STATIC_INLINE uint32_t HSUSBD_GetEpStall(uint32_t u32Ep)
322 {
323     return (HSUSBD->EP[u32Ep].EPRSPCTL & HSUSBD_EP_RSPCTL_HALT);
324 }
325 
326 /**
327  * @brief       Get USB endpoint stall state
328  *
329  * @param[in]   u32EpNum         USB endpoint
330  * @retval      0: USB endpoint is not stalled.
331  * @retval      non-0: USB endpoint is stalled.
332  *
333  * @details     Get USB endpoint stall state.
334  */
HSUSBD_GetStall(uint32_t u32EpNum)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 /*-------------------------------------------------------------------------------------------*/
353 typedef void (*HSUSBD_VENDOR_REQ)(void); /*!<USB Vendor request callback function */
354 typedef void (*HSUSBD_CLASS_REQ)(void); /*!<USB Class request callback function */
355 typedef void (*HSUSBD_SET_INTERFACE_REQ)(uint32_t u32AltInterface); /*!<USB Standard request "Set Interface" callback function */
356 
357 void HSUSBD_Open(S_HSUSBD_INFO_T *param, HSUSBD_CLASS_REQ pfnClassReq, HSUSBD_SET_INTERFACE_REQ pfnSetInterface);
358 void HSUSBD_Start(void);
359 void HSUSBD_ProcessSetupPacket(void);
360 void HSUSBD_StandardRequest(void);
361 void HSUSBD_UpdateDeviceState(void);
362 void HSUSBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size);
363 void HSUSBD_CtrlIn(void);
364 void HSUSBD_CtrlOut(uint8_t pu8Buf[], uint32_t u32Size);
365 void HSUSBD_SwReset(void);
366 void HSUSBD_SetVendorRequest(HSUSBD_VENDOR_REQ pfnVendorReq);
367 
368 
369 
370 /*@}*/ /* end of group HSUSBD_EXPORTED_FUNCTIONS */
371 
372 /*@}*/ /* end of group HSUSBD_Driver */
373 
374 /*@}*/ /* 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. ***/
383