1 /*!
2  * @file        apm32e10x_usb.h
3  *
4  * @brief       This file contains all the prototypes,enumeration and macros for USBD peripheral
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-12-31
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2023 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 /* Define to prevent recursive inclusion */
27 #ifndef __APM32E10X_USBD_H_
28 #define __APM32E10X_USBD_H_
29 
30 /* Includes */
31 #include "apm32e10x.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 /** @addtogroup APM32E10x_StdPeriphDriver
38   @{
39 */
40 
41 /** @addtogroup USBD_Driver
42   @{
43 */
44 
45 /** @defgroup USBD_Macros Macros
46   @{
47 */
48 
49 /* USBD packet memory area base address */
50 #define USBD_PMA_ADDR            (0x40006000L)
51 
52 /* Endpoint register mask value default */
53 #define USBD_EP_MASK_DEFAULT     (USBD_EP_BIT_CTFR | USBD_EP_BIT_SETUP | USBD_EP_BIT_TYPE | USBD_EP_BIT_KIND | USBD_EP_BIT_CTFT |USBD_EP_BIT_ADDR)
54 
55 /**
56  * @brief   USBD interrupt source
57  */
58 #define USBD_INT_ESOF            0X100
59 #define USBD_INT_SOF             0X200
60 #define USBD_INT_RST             0X400
61 #define USBD_INT_SUS             0x800
62 #define USBD_INT_WKUP            0X1000
63 #define USBD_INT_ERR             0X2000
64 #define USBD_INT_PMAOU           0X4000
65 #define USBD_INT_CTR             0X8000
66 #define USBD_INT_ALL             0XFF00
67 
68 /*!
69  * @brief       Set CTRL register
70  *
71  * @param       val:    Register value
72  *
73  * @retval      None
74  *
75  */
76 #define USBD_SetRegCTRL(val)         (USBD->CTRL = val)
77 
78 /*!
79  * @brief       Set INTSTS register
80  *
81  * @param       val:    Register value
82  *
83  * @retval      None
84  */
85 #define USBD_SetRegINTSTS(val)       (USBD->INTSTS = val)
86 
87 /*!
88  * @brief       Set force reset
89  *
90  * @param       None
91  *
92  * @retval      None
93  */
94 #define USBD_SetForceReset()         (USBD->CTRL_B.FORRST = BIT_SET)
95 
96 /*!
97  * @brief       Reset force reset
98  *
99  * @param       None
100  *
101  * @retval      None
102  */
103 #define USBD_ResetForceReset()       (USBD->CTRL_B.FORRST = BIT_RESET)
104 
105 /*!
106  * @brief       Set power down
107  *
108  * @param       None
109  *
110  * @retval      None
111  */
112 #define USBD_SetPowerDown()          (USBD->CTRL_B.PWRDOWN = BIT_SET)
113 
114 /*!
115  * @brief       Reset power down
116  *
117  * @param       None
118  *
119  * @retval      None
120  */
121 #define USBD_ResetPowerDown()        (USBD->CTRL_B.PWRDOWN = BIT_RESET)
122 
123 /*!
124  * @brief       Set low power mode
125  *
126  * @param       None
127  *
128  * @retval      None
129  */
130 #define USBD_SetLowerPowerMode()     (USBD->CTRL_B.LPWREN = BIT_SET)
131 
132 /*!
133  * @brief       Ret low power mode
134  *
135  * @param       None
136  *
137  * @retval      None
138  */
139 #define USBD_ResetLowerPowerMode()   (USBD->CTRL_B.LPWREN = BIT_RESET)
140 
141 /*!
142  * @brief       Set force suspend
143  *
144  * @param       None
145  *
146  * @retval      None
147  */
148 #define USBD_SetForceSuspend()       (USBD->CTRL_B.FORSUS = BIT_SET)
149 
150 /*!
151  * @brief       Reset force suspend
152  *
153  * @param       None
154  *
155  * @retval      None
156  */
157 #define USBD_ResetForceSuspend()     (USBD->CTRL_B.FORSUS = BIT_RESET)
158 
159 /*!
160  * @brief       Read force suspend status
161  *
162  * @param       None
163  *
164  * @retval      None
165  */
166 #define USBD_ReadForceSuspend()      (USBD->CTRL_B.FORSUS)
167 
168 /*!
169  * @brief       Set resume
170  *
171  * @param       None
172  *
173  * @retval      None
174  */
175 #define USBD_SetResume()             (USBD->CTRL_B.WUPREQ = BIT_SET)
176 
177 /*!
178  * @brief       Reset resume
179  *
180  * @param       None
181  *
182  * @retval      None
183  */
184 #define USBD_ResetResume()           (USBD->CTRL_B.WUPREQ = BIT_RESET)
185 
186 /*!
187  * @brief       Enable interrupt
188  *
189  * @param       int:    Interrupt source
190  *
191  * @retval      None
192  */
193 #define USBD_EnableInterrupt(int)    (USBD->CTRL |= int)
194 
195 /*!
196  * @brief       Disable interrupt
197  *
198  * @param       int:    Interrupt source
199  *
200  * @retval      None
201  */
202 #define USBD_DisableInterrupt(int)   (USBD->CTRL &= (uint32_t)~int)
203 
204 /*!
205  * @brief       Read the specified interrupt flag status
206  *
207  * @param       int:    Interrupt source
208  *
209  * @retval      Flag status.0 or not 0
210  */
211 #define USBD_ReadIntFlag(int)        (USBD->INTSTS & int)
212 
213 /*!
214  * @brief       Clear the specified interrupt flag status
215  *
216  * @param       int:    Interrupt source
217  *
218  * @retval      None
219  */
220 #define USBD_ClearIntFlag(int)       (USBD->INTSTS &= (uint32_t)~int)
221 
222 /*!
223  * @brief       Read DOT field value in INTSTS rigister
224  *
225  * @param       None
226  *
227  * @retval      DOT field value
228  */
229 #define USBD_ReadDir()               (USBD->INTSTS_B.DOT)
230 
231 /*!
232  * @brief       Read EPID field value in INTSTS rigister
233  *
234  * @param       None
235  *
236  * @retval      EPIDfield value
237  */
238 #define USBD_ReadEP()                ((USBD_EP_T)(USBD->INTSTS_B.EPID))
239 
240 /*!
241  * @brief       Read EP type
242  *
243  * @param       ep:     EP number
244  *
245  * @retval      EP type
246  */
247 #define USBD_ReadEPType(ep)          (USBD->EP[ep].EP_B.TYPE)
248 
249 /*!
250  * @brief       Read EP Tx status
251  *
252  * @param       ep:     EP number
253  *
254  * @retval      EP Tx status
255  */
256 #define USBD_ReadEPTxStatus(ep)      ((USBD_EP_STATUS_T)(USBD->EP[ep].EP_B.TXSTS))
257 
258 /*!
259  * @brief       Read EP Rx status
260  *
261  * @param       ep:     EP number
262  *
263  * @retval      EP Rx status
264  */
265 #define USBD_ReadEPRxStatus(ep)      ((USBD_EP_STATUS_T)(USBD->EP[ep].EP_B.RXSTS))
266 
267 /*!
268  * @brief       Read EP Tx address pointer
269  *
270  * @param       ep:     EP number
271  *
272  * @retval      EP Tx address pointer
273  */
274 #define USBD_ReadEPTxAddrPointer(ep) (uint32_t *)((USBD->BUFFTB + ep * 8) * 2 + USBD_PMA_ADDR)
275 
276 
277 /*!
278  * @brief       Read EP Tx count pointer
279  *
280  * @param       ep:     EP number
281  *
282  * @retval      EP Tx count pointer
283  */
284 #define USBD_ReadEPTxCntPointer(ep)  (uint32_t *)((USBD->BUFFTB + ep * 8 + 2) * 2 + USBD_PMA_ADDR)
285 
286 /*!
287  * @brief       Read EP Rx address pointer
288  *
289  * @param       ep:     EP number
290  *
291  * @retval      EP Rx address pointer
292  */
293 #define USBD_ReadEPRxAddrPointer(ep) (uint32_t *)((USBD->BUFFTB + ep * 8 + 4) * 2 + USBD_PMA_ADDR)
294 
295 /*!
296  * @brief       Read EP Rx count pointer
297  *
298  * @param       ep:     EP number
299  *
300  * @retval      EP Rx count pointer
301  */
302 #define USBD_ReadEPRxCntPointer(ep)  (uint32_t *)((USBD->BUFFTB + ep * 8 + 6) * 2 + USBD_PMA_ADDR)
303 
304 /*!
305  * @brief       Set EP Tx addr
306  *
307  * @param       ep:     EP number
308  *
309  * @param       addr:   Tx addr
310  *
311  * @retval      None
312  */
313 #define USBD_SetEPTxAddr(ep, addr)   (*USBD_ReadEPTxAddrPointer(ep) = (addr >> 1) << 1)
314 
315 /*!
316  * @brief       Set EP Rx addr
317  *
318  * @param       ep:     EP number
319  *
320  * @param       addr:   Rx addr
321  *
322  * @retval      None
323  */
324 #define USBD_SetEPRxAddr(ep, addr)   (*USBD_ReadEPRxAddrPointer(ep) = (addr >> 1) << 1)
325 
326 /*!
327  * @brief       Read EP Tx addr
328  *
329  * @param       ep:     EP number
330  *
331  * @retval      EP Tx addr
332  */
333 #define USBD_ReadEPTxAddr(ep)        ((uint16_t)*USBD_ReadEPTxAddrPointer(ep))
334 
335 /*!
336  * @brief       Read EP Rx addr
337  *
338  * @param       ep:     EP number
339  *
340  * @retval      EP Rx addr
341  */
342 #define USBD_ReadEPRxAddr(ep)        ((uint16_t)*USBD_ReadEPRxAddrPointer(ep))
343 
344 /*!
345  * @brief       Set EP Rx Count
346  *
347  * @param       ep:     EP number
348  *
349  * @param       cnt:    Tx count
350  *
351  * @retval      None
352  */
353 #define USBD_SetEPTxCnt(ep, cnt)     (*USBD_ReadEPTxCntPointer(ep) = cnt)
354 
355 /*!
356  * @brief       Read EP Tx count
357  *
358  * @param       ep:     EP number
359  *
360  * @retval      EP Tx count
361  */
362 #define USBD_ReadEPTxCnt(ep)        ((uint16_t)*USBD_ReadEPTxCntPointer(ep) & 0x3ff)
363 
364 /*!
365  * @brief       Read EP Rx count
366  *
367  * @param       ep:     EP number
368  *
369  * @retval      EP Rx count
370  */
371 #define USBD_ReadEPRxCnt(ep)        ((uint16_t)*USBD_ReadEPRxCntPointer(ep) & 0x3ff)
372 
373 /*!
374  * @brief       Read SETUP field value in EP register
375  *
376  * @param       ep:     EP number
377  *
378  * @retval      SETUP field value
379  */
380 #define USBD_ReadEPSetup(ep)         (USBD->EP[ep].EP_B.SETUP)
381 
382 /*!
383  * @brief       Set buffer table value
384  *
385  * @param       tab:    Buffer table value
386  *
387  * @retval      None
388  */
389 #define USBD_SetBufferTable(tab)     (USBD->BUFFTB_B.BUFFTB = tab)
390 
391 /*!
392  * @brief       Set device address
393  *
394  * @param       addr:   Device address
395  *
396  * @retval      None
397  */
398 #define USBD_SetDeviceAddr(addr)     (USBD->ADDR_B.ADDR = addr)
399 
400 /*!
401  * @brief       Read CTFR field value in EP register
402  *
403  * @param       ep: Endpoint number
404  *
405  * @retval      CTFR field value
406  */
407 #define USBD_ReadEPRxFlag(ep)        (USBD->EP[ep].EP_B.CTFR)
408 
409 /*!
410  * @brief       Read CTFT field value in EP register
411  *
412  * @param       ep: Endpoint number
413  *
414  * @retval      CTFT field value
415  */
416 #define USBD_ReadEPTxFlag(ep)        (USBD->EP[ep].EP_B.CTFT)
417 
418 /*!
419  * @brief       Enable USBD peripheral
420  *
421  * @param       None
422  *
423  * @retval      None
424  */
425 #define USBD_Enable()                (USBD->ADDR_B.USBDEN = BIT_SET)
426 
427 /*!
428  * @brief       Disable USBD peripheral
429  *
430  * @param       None
431  *
432  * @retval      None
433  */
434 #define USBD_Disable()               (USBD->ADDR_B.USBDEN = BIT_RESET)
435 
436 /*!
437  * @brief       Enable USBD2 peripheral
438  *
439  * @param       None
440  *
441  * @retval      None
442  */
443 #define USBD2_Enable()               (USBD->USB_SWITCH = BIT_SET)
444 
445 /*!
446  * @brief       Disable USBD2 peripheral
447  *
448  * @param       None
449  *
450  * @retval      None
451  */
452 #define USBD2_Disable()              (USBD->USB_SWITCH = BIT_RESET)
453 
454 /*!
455  * @brief       Read RXDPSTS field value in FRANUM register
456  *
457  * @param       None
458  *
459  * @retval      RXDPSTS field value
460  */
461 #define USBD_ReadRDPS()              (USBD->FRANUM_B.RXDPSTS)
462 
463 /*!
464  * @brief       Read RXDMSTS field value in FRANUM register
465  *
466  * @param       None
467  *
468  * @retval      RXDMSTS field value
469  */
470 #define USBD_ReadRDMS()              (USBD->FRANUM_B.RXDMSTS)
471 
472 /*!
473  * @brief       Read LOCK field value in FRANUM register
474  *
475  * @param       None
476  *
477  * @retval      LOCK field value
478  */
479 #define USBD_ReadLOCK()              (USBD->FRANUM_B.LOCK)
480 
481 /*!
482  * @brief       Read LSOFNUM field value in FRANUM register
483  *
484  * @param       None
485  *
486  * @retval      LSOFNUM field value
487  */
488 #define USBD_ReadLSOF()              (USBD->FRANUM_B.LSOFNUM)
489 
490 /*!
491  * @brief       Read FRANUM field value in FRANUM register
492  *
493  * @param       None
494  *
495  * @retval      FRANUM field value
496  */
497 #define USBD_ReadFRANUM()              (USBD->FRANUM_B.FRANUM)
498 
499 /**@} end of group USBD_Macros */
500 
501 /** @defgroup USBD_Enumerations Enumerations
502   @{
503 */
504 
505 /**
506  * @brief   USBD Endpoint register bit definition
507  */
508 typedef enum
509 {
510     USBD_EP_BIT_ADDR     = (uint32_t)(BIT0 | BIT1 | BIT2 | BIT3),
511     USBD_EP_BIT_TXSTS    = (uint32_t)(BIT4 | BIT5),
512     USBD_EP_BIT_TXDTOG   = (uint32_t)(BIT6),
513     USBD_EP_BIT_CTFT     = (uint32_t)(BIT7),
514     USBD_EP_BIT_KIND     = (uint32_t)(BIT8),
515     USBD_EP_BIT_TYPE     = (uint32_t)(BIT9 | BIT10),
516     USBD_EP_BIT_SETUP    = (uint32_t)(BIT11),
517     USBD_EP_BIT_RXSTS    = (uint32_t)(BIT12 | BIT13),
518     USBD_EP_BIT_RXDTOG   = (uint32_t)(BIT14),
519     USBD_EP_BIT_CTFR     = (uint32_t)(BIT15)
520 }USBD_EP_BIT_T;
521 
522 /**
523  * @brief   Endpoint id
524  */
525 typedef enum
526 {
527     USBD_EP_0,
528     USBD_EP_1,
529     USBD_EP_2,
530     USBD_EP_3,
531     USBD_EP_4,
532     USBD_EP_5,
533     USBD_EP_6,
534     USBD_EP_7
535 }USBD_EP_T;
536 
537 /**
538  * @brief   Endpoint status
539  */
540 typedef enum
541 {
542     USBD_EP_STATUS_DISABLE   = ((uint32_t)0),
543     USBD_EP_STATUS_STALL     = ((uint32_t)1),
544     USBD_EP_STATUS_NAK       = ((uint32_t)2),
545     USBD_EP_STATUS_VALID     = ((uint32_t)3)
546 }USBD_EP_STATUS_T;
547 
548 /**
549  * @brief   USBD Endpoint type
550  */
551 typedef enum
552 {
553     USBD_EP_TYPE_BULK,
554     USBD_EP_TYPE_CONTROL,
555     USBD_EP_TYPE_ISO,
556     USBD_EP_TYPE_INTERRUPT
557 }USBD_EP_TYPE_T;
558 
559 /**@} end of group USBD_Enumerations */
560 
561 /** @defgroup USBD_Functions Functions
562   @{
563 */
564 
565 void USBD_SetEPType(USBD_EP_T ep, USBD_EP_TYPE_T type);
566 
567 void USBD_SetEPKind(USBD_EP_T ep);
568 void USBD_ResetEPKind(USBD_EP_T ep);
569 
570 void USBD_ResetEPRxFlag(USBD_EP_T ep);
571 void USBD_ResetEPTxFlag(USBD_EP_T ep);
572 
573 void USBD_ToggleTx(USBD_EP_T ep);
574 void USBD_ToggleRx(USBD_EP_T ep);
575 void USBD_ResetTxToggle(USBD_EP_T ep);
576 void USBD_ResetRxToggle(USBD_EP_T ep);
577 
578 void USBD_SetEpAddr(USBD_EP_T ep, uint8_t addr);
579 
580 void USBD_SetEPTxStatus(USBD_EP_T ep, USBD_EP_STATUS_T status);
581 void USBD_SetEPRxStatus(USBD_EP_T ep, USBD_EP_STATUS_T status);
582 void USBD_SetEPRxTxStatus(USBD_EP_T ep, USBD_EP_STATUS_T txStatus, USBD_EP_STATUS_T rxStatus);
583 
584 void USBD_SetEPRxCnt(USBD_EP_T ep, uint32_t cnt);
585 
586 void USBD_WriteDataToEP(USBD_EP_T ep, uint8_t *wBuf, uint32_t wLen);
587 void USBD_ReadDataFromEP(USBD_EP_T ep, uint8_t *rBuf, uint32_t rLen);
588 
589 /**@} end of group USBD_Functions */
590 /**@} end of group USBD_Driver */
591 /**@} end of group APM32E10x_StdPeriphDriver */
592 
593 #ifdef __cplusplus
594 }
595 #endif
596 
597 #endif /* __APM32E10X_USBD_H */
598