1 /**
2   ******************************************************************************
3   * @file    tae32f53xx_ll_usb.c
4   * @author  MCD Application Team
5   * @brief   USB LL Module Driver.
6   *
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
11   * All rights reserved.</center></h2>
12   *
13   * This software is licensed by Tai-Action under BSD 3-Clause license,
14   * the "License"; You may not use this file except in compliance with the
15   * License. You may obtain a copy of the License at:
16   *                        opensource.org/licenses/BSD-3-Clause
17   *
18   ******************************************************************************
19   */
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "tae32f53xx_ll.h"
23 
24 
25 #define DBG_TAG             "USB LL"
26 #define DBG_LVL             DBG_ERROR
27 #include "dbg/tae32f53xx_dbg.h"
28 
29 
30 /** @addtogroup TAE32F53xx_LL_Driver
31   * @{
32   */
33 
34 /** @defgroup USB_LL USB LL
35   * @brief    USB LL Module Driver.
36   * @{
37   */
38 
39 
40 #ifdef LL_USB_MODULE_ENABLED
41 
42 
43 /* Private typedef -----------------------------------------------------------*/
44 /* Private define ------------------------------------------------------------*/
45 /* Private macro -------------------------------------------------------------*/
46 /* Private variables ---------------------------------------------------------*/
47 /* Private function prototypes -----------------------------------------------*/
48 /* Exported functions --------------------------------------------------------*/
49 /** @defgroup USB_LL_Exported_Functions USB LL Exported Functions
50   * @brief    USB LL Exported Functions
51   * @{
52   */
53 
54 /** @defgroup USB_LL_Exported_Functions_Group1 Initialization and de-initialization functions
55   * @brief    Initialization and Configuration functions
56   *
57   @verbatim
58   ===============================================================================
59               ##### Initialization and de-initialization functions #####
60   ===============================================================================
61    [..]
62      This section provides functions allowing to initialize and de-initialize the USB
63      to be ready for use.
64 
65   @endverbatim
66   * @{
67   */
68 
69 /**
70   * @brief  Initializes the USB peripheral
71   * @param  Instance Specifies USB peripheral
72   * @return Status of the Initialization
73   */
LL_USB_Init(USB_TypeDef * Instance)74 LL_StatusETypeDef LL_USB_Init(USB_TypeDef *Instance)
75 {
76     /* Init the low level hardware eg. Clock, NVIC */
77     LL_USB_MspInit(Instance);
78 
79     //USB Hardware Config
80     __LL_USB_DMOutputHardware(Instance);
81     __LL_USB_DPOutputHardware(Instance);
82     __LL_USB_DMInputHardware(Instance);
83     __LL_USB_DPInputHardware(Instance);
84 
85     __LL_USB_DMPullDownDisable(Instance);
86     __LL_USB_DMPullUpNormal(Instance);
87     __LL_USB_DPPullDownDisable(Instance);
88     __LL_USB_DPPullUpNormal(Instance);
89 
90     __LL_USB_VbusValidThreshold_Set(Instance);
91     __LL_USB_VbusAboveAdevSessThres_Set(Instance);
92     __LL_USB_VbusAboveSessEndThres_Set(Instance);
93     __LL_USB_MiniABConnectorID_Set(Instance);
94     __LL_USB_PHY_En(Instance);
95 
96     //USB Power Control
97     __LL_USB_SoftConn_En(Instance);
98     __LL_USB_HighSpeed_Dis(Instance);
99     __LL_USB_SuspendDMOut_En(Instance);
100 
101     //USB Detect Interrupt Config
102     __LL_USB_DebouceMax_Set(Instance, 0x80);
103     __LL_USB_Conn_Clr(Instance);
104     __LL_USB_Conn_Int_En(Instance);
105     __LL_USB_Disconn_Clr(Instance);
106     __LL_USB_Disconn_Int_En(Instance);
107 
108     //USB Interrupt Enable
109     __LL_USB_Int_EN(Instance, USB_CTRL_INT_ALL_Msk);
110     __LL_USB_EP0AndEPxTX_Int_EN(Instance, EP_NUM_0);
111 
112     //USB INT Send To CPU Config
113     __LL_USB_INTSendToCPU_En(Instance, USB_CTRL_INT_TO_CPU_Msk);
114     __LL_USB_INTSendToCPU_En(Instance, USB_EP_INT_TO_CPU_Msk);
115     __LL_USB_INTSendToCPU_En(Instance, USB_LPM_INT_TO_CPU_Msk);
116 
117     return LL_OK;
118 }
119 
120 /**
121   * @brief  DeInitializes the USB peripheral
122   * @param  Instance Specifies USB peripheral
123   * @return Status of the Initialization
124   */
LL_USB_DeInit(USB_TypeDef * Instance)125 LL_StatusETypeDef LL_USB_DeInit(USB_TypeDef *Instance)
126 {
127     //USB INT Send To CPU Config
128     __LL_USB_INTSendToCPU_Dis(Instance, USB_CTRL_INT_TO_CPU_Msk);
129     __LL_USB_INTSendToCPU_Dis(Instance, USB_EP_INT_TO_CPU_Msk);
130     __LL_USB_INTSendToCPU_Dis(Instance, USB_LPM_INT_TO_CPU_Msk);
131 
132     //USB Interrupt Disable
133     __LL_USB_Int_Dis(Instance, USB_CTRL_INT_ALL_Msk);
134     __LL_USB_EP0AndEPxTX_Int_Dis(Instance, EP_NUM_0);
135 
136     //USB Detect Interrupt Config
137     __LL_USB_Conn_Clr(Instance);
138     __LL_USB_Conn_Int_Dis(Instance);
139     __LL_USB_Disconn_Clr(Instance);
140     __LL_USB_Disconn_Int_Dis(Instance);
141 
142     //USB Power Control
143     __LL_USB_SoftConn_Dis(Instance);
144     __LL_USB_SuspendDMOut_Dis(Instance);
145 
146     //USB Hardware Config
147     __LL_USB_DMOutputNormal(Instance);
148     __LL_USB_DPOutputNormal(Instance);
149     __LL_USB_DMInputNormal(Instance);
150     __LL_USB_DPInputNormal(Instance);
151     __LL_USB_DMPullDownNormal(Instance);
152     __LL_USB_DMPullUpNormal(Instance);
153     __LL_USB_DPPullDownNormal(Instance);
154     __LL_USB_DPPullUpNormal(Instance);
155 
156     __LL_USB_VbusValidThreshold_Clr(Instance);
157     __LL_USB_VbusAboveAdevSessThres_Clr(Instance);
158     __LL_USB_VbusAboveSessEndThres_Clr(Instance);
159     __LL_USB_MiniABConnectorID_Clr(Instance);
160     __LL_USB_PHY_Dis(Instance);
161 
162     /* DeInit the low level hardware eg. Clock, NVIC */
163     LL_USB_MspDeInit(Instance);
164 
165     return LL_OK;
166 }
167 
168 /**
169   * @brief  Initializes the USB MSP.
170   * @param  Instance Specifies USB peripheral
171   * @return None
172   */
LL_USB_MspInit(USB_TypeDef * Instance)173 __WEAK void LL_USB_MspInit(USB_TypeDef *Instance)
174 {
175     /* Prevent unused argument(s) compilation warning */
176     LL_UNUSED(Instance);
177     /* NOTE: This function should not be modified, when the callback is needed,
178              the LL_USB_MspInit could be implemented in the user file
179      */
180 }
181 
182 /**
183   * @brief  DeInitializes the USB MSP
184   * @param  Instance Specifies USB peripheral
185   * @return None
186   */
LL_USB_MspDeInit(USB_TypeDef * Instance)187 __WEAK void LL_USB_MspDeInit(USB_TypeDef *Instance)
188 {
189     /* Prevent unused argument(s) compilation warning */
190     LL_UNUSED(Instance);
191     /* NOTE: This function should not be modified, when the callback is needed,
192              the LL_USB_MspDeInit could be implemented in the user file
193      */
194 }
195 /**
196   * @}
197   */
198 
199 
200 /** @defgroup USB_LL_Exported_Functions_Group2 USB Controler Initerrupt Management
201   * @brief    USB Controler Initerrupt Management
202     *
203   @verbatim
204   ==============================================================================
205                         ##### IRQ handler management #####
206   ==============================================================================
207   [..]
208     This section provides USB Controler IRQ handler function.
209   @endverbatim
210   * @{
211   */
212 
213 /**
214   * @brief  LL USB Controler IRQ Handler
215   * @param  Instance Specifies USB peripheral
216   * @return None
217   */
LL_USB_CtrlIRQHandler(USB_TypeDef * Instance)218 void LL_USB_CtrlIRQHandler(USB_TypeDef *Instance)
219 {
220     uint8_t usb_ctrl_int_sta = __LL_USB_IntSta_Get(Instance);
221 
222     if (usb_ctrl_int_sta & USB_SUSPEND_INT_STA_Msk) {
223         LL_USB_CtrlSuspendCallback(Instance);
224     }
225 
226     if (usb_ctrl_int_sta & USB_RESUME_INT_STA_Msk) {
227         LL_USB_CtrlResumeCallback(Instance);
228     }
229 
230     if (usb_ctrl_int_sta & USB_RST_INT_STA_Msk) {
231         LL_USB_CtrlResetCallback(Instance);
232     }
233 
234     if (usb_ctrl_int_sta & USB_SOF_INT_STA_Msk) {
235         LL_USB_CtrlSofCallback(Instance);
236     }
237 
238     if (usb_ctrl_int_sta & USB_SESS_END_INT_STA_Msk) {
239         LL_USB_CtrlSessEndCallback(Instance);
240     }
241 }
242 
243 /**
244   * @brief  USB Controler Suspend Callback
245   * @param  Instance Specifies USB peripheral
246   * @return None
247   */
LL_USB_CtrlSuspendCallback(USB_TypeDef * Instance)248 __WEAK void LL_USB_CtrlSuspendCallback(USB_TypeDef *Instance)
249 {
250     /* Prevent unused argument(s) compilation warning */
251     LL_UNUSED(Instance);
252     /* NOTE: This function should not be modified, when the callback is needed,
253              the LL_USB_CtrlSuspendCallback could be implemented in the USB Middleware file
254      */
255 }
256 
257 /**
258   * @brief  USB Controler Resume Callback
259   * @param  Instance Specifies USB peripheral
260   * @return None
261   */
LL_USB_CtrlResumeCallback(USB_TypeDef * Instance)262 __WEAK void LL_USB_CtrlResumeCallback(USB_TypeDef *Instance)
263 {
264     /* Prevent unused argument(s) compilation warning */
265     LL_UNUSED(Instance);
266     /* NOTE: This function should not be modified, when the callback is needed,
267              the LL_USB_CtrlResumeCallback could be implemented in the USB Middleware file
268      */
269 }
270 
271 /**
272   * @brief  USB Controler Reset Callback
273   * @param  Instance Specifies USB peripheral
274   * @return None
275   */
LL_USB_CtrlResetCallback(USB_TypeDef * Instance)276 __WEAK void LL_USB_CtrlResetCallback(USB_TypeDef *Instance)
277 {
278     /* Prevent unused argument(s) compilation warning */
279     LL_UNUSED(Instance);
280     /* NOTE: This function should not be modified, when the callback is needed,
281              the LL_USB_CtrlResetCallback could be implemented in the USB Middleware file
282      */
283 }
284 
285 /**
286   * @brief  USB Controler SOF Callback
287   * @param  Instance Specifies USB peripheral
288   * @return None
289   */
LL_USB_CtrlSofCallback(USB_TypeDef * Instance)290 __WEAK void LL_USB_CtrlSofCallback(USB_TypeDef *Instance)
291 {
292     /* Prevent unused argument(s) compilation warning */
293     LL_UNUSED(Instance);
294     /* NOTE: This function should not be modified, when the callback is needed,
295              the LL_USB_CtrlSofCallback could be implemented in the USB Middleware file
296      */
297 }
298 
299 /**
300   * @brief  USB Controler Session End Callback
301   * @param  Instance Specifies USB peripheral
302   * @return None
303   */
LL_USB_CtrlSessEndCallback(USB_TypeDef * Instance)304 __WEAK void LL_USB_CtrlSessEndCallback(USB_TypeDef *Instance)
305 {
306     /* Prevent unused argument(s) compilation warning */
307     LL_UNUSED(Instance);
308     /* NOTE: This function should not be modified, when the callback is needed,
309              the LL_USB_CtrlSessEndCallback could be implemented in the USB Middleware file
310      */
311 }
312 /**
313   * @}
314   */
315 
316 
317 /** @defgroup USB_LL_Exported_Functions_Group3 USB Detect Initerrupt Management
318   * @brief    USB Detect Initerrupt Management
319     *
320   @verbatim
321   ==============================================================================
322                         ##### IRQ handler management #####
323   ==============================================================================
324   [..]
325     This section provides USB Detect IRQ handler function.
326   @endverbatim
327   * @{
328   */
329 
330 /**
331   * @brief  LL USB Detect IRQ Handler
332   * @param  Instance Specifies USB peripheral
333   * @return None
334   */
LL_USB_DetIRQHandler(USB_TypeDef * Instance)335 void LL_USB_DetIRQHandler(USB_TypeDef *Instance)
336 {
337     if (__LL_USB_IsConn(Instance)) {
338         __LL_USB_Conn_Clr(Instance);
339         LL_USB_DetConnectCallback(Instance);
340     }
341 
342     if (__LL_USB_IsDisconn(Instance)) {
343         __LL_USB_Disconn_Clr(Instance);
344         LL_USB_DetDisonnectCallback(Instance);
345     }
346 }
347 
348 /**
349   * @brief  USB Detect Connect Callback
350   * @param  Instance Specifies USB peripheral
351   * @return None
352   */
LL_USB_DetConnectCallback(USB_TypeDef * Instance)353 __WEAK void LL_USB_DetConnectCallback(USB_TypeDef *Instance)
354 {
355     /* Prevent unused argument(s) compilation warning */
356     LL_UNUSED(Instance);
357     /* NOTE: This function should not be modified, when the callback is needed,
358              the LL_USB_DetConnectCallback could be implemented in the USB Middleware file
359      */
360 }
361 
362 /**
363   * @brief  USB Detect Disconnect Callback
364   * @param  Instance Specifies USB peripheral
365   * @return None
366   */
LL_USB_DetDisonnectCallback(USB_TypeDef * Instance)367 __WEAK void LL_USB_DetDisonnectCallback(USB_TypeDef *Instance)
368 {
369     /* Prevent unused argument(s) compilation warning */
370     LL_UNUSED(Instance);
371     /* NOTE: This function should not be modified, when the callback is needed,
372              the LL_USB_DetDisonnectCallback could be implemented in the USB Middleware file
373      */
374 }
375 /**
376   * @}
377   */
378 
379 
380 /** @defgroup USB_LL_Exported_Functions_Group4 USB Endpoint Initerrupt Management
381   * @brief    USB Endpoint Initerrupt Management
382     *
383   @verbatim
384   ==============================================================================
385                         ##### IRQ handler management #####
386   ==============================================================================
387   [..]
388     This section provides USB Endpoint IRQ handler function.
389   @endverbatim
390   * @{
391   */
392 
393 /**
394   * @brief  LL USB Endpoint IRQ Handler
395   * @param  Instance Specifies USB peripheral
396   * @return None
397   */
LL_USB_EpIRQHandler(USB_TypeDef * Instance)398 void LL_USB_EpIRQHandler(USB_TypeDef *Instance)
399 {
400     uint8_t ep0_epxtx_int_sta, epx_rx_int_sta;
401 
402     //Get all endpoint interrupt status
403     epx_rx_int_sta = __LL_USB_EPx_RXIntSta_Get(Instance);
404     ep0_epxtx_int_sta = __LL_USB_EP0AndEPxTX_IntSta_Get(Instance);
405 
406     if (ep0_epxtx_int_sta & USB_EP0_INT_STA_Msk) {              /* Endpoint 0 Interrupt */
407         //USB Endpoint Index Set
408         __LL_USB_EPIndex_Set(Instance, EP_NUM_0);
409 
410         if (__LL_USB_IsSetupPacket(Instance) && __LL_USB_EP0_IsRXPktRdy(Instance)) {        //Endpoint 0 Setup
411             LL_USB_Ep0SetupCallback(Instance);
412         } else if (__LL_USB_IsInPacket(Instance)) {                                         //Endpoint 0 IN
413             LL_USB_Ep0InCallback(Instance);
414         } else if (__LL_USB_IsOutPacket(Instance) && __LL_USB_EP0_IsRXPktRdy(Instance)) {   //Endpoint 0 OUT
415             LL_USB_Ep0OutCallback(Instance);
416         }
417     } else if (ep0_epxtx_int_sta & USB_TX_EP1_INT_STA_Msk) {    /* Endpoint 1 IN Interrupt */
418         //USB Endpoint Index Set
419         __LL_USB_EPIndex_Set(Instance, EP_NUM_1);
420         LL_USB_Ep1InCallback(Instance);
421     } else if (ep0_epxtx_int_sta & USB_TX_EP2_INT_STA_Msk) {    /* Endpoint 2 IN Interrupt */
422         //USB Endpoint Index Set
423         __LL_USB_EPIndex_Set(Instance, EP_NUM_2);
424         LL_USB_Ep2InCallback(Instance);
425     } else if (epx_rx_int_sta & USB_RX_EP1_INT_STA_Msk) {       /* Endpoint 1 OUT Interrupt */
426         //USB Endpoint Index Set
427         __LL_USB_EPIndex_Set(Instance, EP_NUM_1);
428         LL_USB_Ep1OutCallback(Instance);
429     } else if (epx_rx_int_sta & USB_RX_EP2_INT_STA_Msk) {       /* Endpoint 2 OUT Interrupt */
430         //USB Endpoint Index Set
431         __LL_USB_EPIndex_Set(Instance, EP_NUM_2);
432         LL_USB_Ep2OutCallback(Instance);
433     }
434 }
435 
436 /**
437   * @brief  USB Endpoint 0 Setup Callback
438   * @param  Instance Specifies USB peripheral
439   * @return None
440   */
LL_USB_Ep0SetupCallback(USB_TypeDef * Instance)441 __WEAK void LL_USB_Ep0SetupCallback(USB_TypeDef *Instance)
442 {
443     /* Prevent unused argument(s) compilation warning */
444     LL_UNUSED(Instance);
445     /* NOTE: This function should not be modified, when the callback is needed,
446              the LL_USB_Ep0SetupCallback could be implemented in the USB Middleware file
447      */
448 }
449 
450 /**
451   * @brief  USB Endpoint 0 In Callback
452   * @param  Instance Specifies USB peripheral
453   * @return None
454   */
LL_USB_Ep0InCallback(USB_TypeDef * Instance)455 __WEAK void LL_USB_Ep0InCallback(USB_TypeDef *Instance)
456 {
457     /* Prevent unused argument(s) compilation warning */
458     LL_UNUSED(Instance);
459     /* NOTE: This function should not be modified, when the callback is needed,
460              the LL_USB_Ep0InCallback could be implemented in the USB Middleware file
461      */
462 }
463 
464 /**
465   * @brief  USB Endpoint 0 Out Callback
466   * @param  Instance Specifies USB peripheral
467   * @return None
468   */
LL_USB_Ep0OutCallback(USB_TypeDef * Instance)469 __WEAK void LL_USB_Ep0OutCallback(USB_TypeDef *Instance)
470 {
471     /* Prevent unused argument(s) compilation warning */
472     LL_UNUSED(Instance);
473     /* NOTE: This function should not be modified, when the callback is needed,
474              the LL_USB_Ep0OutCallback could be implemented in the USB Middleware file
475      */
476 }
477 
478 /**
479   * @brief  USB Endpoint 1 In Callback
480   * @param  Instance Specifies USB peripheral
481   * @return None
482   */
LL_USB_Ep1InCallback(USB_TypeDef * Instance)483 __WEAK void LL_USB_Ep1InCallback(USB_TypeDef *Instance)
484 {
485     /* Prevent unused argument(s) compilation warning */
486     LL_UNUSED(Instance);
487     /* NOTE: This function should not be modified, when the callback is needed,
488              the LL_USB_Ep1InCallback could be implemented in the USB Middleware file
489      */
490 }
491 
492 /**
493   * @brief  USB Endpoint 1 Out Callback
494   * @param  Instance Specifies USB peripheral
495   * @return None
496   */
LL_USB_Ep1OutCallback(USB_TypeDef * Instance)497 __WEAK void LL_USB_Ep1OutCallback(USB_TypeDef *Instance)
498 {
499     /* Prevent unused argument(s) compilation warning */
500     LL_UNUSED(Instance);
501     /* NOTE: This function should not be modified, when the callback is needed,
502              the LL_USB_Ep1OutCallback could be implemented in the USB Middleware file
503      */
504 }
505 
506 /**
507   * @brief  USB Endpoint 2 In Callback
508   * @param  Instance Specifies USB peripheral
509   * @return None
510   */
LL_USB_Ep2InCallback(USB_TypeDef * Instance)511 __WEAK void LL_USB_Ep2InCallback(USB_TypeDef *Instance)
512 {
513     /* Prevent unused argument(s) compilation warning */
514     LL_UNUSED(Instance);
515     /* NOTE: This function should not be modified, when the callback is needed,
516              the LL_USB_Ep2InCallback could be implemented in the USB Middleware file
517      */
518 }
519 
520 /**
521   * @brief  USB Endpoint 2 Out Callback
522   * @param  Instance Specifies USB peripheral
523   * @return None
524   */
LL_USB_Ep2OutCallback(USB_TypeDef * Instance)525 __WEAK void LL_USB_Ep2OutCallback(USB_TypeDef *Instance)
526 {
527     /* Prevent unused argument(s) compilation warning */
528     LL_UNUSED(Instance);
529     /* NOTE: This function should not be modified, when the callback is needed,
530              the LL_USB_Ep2OutCallback could be implemented in the USB Middleware file
531      */
532 }
533 /**
534   * @}
535   */
536 
537 /**
538   * @}
539   */
540 
541 
542 /* Private functions ---------------------------------------------------------*/
543 
544 
545 #endif /* LL_USB_MODULE_ENABLED */
546 
547 
548 /**
549   * @}
550   */
551 
552 /**
553   * @}
554   */
555 
556 
557 /************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/
558 
559