1 /**************************************************************************//**
2  * @file     usbd.h
3  * @version  V1.00
4  * @brief    M2L31 series USB 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 __USBD_H__
10 #define __USBD_H__
11 
12 //#define SUPPORT_LPM     // define to support LPM
13 #ifdef __cplusplus
14 extern "C"
15 {
16 #endif
17 
18 
19 /** @addtogroup Standard_Driver Standard Driver
20   @{
21 */
22 
23 /** @addtogroup USBD_Driver USBD Driver
24   @{
25 */
26 
27 /** @addtogroup USBD_EXPORTED_STRUCT USBD Exported Struct
28   @{
29 */
30 typedef struct s_usbd_info
31 {
32     uint8_t *gu8DevDesc;            /*!< Pointer for USB Device Descriptor          */
33     uint8_t *gu8ConfigDesc;         /*!< Pointer for USB Configuration Descriptor   */
34     uint8_t **gu8StringDesc;        /*!< Pointer for USB String Descriptor pointers */
35     uint8_t **gu8HidReportDesc;     /*!< Pointer for USB HID Report Descriptor      */
36     uint8_t *gu8BosDesc;            /*!< Pointer for USB BOS Descriptor             */
37     uint32_t *gu32HidReportSize;    /*!< Pointer for HID Report descriptor Size */
38     uint32_t *gu32ConfigHidDescIdx; /*!< Pointer for HID Descriptor start index */
39 
40 } S_USBD_INFO_T;  /*!< Device description structure */
41 
42 extern const S_USBD_INFO_T gsInfo;
43 
44 /*@}*/ /* end of group USBD_EXPORTED_STRUCT */
45 
46 
47 
48 
49 /** @addtogroup USBD_EXPORTED_CONSTANTS USBD Exported Constants
50   @{
51 */
52 #define USBD_BUF_BASE   (USBD_BASE+0x800ul)  /*!< USBD buffer base address \hideinitializer */
53 #define USBD_MAX_EP     19ul /*!< Total EP number \hideinitializer */
54 
55 #define EP0     0ul       /*!< Endpoint 0 \hideinitializer */
56 #define EP1     1ul       /*!< Endpoint 1 \hideinitializer */
57 #define EP2     2ul       /*!< Endpoint 2 \hideinitializer */
58 #define EP3     3ul       /*!< Endpoint 3 \hideinitializer */
59 #define EP4     4ul       /*!< Endpoint 4 \hideinitializer */
60 #define EP5     5ul       /*!< Endpoint 5 \hideinitializer */
61 #define EP6     6ul       /*!< Endpoint 6 \hideinitializer */
62 #define EP7     7ul       /*!< Endpoint 7 \hideinitializer */
63 #define EP8     8ul       /*!< Endpoint 8 \hideinitializer */
64 #define EP9     9ul       /*!< Endpoint 9 \hideinitializer */
65 #define EP10    10ul      /*!< Endpoint 10 \hideinitializer */
66 #define EP11    11ul      /*!< Endpoint 11 \hideinitializer */
67 #define EP12    12ul      /*!< Endpoint 12 \hideinitializer */
68 #define EP13    13ul      /*!< Endpoint 13 \hideinitializer */
69 #define EP14    14ul      /*!< Endpoint 14 \hideinitializer */
70 #define EP15    15ul      /*!< Endpoint 15 \hideinitializer */
71 #define EP16    16ul      /*!< Endpoint 16 \hideinitializer */
72 #define EP17    17ul      /*!< Endpoint 17 \hideinitializer */
73 #define EP18    18ul      /*!< Endpoint 18 \hideinitializer */
74 
75 
76 /** @cond HIDDEN_SYMBOLS */
77 /* USB Request Type */
78 #define REQ_STANDARD        0x00ul
79 #define REQ_CLASS           0x20ul
80 #define REQ_VENDOR          0x40ul
81 
82 /* USB Standard Request */
83 #define GET_STATUS          0x00ul
84 #define CLEAR_FEATURE       0x01ul
85 #define SET_FEATURE         0x03ul
86 #define SET_ADDRESS         0x05ul
87 #define GET_DESCRIPTOR      0x06ul
88 #define SET_DESCRIPTOR      0x07ul
89 #define GET_CONFIGURATION   0x08ul
90 #define SET_CONFIGURATION   0x09ul
91 #define GET_INTERFACE       0x0Aul
92 #define SET_INTERFACE       0x0Bul
93 #define SYNC_FRAME          0x0Cul
94 
95 /* USB Descriptor Type */
96 #define DESC_DEVICE         0x01ul
97 #define DESC_CONFIG         0x02ul
98 #define DESC_STRING         0x03ul
99 #define DESC_INTERFACE      0x04ul
100 #define DESC_ENDPOINT       0x05ul
101 #define DESC_QUALIFIER      0x06ul
102 #define DESC_OTHERSPEED     0x07ul
103 #define DESC_IFPOWER        0x08ul
104 #define DESC_OTG            0x09ul
105 #define DESC_IAD            0x0Bul
106 #define DESC_BOS            0x0Ful
107 #define DESC_CAPABILITY     0x10ul
108 
109 /* USB Device Capability Type */
110 #define CAP_WIRELESS        0x01ul
111 #define CAP_USB20_EXT       0x02ul
112 
113 /* USB HID Descriptor Type */
114 #define DESC_HID            0x21ul
115 #define DESC_HID_RPT        0x22ul
116 
117 /* USB Descriptor Length */
118 #define LEN_DEVICE          18ul
119 #define LEN_QUALIFIER       10ul
120 #define LEN_CONFIG          9ul
121 #define LEN_INTERFACE       9ul
122 #define LEN_ENDPOINT        7ul
123 #define LEN_OTG             5ul
124 #define LEN_BOS             5ul
125 #define LEN_HID             9ul
126 #define LEN_CCID            0x36ul
127 #define LEN_BOSCAP          7ul
128 #define LEN_IAD             8ul
129 
130 /* USB Endpoint Type */
131 #define EP_ISO              0x01
132 #define EP_BULK             0x02
133 #define EP_INT              0x03
134 
135 #define EP_INPUT            0x80
136 #define EP_OUTPUT           0x00
137 
138 /* USB Feature Selector */
139 #define FEATURE_DEVICE_REMOTE_WAKEUP    0x01ul
140 #define FEATURE_ENDPOINT_HALT           0x00ul
141 /** @endcond HIDDEN_SYMBOLS */
142 
143 /******************************************************************************/
144 /*                USB Specific Macros                                         */
145 /******************************************************************************/
146 
147 #define USBD_WAKEUP_EN          USBD_INTEN_WKEN_Msk         /*!< USB Wake-up Enable \hideinitializer */
148 #define USBD_DRVSE0             USBD_SE0_SE0_Msk            /*!< Drive SE0 \hideinitializer */
149 
150 #define USBD_L1RESUME		    USBD_ATTR_L1RESUME_Msk
151 #define USBD_L1SUSPEND		    USBD_ATTR_L1SUSPEND_Msk
152 #define USBD_LPMACK             USBD_ATTR_LPMACK_Msk        /*!< LPM Enable \hideinitializer */
153 #define USBD_BYTEM              USBD_ATTR_BYTEM_Msk         /*!< Access Size Mode Selection \hideinitializer */
154 #define USBD_PWRDN              USBD_ATTR_PWRDN_Msk         /*!< PHY Turn-On \hideinitializer */
155 #define USBD_DPPU_EN            USBD_ATTR_DPPUEN_Msk        /*!< USB D+ Pull-up Enable \hideinitializer */
156 #define USBD_USB_EN             USBD_ATTR_USBEN_Msk         /*!< USB Enable \hideinitializer */
157 #define USBD_RWAKEUP            USBD_ATTR_RWAKEUP_Msk       /*!< Remote Wake-Up \hideinitializer */
158 #define USBD_PHY_EN             USBD_ATTR_PHYEN_Msk         /*!< PHY Enable \hideinitializer */
159 
160 #define USBD_INT_BUS            USBD_INTEN_BUSIEN_Msk       /*!< USB Bus Event Interrupt \hideinitializer */
161 #define USBD_INT_USB            USBD_INTEN_USBIEN_Msk       /*!< USB Event Interrupt \hideinitializer */
162 #define USBD_INT_FLDET          USBD_INTEN_VBDETIEN_Msk     /*!< USB VBUS Detection Interrupt \hideinitializer */
163 #define USBD_INT_WAKEUP         (USBD_INTEN_NEVWKIEN_Msk | USBD_INTEN_WKEN_Msk)     /*!< USB No-Event-Wake-Up Interrupt \hideinitializer */
164 #define USBD_INT_SOF            USBD_INTEN_SOFIEN_Msk       /*!< USB Start of Frame Interrupt \hideinitializer */
165 
166 #define USBD_INTSTS_WAKEUP      USBD_INTSTS_NEVWKIF_Msk     /*!< USB No-Event-Wake-Up Interrupt Status \hideinitializer */
167 #define USBD_INTSTS_FLDET       USBD_INTSTS_VBDETIF_Msk     /*!< USB Float Detect Interrupt Status \hideinitializer */
168 #define USBD_INTSTS_BUS         USBD_INTSTS_BUSIF_Msk       /*!< USB Bus Event Interrupt Status \hideinitializer */
169 #define USBD_INTSTS_USB         USBD_INTSTS_USBIF_Msk       /*!< USB Event Interrupt Status \hideinitializer */
170 #define USBD_INTSTS_SETUP       USBD_INTSTS_SETUP_Msk       /*!< USB Setup Event \hideinitializer */
171 #define USBD_INTSTS_EP0         USBD_INTSTS_EPEVT0_Msk      /*!< USB Endpoint 0 Event \hideinitializer */
172 #define USBD_INTSTS_EP1         USBD_INTSTS_EPEVT1_Msk      /*!< USB Endpoint 1 Event \hideinitializer */
173 #define USBD_INTSTS_EP2         USBD_INTSTS_EPEVT2_Msk      /*!< USB Endpoint 2 Event \hideinitializer */
174 #define USBD_INTSTS_EP3         USBD_INTSTS_EPEVT3_Msk      /*!< USB Endpoint 3 Event \hideinitializer */
175 #define USBD_INTSTS_EP4         USBD_INTSTS_EPEVT4_Msk      /*!< USB Endpoint 4 Event \hideinitializer */
176 #define USBD_INTSTS_EP5         USBD_INTSTS_EPEVT5_Msk      /*!< USB Endpoint 5 Event \hideinitializer */
177 #define USBD_INTSTS_EP6         USBD_INTSTS_EPEVT6_Msk      /*!< USB Endpoint 6 Event \hideinitializer */
178 #define USBD_INTSTS_EP7         USBD_INTSTS_EPEVT7_Msk      /*!< USB Endpoint 7 Event \hideinitializer */
179 #define USBD_INTSTS_EP8         USBD_INTSTS_EPEVT8_Msk      /*!< USB Endpoint 8 Event \hideinitializer */
180 #define USBD_INTSTS_EP9         USBD_INTSTS_EPEVT9_Msk      /*!< USB Endpoint 9 Event \hideinitializer */
181 #define USBD_INTSTS_EP10        USBD_INTSTS_EPEVT10_Msk     /*!< USB Endpoint 10 Event \hideinitializer */
182 #define USBD_INTSTS_EP11        USBD_INTSTS_EPEVT11_Msk     /*!< USB Endpoint 11 Event \hideinitializer */
183 
184 #define USBD_EPINTSTS_EP0       USBD_EPINTSTS_EPEVT0_Msk    /*!< USB Endpoint 0 Event \hideinitializer */
185 #define USBD_EPINTSTS_EP1       USBD_EPINTSTS_EPEVT1_Msk    /*!< USB Endpoint 1 Event \hideinitializer */
186 #define USBD_EPINTSTS_EP2       USBD_EPINTSTS_EPEVT2_Msk    /*!< USB Endpoint 2 Event \hideinitializer */
187 #define USBD_EPINTSTS_EP3       USBD_EPINTSTS_EPEVT3_Msk    /*!< USB Endpoint 3 Event \hideinitializer */
188 #define USBD_EPINTSTS_EP4       USBD_EPINTSTS_EPEVT4_Msk    /*!< USB Endpoint 4 Event \hideinitializer */
189 #define USBD_EPINTSTS_EP5       USBD_EPINTSTS_EPEVT5_Msk    /*!< USB Endpoint 5 Event \hideinitializer */
190 #define USBD_EPINTSTS_EP6       USBD_EPINTSTS_EPEVT6_Msk    /*!< USB Endpoint 6 Event \hideinitializer */
191 #define USBD_EPINTSTS_EP7       USBD_EPINTSTS_EPEVT7_Msk    /*!< USB Endpoint 7 Event \hideinitializer */
192 #define USBD_EPINTSTS_EP8       USBD_EPINTSTS_EPEVT8_Msk    /*!< USB Endpoint 8 Event \hideinitializer */
193 #define USBD_EPINTSTS_EP9       USBD_EPINTSTS_EPEVT9_Msk    /*!< USB Endpoint 9 Event \hideinitializer */
194 #define USBD_EPINTSTS_EP10      USBD_EPINTSTS_EPEVT10_Msk   /*!< USB Endpoint 10 Event \hideinitializer */
195 #define USBD_EPINTSTS_EP11      USBD_EPINTSTS_EPEVT11_Msk   /*!< USB Endpoint 11 Event \hideinitializer */
196 #define USBD_EPINTSTS_EP12      USBD_EPINTSTS_EPEVT12_Msk   /*!< USB Endpoint 12 Event \hideinitializer */
197 #define USBD_EPINTSTS_EP13      USBD_EPINTSTS_EPEVT13_Msk   /*!< USB Endpoint 13 Event \hideinitializer */
198 #define USBD_EPINTSTS_EP14      USBD_EPINTSTS_EPEVT14_Msk   /*!< USB Endpoint 14 Event \hideinitializer */
199 #define USBD_EPINTSTS_EP15      USBD_EPINTSTS_EPEVT15_Msk   /*!< USB Endpoint 15 Event \hideinitializer */
200 #define USBD_EPINTSTS_EP16      USBD_EPINTSTS_EPEVT16_Msk   /*!< USB Endpoint 16 Event \hideinitializer */
201 #define USBD_EPINTSTS_EP17      USBD_EPINTSTS_EPEVT17_Msk   /*!< USB Endpoint 17 Event \hideinitializer */
202 #define USBD_EPINTSTS_EP18      USBD_EPINTSTS_EPEVT18_Msk   /*!< USB Endpoint 18 Event \hideinitializer */
203 
204 #define USBD_STATE_USBRST       USBD_ATTR_USBRST_Msk        /*!< USB Bus Reset \hideinitializer */
205 #define USBD_STATE_SUSPEND      USBD_ATTR_SUSPEND_Msk       /*!< USB Bus Suspend \hideinitializer */
206 #define USBD_STATE_RESUME       USBD_ATTR_RESUME_Msk        /*!< USB Bus Resume \hideinitializer */
207 #define USBD_STATE_TIMEOUT      USBD_ATTR_TOUT_Msk          /*!< USB Bus Timeout \hideinitializer */
208 #define USBD_STATE_L1SUSPEND    USBD_ATTR_L1SUSPEND_Msk     /*!< USB Bus L1SUSPEND \hideinitializer */
209 #define USBD_STATE_L1RESUME     USBD_ATTR_L1RESUME_Msk      /*!< USB Bus L1RESUME \hideinitializer */
210 
211 #define USBD_CFG_DB_EN          USBD_CFG_DBEN_Msk           /*!< Double Buffer Enable \hideinitializer */
212 #define USBD_CFG_DBTGACTIVE     USBD_CFG_DBTGACTIVE_Msk     /*!< Double Buffer Toggle Active \hideinitializer */
213 
214 #define USBD_CFGP_SSTALL        USBD_CFGP_SSTALL_Msk        /*!< Set Stall \hideinitializer */
215 #define USBD_CFG_CSTALL         USBD_CFG_CSTALL_Msk         /*!< Clear Stall \hideinitializer */
216 
217 #define USBD_CFG_EPMODE_DISABLE (0ul << USBD_CFG_STATE_Pos)/*!< Endpoint Disable \hideinitializer */
218 #define USBD_CFG_EPMODE_OUT     (1ul << USBD_CFG_STATE_Pos)/*!< Out Endpoint \hideinitializer */
219 #define USBD_CFG_EPMODE_IN      (2ul << USBD_CFG_STATE_Pos)/*!< In Endpoint \hideinitializer */
220 #define USBD_CFG_TYPE_ISO       (1ul << USBD_CFG_ISOCH_Pos) /*!< Isochronous \hideinitializer */
221 
222 
223 
224 /*@}*/ /* end of group USBD_EXPORTED_CONSTANTS */
225 
226 
227 /** @addtogroup USBD_EXPORTED_FUNCTIONS USBD Exported Functions
228   @{
229 */
230 /**
231   * @brief      Compare two input numbers and return maximum one.
232   *
233   * @param[in]  a   First number to be compared.
234   * @param[in]  b   Second number to be compared.
235   *
236   * @return     Maximum value between a and b.
237   *
238   * @details    If a > b, then return a. Otherwise, return b.
239   * \hideinitializer
240   */
241 #define USBD_Maximum(a,b)        ((a)>(b) ? (a) : (b))
242 
243 
244 /**
245   * @brief      Compare two input numbers and return minimum one
246   *
247   * @param[in]  a   First number to be compared
248   * @param[in]  b   Second number to be compared
249   *
250   * @return     Minimum value between a and b
251   *
252   * @details    If a < b, then return a. Otherwise, return b.
253   * \hideinitializer
254   */
255 #define USBD_Minimum(a,b)        ((a)<(b) ? (a) : (b))
256 
257 
258 /**
259   * @brief    Enable USB
260   *
261   * @param    None
262   *
263   * @return   None
264   *
265   * @details  To set USB ATTR control register to enable USB and PHY.
266   * \hideinitializer
267   */
268 #define USBD_ENABLE_USB()           ((uint32_t)(USBD->ATTR |= 0x7D0))
269 
270 /**
271   * @brief    Disable USB
272   *
273   * @param    None
274   *
275   * @return   None
276   *
277   * @details  To set USB ATTR control register to disable USB.
278   * \hideinitializer
279   */
280 #define USBD_DISABLE_USB()          ((uint32_t)(USBD->ATTR &= ~USBD_USB_EN))
281 
282 /**
283   * @brief    Enable USB PHY
284   *
285   * @param    None
286   *
287   * @return   None
288   *
289   * @details  To set USB ATTR control register to enable USB PHY.
290   * \hideinitializer
291   */
292 #define USBD_ENABLE_PHY()           ((uint32_t)(USBD->ATTR |= USBD_PHY_EN))
293 
294 /**
295   * @brief    Disable USB PHY
296   *
297   * @param    None
298   *
299   * @return   None
300   *
301   * @details  To set USB ATTR control register to disable USB PHY.
302   * \hideinitializer
303   */
304 #define USBD_DISABLE_PHY()          ((uint32_t)(USBD->ATTR &= ~USBD_PHY_EN))
305 
306 /**
307   * @brief    Enable SE0. Force USB PHY transceiver to drive SE0.
308   *
309   * @param    None
310   *
311   * @return   None
312   *
313   * @details  Set DRVSE0 bit of USB_DRVSE0 register to enable software-disconnect function. Force USB PHY transceiver to drive SE0 to bus.
314   * \hideinitializer
315   */
316 #define USBD_SET_SE0()              ((uint32_t)(USBD->SE0 |= USBD_DRVSE0))
317 
318 /**
319   * @brief    Disable SE0
320   *
321   * @param    None
322   *
323   * @return   None
324   *
325   * @details  Clear DRVSE0 bit of USB_DRVSE0 register to disable software-disconnect function.
326   * \hideinitializer
327   */
328 #define USBD_CLR_SE0()              ((uint32_t)(USBD->SE0 &= ~USBD_DRVSE0))
329 
330 /**
331   * @brief       Set USB device address
332   *
333   * @param[in]   addr The USB device address.
334   *
335   * @return      None
336   *
337   * @details     Write USB device address to USB_FADDR register.
338   * \hideinitializer
339   */
340 #define USBD_SET_ADDR(addr)         (USBD->FADDR = (addr))
341 
342 /**
343   * @brief    Get USB device address
344   *
345   * @param    None
346   *
347   * @return   USB device address
348   *
349   * @details  Read USB_FADDR register to get USB device address.
350   * \hideinitializer
351   */
352 #define USBD_GET_ADDR()             ((uint32_t)(USBD->FADDR))
353 
354 /**
355   * @brief      Enable USB interrupt function
356   *
357   * @param[in]  intr The combination of the specified interrupt enable bits.
358   *             Each bit corresponds to a interrupt enable bit.
359   *             This parameter decides which interrupts will be enabled.
360   *             (USBD_INT_SOF, USBD_INT_WAKEUP, USBD_INT_FLDET, USBD_INT_USB, USBD_INT_BUS)
361   *
362   * @return     None
363   *
364   * @details    Enable USB related interrupt functions specified by intr parameter.
365   * \hideinitializer
366   */
367 #define USBD_ENABLE_INT(intr)       (USBD->INTEN |= (intr))
368 
369 /**
370   * @brief    Get interrupt status
371   *
372   * @param    None
373   *
374   * @return   The value of USB_INTSTS register
375   *
376   * @details  Return all interrupt flags of USB_INTSTS register.
377   * \hideinitializer
378   */
379 #define USBD_GET_INT_FLAG()         ((uint32_t)(USBD->INTSTS))
380 
381 /**
382   * @brief      Clear USB interrupt flag
383   *
384   * @param[in]  flag The combination of the specified interrupt flags.
385   *             Each bit corresponds to a interrupt source.
386   *             This parameter decides which interrupt flags will be cleared.
387   *             (USBD_INT_SOF, USBD_INTSTS_WAKEUP, USBD_INTSTS_FLDET, USBD_INTSTS_BUS, USBD_INTSTS_USB)
388   *
389   * @return     None
390   *
391   * @details    Clear USB related interrupt flags specified by flag parameter.
392   * \hideinitializer
393   */
394 #define USBD_CLR_INT_FLAG(flag)     (USBD->INTSTS = (flag))
395 
396 /**
397   * @brief    Get endpoint interrupt status
398   *
399   * @param    None
400   *
401   * @return   The value of USB_EPINTSTS register
402   *
403   * @details  Return all endpoint interrupt flags of USB_EPINTSTS register.
404   * \hideinitializer
405   */
406 #define USBD_GET_EP_INT_FLAG()      ((uint32_t)(USBD->EPINTSTS))
407 
408 /**
409   * @brief      Clear USB endpoint interrupt flag
410   *
411   * @param[in]  flag The combination of the specified endpoint interrupt flags.
412   *             Each bit corresponds to a endpoint interrupt source.
413   *             This parameter decides which endpoint interrupt flags will be cleared.
414   *
415   * @return     None
416   *
417   * @details    Clear USB related interrupt flags specified by flag parameter.
418   * \hideinitializer
419   */
420 #define USBD_CLR_EP_INT_FLAG(flag)     (USBD->EPINTSTS = (flag))
421 
422 /**
423   * @brief    Get endpoint status
424   *
425   * @param    None
426   *
427   * @return   The value of USB_EPSTS register.
428   *
429   * @details  Return all endpoint status.
430   * \hideinitializer
431   */
432 #define USBD_GET_EP_FLAG()          ((uint32_t)(USBD->EPSTS))
433 
434 /**
435   * @brief    Get USB bus state
436   *
437   * @param    None
438   *
439   * @return   The value of USB_ATTR[13:12] and USB_ATTR[3:0].
440   *           Bit 0 indicates USB bus reset status.
441   *           Bit 1 indicates USB bus suspend status.
442   *           Bit 2 indicates USB bus resume status.
443   *           Bit 3 indicates USB bus time-out status.
444   *           Bit 12 indicates USB bus LPM L1 suspend status.
445   *           Bit 13 indicates USB bus LPM L1 resume status.
446   *
447   * @details  Return USB_ATTR[13:12] and USB_ATTR[3:0] for USB bus events.
448   * \hideinitializer
449   */
450 #define USBD_GET_BUS_STATE()        ((uint32_t)(USBD->ATTR & 0x300f))
451 
452 /**
453   * @brief    Check cable connection state
454   *
455   * @param    None
456   *
457   * @retval   0 USB cable is not attached.
458   * @retval   1 USB cable is attached.
459   *
460   * @details  Check the connection state by FLDET bit of USB_FLDET register.
461   * \hideinitializer
462   */
463 #define USBD_IS_ATTACHED()          ((uint32_t)(USBD->VBUSDET & USBD_VBUSDET_VBUSDET_Msk))
464 
465 /**
466   * @brief      Stop USB transaction of the specified endpoint ID
467   *
468   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
469   *
470   * @return     None
471   *
472   * @details    Write 1 to CLRRDY bit of USB_CFGPx register to stop USB transaction of the specified endpoint ID.
473   * \hideinitializer
474   */
475 #define USBD_STOP_TRANSACTION(ep)   (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_CLRRDY_Msk)
476 
477 /**
478   * @brief      Set USB DATA1 PID for the specified endpoint ID
479   *
480   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
481   *
482   * @return     None
483   *
484   * @details    Set DSQ_SYNC bit of USB_CFGx register to specify the DATA1 PID for the following IN token transaction.
485   *             Base on this setting, hardware will toggle PID between DATA0 and DATA1 automatically for IN token transactions.
486   * \hideinitializer
487   */
488 #define USBD_SET_DATA1(ep)          (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DSQSYNC_Msk)
489 
490 /**
491   * @brief      Set USB DATA0 PID for the specified endpoint ID
492   *
493   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 19.
494   *
495   * @return     None
496   *
497   * @details    Clear DSQ_SYNC bit of USB_CFGx register to specify the DATA0 PID for the following IN token transaction.
498   *             Base on this setting, hardware will toggle PID between DATA0 and DATA1 automatically for IN token transactions.
499   * \hideinitializer
500   */
501 #define USBD_SET_DATA0(ep)          (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DSQSYNC_Msk))
502 
503 /**
504   * @brief      Set USB payload size (IN data)
505   *
506   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
507   *
508   * @param[in]  size The transfer length.
509   *
510   * @return     None
511   *
512   * @details    This macro will write the transfer length to USB_MXPLDx register for IN data transaction.
513   * \hideinitializer
514   */
515 #define USBD_SET_PAYLOAD_LEN(ep, size)  (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))) = (size))
516 
517 /**
518   * @brief      Get USB payload size (OUT data)
519   *
520   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 endpoint ID. This parameter could be 0 ~ 18.
521   *
522   * @return     The value of USB_MXPLDx register.
523   *
524   * @details    Get the data length of OUT data transaction by reading USB_MXPLDx register.
525   * \hideinitializer
526   */
527 #define USBD_GET_PAYLOAD_LEN(ep)        ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))))
528 
529 /**
530   * @brief      Configure endpoint
531   *
532   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
533   *
534   * @param[in]  config The USB configuration.
535   *
536   * @return     None
537   *
538   * @details    This macro will write config parameter to USB_CFGx register of specified endpoint ID.
539   * \hideinitializer
540   */
541 #define USBD_CONFIG_EP(ep, config)      (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) = (config))
542 
543 /**
544   * @brief      Set USB endpoint buffer
545   *
546   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
547   *
548   * @param[in]  offset The SRAM offset.
549   *
550   * @return     None
551   *
552   * @details    This macro will set the SRAM offset for the specified endpoint ID.
553   * \hideinitializer
554   */
555 #define USBD_SET_EP_BUF_ADDR(ep, offset)    (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))) = (offset))
556 
557 /**
558   * @brief      Get the offset of the specified USB endpoint buffer
559   *
560   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
561   *
562   * @return     The offset of the specified endpoint buffer.
563   *
564   * @details    This macro will return the SRAM offset of the specified endpoint ID.
565   * \hideinitializer
566   */
567 #define USBD_GET_EP_BUF_ADDR(ep)        ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))))
568 
569 /**
570   * @brief       Set USB endpoint stall state
571   *
572   * @param[in]   ep  The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
573   *
574   * @return      None
575   *
576   * @details     Set USB endpoint stall state for the specified endpoint ID. Endpoint will respond STALL token automatically.
577   * \hideinitializer
578   */
579 #define USBD_SET_EP_STALL(ep)        (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0ul].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_SSTALL_Msk)
580 
581 /**
582   * @brief       Clear USB endpoint stall state
583   *
584   * @param[in]   ep  The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
585   *
586   * @return      None
587   *
588   * @details     Clear USB endpoint stall state for the specified endpoint ID. Endpoint will respond ACK/NAK token.
589   * \hideinitializer
590   */
591 #define USBD_CLR_EP_STALL(ep)        (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) &= ~USBD_CFGP_SSTALL_Msk)
592 
593 /**
594   * @brief       Get USB endpoint stall state
595   *
596   * @param[in]   ep  The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
597   *
598   * @retval      0      USB endpoint is not stalled.
599   * @retval      Others USB endpoint is stalled.
600   *
601   * @details     Get USB endpoint stall state of the specified endpoint ID.
602   * \hideinitializer
603   */
604 #define USBD_GET_EP_STALL(ep)        (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) & USBD_CFGP_SSTALL_Msk)
605 
606 /**
607   * @brief      Set USB double buffer mode for the specified endpoint ID
608   *
609   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
610   *
611   * @return     None
612   *
613   * @details    Set DBEN bit of USB_CFGx register to enable the double buffer mode of the specified endpoint ID.
614   * \hideinitializer
615   */
616 #define USBD_SET_DB_MODE(ep)          (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DBEN_Msk)
617 
618 /**
619   * @brief      Set USB single buffer mode for the specified endpoint ID
620   *
621   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
622   *
623   * @return     None
624   *
625   * @details    Clear DBEN bit of USB_CFGx register to enable the single buffer mode of the specified endpoint ID.
626   * \hideinitializer
627   */
628 #define USBD_SET_SB_MODE(ep)          (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DBEN_Msk))
629 
630 /**
631   * @brief      Get the buffer mode of the specified USB endpoint buffer
632   *
633   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
634   *
635   * @retval     0 USB is single buffer mode.
636   * @retval     1 USB is double buffer mode.
637   *
638   * @details    This macro will return the buffer mode of the specified endpoint ID.
639   * \hideinitializer
640   */
641 #define USBD_IS_DB_MODE(ep)           (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) & USBD_CFG_DBEN_Msk)
642 
643 /**
644   * @brief      Set to active in USB double buffer mode for the specified endpoint ID
645   *
646   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
647   *
648   * @return     None
649   *
650   * @details    Set DBTGACTIVE bit of USB_CFGx register for toggle active in the double buffer mode of the specified endpoint ID.
651   * \hideinitializer
652   */
653 #define USBD_SET_DB_ACTIVE(ep)        (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DBTGACTIVE_Msk)
654 
655 /**
656   * @brief      Set to inactive in USB double buffer mode for the specified endpoint ID
657   *
658   * @param[in]  ep The USB endpoint ID. M2L31 Series supports 19 hardware endpoint ID. This parameter could be 0 ~ 18.
659   *
660   * @return     None
661   *
662   * @details    Clear DBTGACTIVE bit of USB_CFGx register for toggle inactive in the double buffer mode of the specified endpoint ID.
663   * \hideinitializer
664   */
665 #define USBD_SET_DB_INACTIVE(ep)      (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DBTGACTIVE_Msk))
666 
667 /**
668   * @brief      To support byte access between USB SRAM and system SRAM
669   *
670   * @param[in]  dest Destination pointer.
671   *
672   * @param[in]  src  Source pointer.
673   *
674   * @param[in]  size Byte count.
675   *
676   * @return     None
677   *
678   * @details    This function will copy the number of data specified by size and src parameters to the address specified by dest parameter.
679   *
680   */
USBD_MemCopy(uint8_t dest[],uint8_t src[],uint32_t size)681 __STATIC_INLINE void USBD_MemCopy(uint8_t dest[], uint8_t src[], uint32_t size)
682 {
683     uint32_t volatile i=0ul;
684 
685     while(size--)
686     {
687         dest[i] = src[i];
688         i++;
689     }
690 }
691 
692 /**
693   * @brief       Set USB endpoint stall state
694   *
695   * @param[in]   epnum  USB endpoint number
696   *
697   * @return      None
698   *
699   * @details     Set USB endpoint stall state. Endpoint will respond STALL token automatically.
700   *
701   */
USBD_SetStall(uint8_t epnum)702 __STATIC_INLINE void USBD_SetStall(uint8_t epnum)
703 {
704     uint32_t u32CfgAddr;
705     uint32_t u32Cfg;
706     uint32_t i;
707 
708     for(i = 0ul; i < USBD_MAX_EP; i++)
709     {
710         u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
711         u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
712 
713         if((u32Cfg & 0xful) == epnum)
714         {
715             u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
716             u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
717 
718             *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg | USBD_CFGP_SSTALL);
719             break;
720         }
721     }
722 }
723 
724 /**
725   * @brief       Clear USB endpoint stall state
726   *
727   * @param[in]   epnum  USB endpoint number
728   *
729   * @return      None
730   *
731   * @details     Clear USB endpoint stall state. Endpoint will respond ACK/NAK token.
732   */
USBD_ClearStall(uint8_t epnum)733 __STATIC_INLINE void USBD_ClearStall(uint8_t epnum)
734 {
735     uint32_t u32CfgAddr;
736     uint32_t u32Cfg;
737     uint32_t i;
738 
739     for(i = 0ul; i < USBD_MAX_EP; i++)
740     {
741         u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
742         u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
743 
744         if((u32Cfg & 0xful) == epnum)
745         {
746             u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
747             u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
748 
749             *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg & ~USBD_CFGP_SSTALL);
750             break;
751         }
752     }
753 }
754 
755 /**
756   * @brief       Get USB endpoint stall state
757   *
758   * @param[in]   epnum  USB endpoint number
759   *
760   * @retval      0      USB endpoint is not stalled.
761   * @retval      Others USB endpoint is stalled.
762   *
763   * @details     Get USB endpoint stall state.
764   *
765   */
USBD_GetStall(uint8_t epnum)766 __STATIC_INLINE uint32_t USBD_GetStall(uint8_t epnum)
767 {
768     uint32_t u32CfgAddr;
769     uint32_t u32Cfg;
770     uint32_t i;
771 
772     for(i = 0ul; i < USBD_MAX_EP; i++)
773     {
774         u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */
775         u32Cfg = *((__IO uint32_t *)(u32CfgAddr));
776 
777         if((u32Cfg & 0xful) == epnum)
778         {
779             u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */
780             break;
781         }
782     }
783 
784     return ((*((__IO uint32_t *)(u32CfgAddr))) & USBD_CFGP_SSTALL);
785 }
786 
787 
788 extern volatile uint8_t g_usbd_RemoteWakeupEn;
789 
790 
791 typedef void (*VENDOR_REQ)(void);           /*!< Functional pointer type definition for Vendor class */
792 typedef void (*CLASS_REQ)(void);            /*!< Functional pointer type declaration for USB class request callback handler */
793 typedef void (*SET_INTERFACE_REQ)(uint32_t u32AltInterface);    /*!< Functional pointer type declaration for USB set interface request callback handler */
794 typedef void (*SET_CONFIG_CB)(void);       /*!< Functional pointer type declaration for USB set configuration request callback handler */
795 
796 
797 /*--------------------------------------------------------------------*/
798 void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface);
799 void USBD_Start(void);
800 void USBD_GetSetupPacket(uint8_t *buf);
801 void USBD_ProcessSetupPacket(void);
802 void USBD_StandardRequest(void);
803 void USBD_PrepareCtrlIn(uint8_t pu8Buf[], uint32_t u32Size);
804 void USBD_CtrlIn(void);
805 void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size);
806 void USBD_CtrlOut(void);
807 void USBD_SwReset(void);
808 void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq);
809 void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback);
810 void USBD_LockEpStall(uint32_t u32EpBitmap);
811 
812 /*@}*/ /* end of group USBD_EXPORTED_FUNCTIONS */
813 
814 /*@}*/ /* end of group USBD_Driver */
815 
816 /*@}*/ /* end of group Standard_Driver */
817 
818 #ifdef __cplusplus
819 }
820 #endif
821 
822 #endif /*__USBD_H__*/
823 
824 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
825