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