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