summaryrefslogtreecommitdiff
path: root/bsp/tae32f5300/libraries/TAE32F53xx_StdPeriph_Driver/src/tae32f53xx_ll_usb.c
blob: a3248f562a0a7d0a9ed208df1d6b7d9ddfd6502e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/**
  ******************************************************************************
  * @file    tae32f53xx_ll_usb.c
  * @author  MCD Application Team
  * @brief   USB LL Module Driver.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 Tai-Action.
  * All rights reserved.</center></h2>
  *
  * This software is licensed by Tai-Action under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tae32f53xx_ll.h"


#define DBG_TAG             "USB LL"
#define DBG_LVL             DBG_ERROR
#include "dbg/tae32f53xx_dbg.h"


/** @addtogroup TAE32F53xx_LL_Driver
  * @{
  */

/** @defgroup USB_LL USB LL
  * @brief    USB LL Module Driver.
  * @{
  */


#ifdef LL_USB_MODULE_ENABLED


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/** @defgroup USB_LL_Exported_Functions USB LL Exported Functions
  * @brief    USB LL Exported Functions
  * @{
  */

/** @defgroup USB_LL_Exported_Functions_Group1 Initialization and de-initialization functions
  * @brief    Initialization and Configuration functions
  *
  @verbatim
  ===============================================================================
              ##### Initialization and de-initialization functions #####
  ===============================================================================
   [..]
     This section provides functions allowing to initialize and de-initialize the USB
     to be ready for use.

  @endverbatim
  * @{
  */

/**
  * @brief  Initializes the USB peripheral
  * @param  Instance Specifies USB peripheral
  * @return Status of the Initialization
  */
LL_StatusETypeDef LL_USB_Init(USB_TypeDef *Instance)
{
    /* Init the low level hardware eg. Clock, NVIC */
    LL_USB_MspInit(Instance);

    //USB Hardware Config
    __LL_USB_DMOutputHardware(Instance);
    __LL_USB_DPOutputHardware(Instance);
    __LL_USB_DMInputHardware(Instance);
    __LL_USB_DPInputHardware(Instance);

    __LL_USB_DMPullDownDisable(Instance);
    __LL_USB_DMPullUpNormal(Instance);
    __LL_USB_DPPullDownDisable(Instance);
    __LL_USB_DPPullUpNormal(Instance);

    __LL_USB_VbusValidThreshold_Set(Instance);
    __LL_USB_VbusAboveAdevSessThres_Set(Instance);
    __LL_USB_VbusAboveSessEndThres_Set(Instance);
    __LL_USB_MiniABConnectorID_Set(Instance);
    __LL_USB_PHY_En(Instance);

    //USB Power Control
    __LL_USB_SoftConn_En(Instance);
    __LL_USB_HighSpeed_Dis(Instance);
    __LL_USB_SuspendDMOut_En(Instance);

    //USB Detect Interrupt Config
    __LL_USB_DebouceMax_Set(Instance, 0x80);
    __LL_USB_Conn_Clr(Instance);
    __LL_USB_Conn_Int_En(Instance);
    __LL_USB_Disconn_Clr(Instance);
    __LL_USB_Disconn_Int_En(Instance);

    //USB Interrupt Enable
    __LL_USB_Int_EN(Instance, USB_CTRL_INT_ALL_Msk);
    __LL_USB_EP0AndEPxTX_Int_EN(Instance, EP_NUM_0);

    //USB INT Send To CPU Config
    __LL_USB_INTSendToCPU_En(Instance, USB_CTRL_INT_TO_CPU_Msk);
    __LL_USB_INTSendToCPU_En(Instance, USB_EP_INT_TO_CPU_Msk);
    __LL_USB_INTSendToCPU_En(Instance, USB_LPM_INT_TO_CPU_Msk);

    return LL_OK;
}

/**
  * @brief  DeInitializes the USB peripheral
  * @param  Instance Specifies USB peripheral
  * @return Status of the Initialization
  */
LL_StatusETypeDef LL_USB_DeInit(USB_TypeDef *Instance)
{
    //USB INT Send To CPU Config
    __LL_USB_INTSendToCPU_Dis(Instance, USB_CTRL_INT_TO_CPU_Msk);
    __LL_USB_INTSendToCPU_Dis(Instance, USB_EP_INT_TO_CPU_Msk);
    __LL_USB_INTSendToCPU_Dis(Instance, USB_LPM_INT_TO_CPU_Msk);

    //USB Interrupt Disable
    __LL_USB_Int_Dis(Instance, USB_CTRL_INT_ALL_Msk);
    __LL_USB_EP0AndEPxTX_Int_Dis(Instance, EP_NUM_0);

    //USB Detect Interrupt Config
    __LL_USB_Conn_Clr(Instance);
    __LL_USB_Conn_Int_Dis(Instance);
    __LL_USB_Disconn_Clr(Instance);
    __LL_USB_Disconn_Int_Dis(Instance);

    //USB Power Control
    __LL_USB_SoftConn_Dis(Instance);
    __LL_USB_SuspendDMOut_Dis(Instance);

    //USB Hardware Config
    __LL_USB_DMOutputNormal(Instance);
    __LL_USB_DPOutputNormal(Instance);
    __LL_USB_DMInputNormal(Instance);
    __LL_USB_DPInputNormal(Instance);
    __LL_USB_DMPullDownNormal(Instance);
    __LL_USB_DMPullUpNormal(Instance);
    __LL_USB_DPPullDownNormal(Instance);
    __LL_USB_DPPullUpNormal(Instance);

    __LL_USB_VbusValidThreshold_Clr(Instance);
    __LL_USB_VbusAboveAdevSessThres_Clr(Instance);
    __LL_USB_VbusAboveSessEndThres_Clr(Instance);
    __LL_USB_MiniABConnectorID_Clr(Instance);
    __LL_USB_PHY_Dis(Instance);

    /* DeInit the low level hardware eg. Clock, NVIC */
    LL_USB_MspDeInit(Instance);

    return LL_OK;
}

/**
  * @brief  Initializes the USB MSP.
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_MspInit(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_MspInit could be implemented in the user file
     */
}

/**
  * @brief  DeInitializes the USB MSP
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_MspDeInit(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_MspDeInit could be implemented in the user file
     */
}
/**
  * @}
  */


/** @defgroup USB_LL_Exported_Functions_Group2 USB Controler Initerrupt Management
  * @brief    USB Controler Initerrupt Management
    *
  @verbatim
  ==============================================================================
                        ##### IRQ handler management #####
  ==============================================================================
  [..]
    This section provides USB Controler IRQ handler function.
  @endverbatim
  * @{
  */

/**
  * @brief  LL USB Controler IRQ Handler
  * @param  Instance Specifies USB peripheral
  * @return None
  */
void LL_USB_CtrlIRQHandler(USB_TypeDef *Instance)
{
    uint8_t usb_ctrl_int_sta = __LL_USB_IntSta_Get(Instance);

    if (usb_ctrl_int_sta & USB_SUSPEND_INT_STA_Msk) {
        LL_USB_CtrlSuspendCallback(Instance);
    }

    if (usb_ctrl_int_sta & USB_RESUME_INT_STA_Msk) {
        LL_USB_CtrlResumeCallback(Instance);
    }

    if (usb_ctrl_int_sta & USB_RST_INT_STA_Msk) {
        LL_USB_CtrlResetCallback(Instance);
    }

    if (usb_ctrl_int_sta & USB_SOF_INT_STA_Msk) {
        LL_USB_CtrlSofCallback(Instance);
    }

    if (usb_ctrl_int_sta & USB_SESS_END_INT_STA_Msk) {
        LL_USB_CtrlSessEndCallback(Instance);
    }
}

/**
  * @brief  USB Controler Suspend Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_CtrlSuspendCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_CtrlSuspendCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Controler Resume Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_CtrlResumeCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_CtrlResumeCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Controler Reset Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_CtrlResetCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_CtrlResetCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Controler SOF Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_CtrlSofCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_CtrlSofCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Controler Session End Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_CtrlSessEndCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_CtrlSessEndCallback could be implemented in the USB Middleware file
     */
}
/**
  * @}
  */


/** @defgroup USB_LL_Exported_Functions_Group3 USB Detect Initerrupt Management
  * @brief    USB Detect Initerrupt Management
    *
  @verbatim
  ==============================================================================
                        ##### IRQ handler management #####
  ==============================================================================
  [..]
    This section provides USB Detect IRQ handler function.
  @endverbatim
  * @{
  */

/**
  * @brief  LL USB Detect IRQ Handler
  * @param  Instance Specifies USB peripheral
  * @return None
  */
void LL_USB_DetIRQHandler(USB_TypeDef *Instance)
{
    if (__LL_USB_IsConn(Instance)) {
        __LL_USB_Conn_Clr(Instance);
        LL_USB_DetConnectCallback(Instance);
    }

    if (__LL_USB_IsDisconn(Instance)) {
        __LL_USB_Disconn_Clr(Instance);
        LL_USB_DetDisonnectCallback(Instance);
    }
}

/**
  * @brief  USB Detect Connect Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_DetConnectCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_DetConnectCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Detect Disconnect Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_DetDisonnectCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_DetDisonnectCallback could be implemented in the USB Middleware file
     */
}
/**
  * @}
  */


/** @defgroup USB_LL_Exported_Functions_Group4 USB Endpoint Initerrupt Management
  * @brief    USB Endpoint Initerrupt Management
    *
  @verbatim
  ==============================================================================
                        ##### IRQ handler management #####
  ==============================================================================
  [..]
    This section provides USB Endpoint IRQ handler function.
  @endverbatim
  * @{
  */

/**
  * @brief  LL USB Endpoint IRQ Handler
  * @param  Instance Specifies USB peripheral
  * @return None
  */
void LL_USB_EpIRQHandler(USB_TypeDef *Instance)
{
    uint8_t ep0_epxtx_int_sta, epx_rx_int_sta;

    //Get all endpoint interrupt status
    epx_rx_int_sta = __LL_USB_EPx_RXIntSta_Get(Instance);
    ep0_epxtx_int_sta = __LL_USB_EP0AndEPxTX_IntSta_Get(Instance);

    if (ep0_epxtx_int_sta & USB_EP0_INT_STA_Msk) {              /* Endpoint 0 Interrupt */
        //USB Endpoint Index Set
        __LL_USB_EPIndex_Set(Instance, EP_NUM_0);

        if (__LL_USB_IsSetupPacket(Instance) && __LL_USB_EP0_IsRXPktRdy(Instance)) {        //Endpoint 0 Setup
            LL_USB_Ep0SetupCallback(Instance);
        } else if (__LL_USB_IsInPacket(Instance)) {                                         //Endpoint 0 IN
            LL_USB_Ep0InCallback(Instance);
        } else if (__LL_USB_IsOutPacket(Instance) && __LL_USB_EP0_IsRXPktRdy(Instance)) {   //Endpoint 0 OUT
            LL_USB_Ep0OutCallback(Instance);
        }
    } else if (ep0_epxtx_int_sta & USB_TX_EP1_INT_STA_Msk) {    /* Endpoint 1 IN Interrupt */
        //USB Endpoint Index Set
        __LL_USB_EPIndex_Set(Instance, EP_NUM_1);
        LL_USB_Ep1InCallback(Instance);
    } else if (ep0_epxtx_int_sta & USB_TX_EP2_INT_STA_Msk) {    /* Endpoint 2 IN Interrupt */
        //USB Endpoint Index Set
        __LL_USB_EPIndex_Set(Instance, EP_NUM_2);
        LL_USB_Ep2InCallback(Instance);
    } else if (epx_rx_int_sta & USB_RX_EP1_INT_STA_Msk) {       /* Endpoint 1 OUT Interrupt */
        //USB Endpoint Index Set
        __LL_USB_EPIndex_Set(Instance, EP_NUM_1);
        LL_USB_Ep1OutCallback(Instance);
    } else if (epx_rx_int_sta & USB_RX_EP2_INT_STA_Msk) {       /* Endpoint 2 OUT Interrupt */
        //USB Endpoint Index Set
        __LL_USB_EPIndex_Set(Instance, EP_NUM_2);
        LL_USB_Ep2OutCallback(Instance);
    }
}

/**
  * @brief  USB Endpoint 0 Setup Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_Ep0SetupCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_Ep0SetupCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Endpoint 0 In Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_Ep0InCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_Ep0InCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Endpoint 0 Out Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_Ep0OutCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_Ep0OutCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Endpoint 1 In Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_Ep1InCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_Ep1InCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Endpoint 1 Out Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_Ep1OutCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_Ep1OutCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Endpoint 2 In Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_Ep2InCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_Ep2InCallback could be implemented in the USB Middleware file
     */
}

/**
  * @brief  USB Endpoint 2 Out Callback
  * @param  Instance Specifies USB peripheral
  * @return None
  */
__WEAK void LL_USB_Ep2OutCallback(USB_TypeDef *Instance)
{
    /* Prevent unused argument(s) compilation warning */
    LL_UNUSED(Instance);
    /* NOTE: This function should not be modified, when the callback is needed,
             the LL_USB_Ep2OutCallback could be implemented in the USB Middleware file
     */
}
/**
  * @}
  */

/**
  * @}
  */


/* Private functions ---------------------------------------------------------*/


#endif /* LL_USB_MODULE_ENABLED */


/**
  * @}
  */

/**
  * @}
  */


/************************* (C) COPYRIGHT Tai-Action *****END OF FILE***********/