summaryrefslogtreecommitdiff
path: root/bsp/tae32f5300/libraries/TAE32F53xx_StdPeriph_Driver/src/tae32f53xx_ll_uart.c
blob: c0aa9ec98021ab2c75d5587e743b98251b6071f0 (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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
/**
  ******************************************************************************
  * @file    tae32f53xx_ll_uart.c
  * @author  MCD Application Team
  * @brief   UART 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             "UART"
#define DBG_LVL             DBG_ERROR
#include "dbg/tae32f53xx_dbg.h"


/** @addtogroup TAE32F53xx_LL_Driver
  * @{
  */

/** @defgroup UART_LL UART LL
  * @brief    UART LL module driver.
  * @{
  */

#ifdef LL_UART_MODULE_ENABLED

/* Private variables ---------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/** @defgroup UART_LL_Private_Macros UART LL Private Macros
  * @brief    UART LL Private Macros
  * @{
  */

/**
  * @brief  UART Function Alternative
  * @param  flag Condition Flag
  * @param  func_t True Funciton
  * @param  func_f False Function
  * @return None
  */
#define UART_FUNC_ALTER(flag, func_t, func_f)   \
        do{                                     \
            if((flag)) {                        \
                func_t;                         \
            } else {                            \
                func_f;                         \
            }                                   \
        } while(0)

/**
  * @}
  */


/* Private function prototypes -----------------------------------------------*/
/** @addtogroup UART_LL_Exported_Functions UART LL Exported Functions
  * @brief    UART LL Exported Functions
  * @{
  */
#ifdef LL_DMA_MODULE_ENABLED

    DMA_ChannelETypeDef LL_UART_DMA_Init(DMA_UserCfgTypeDef *dma_user_cfg);
    LL_StatusETypeDef LL_UART_DMA_Deinit(DMA_ChannelETypeDef ch);
    void LL_UART_DMA_TXCHEndCallback(void *arg);
    void LL_UART_DMA_RXCHEndCallback(void *arg);
    void LL_UART_DMA_TXCHErrorCallBck(void *arg);
    void LL_UART_DMA_RXCHErrorCallBck(void *arg);

#endif  /*!< LL_DMA_MODULE_ENABLED */
/**
  * @}
  */

/* Exported functions --------------------------------------------------------*/
/** @defgroup UART_LL_Exported_Functions UART LL Exported Functions
  * @brief    UART LL Exported Functions
  * @{
  */

/** @defgroup UART_LL_Exported_Functions_Group1 Initialization and de-initialization functions
  * @brief    Initialization and de-initialization functions
  * @{
  */

/**
  * @brief  UART LL init
  * @param  Instance Specifies UART peripheral
  * @param  Init init pointer
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_Init(UART_TypeDef *Instance, UART_InitTypeDef *Init)
{
    uint32_t baud_rate;

    /* Init the low level hardware eg. Clock, NVIC */
    LL_UART_MspInit(Instance);

    //Parity config
    if (Init->parity == UART_PARITY_NO) {
        __LL_UART_Parity_Dis(Instance);
    } else {
        __LL_UART_Parity_En(Instance);
        UART_FUNC_ALTER(Init->parity == UART_PARITY_ODD, __LL_UART_EvenParity_Clr(Instance), __LL_UART_EvenParity_Set(Instance));
    }

    //Stop bit config
    UART_FUNC_ALTER(Init->stop_len == UART_STOP_LEN_1b, __LL_UART_Stop1Bit_Set(Instance), __LL_UART_Stop2bits_Set(Instance));

    //Data length config
    if (Init->dat_len == UART_DAT_LEN_9b) {
        __LL_UART_DatLen9bitsExt_En(Instance);
        __LL_UART_RAR_Set(Instance, Init->U9BRxAddress);

        if (Init->U9BAddrMatchMode_Enable == ENABLE) {
            __LL_UART_AddrMatchMode_En(Instance);
            __LL_UART_TxMode8bits_Set(Instance);
        } else {
            __LL_UART_AddrMatchMode_Dis(Instance);
            __LL_UART_TxMode9bits_Set(Instance);
        }
    } else {
        __LL_UART_DatLen9bitsExt_Dis(Instance);
        __LL_UART_DatLen_Sel(Instance, (uint8_t)Init->dat_len);
    }

    //FIFO config, FCR register can write only
    __LL_UART_FCR_Write(Instance, Init->rx_tl | Init->tx_tl | UART_FCR_XFIFOR_Msk | UART_FCR_RFIFOR_Msk | UART_FCR_FIFOE_Msk);

    //Baudrate config
    __LL_UART_DivLatchAccess_Set(Instance);

    baud_rate = (LL_SYSCTRL_APB0ClkGet() + Init->baudrate / 2) / Init->baudrate;
    __LL_UART_DivLatchLow_Write(Instance, (uint8_t)(baud_rate >> 4));
    __LL_UART_DivLatchHigh_Write(Instance, (uint8_t)(baud_rate >> 12));
    __LL_UART_DivLatchFrac_Set(Instance, (baud_rate & 0xFUL));

    __LL_UART_DivLatchAccess_Clr(Instance);

    return LL_OK;
}

/**
  * @brief  UART LL deinit
  * @param  Instance Specifies UART peripheral
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_DeInit(UART_TypeDef *Instance)
{
    /* DeInit the low level hardware eg. Clock, NVIC */
    LL_UART_MspDeInit(Instance);

    return LL_OK;
}

/**
  * @brief  Initializes the UART MSP.
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_MspInit(UART_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_UART_MspInit could be implemented in the user file
     */
}

/**
  * @brief  DeInitializes the UART MSP
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_MspDeInit(UART_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_UART_MspDeInit could be implemented in the user file
     */
}
/**
  * @}
  */


/** @defgroup UART_LL_Exported_Functions_Group2 UART Transmit and Recieve Function
  * @brief    UART Transmit Function
  * @{
  */

/**
  * @brief Send an amount of data in blocking mode.
  * @note   When UART parity is not enabled, and Word Length is configured to 9 bits,
  *         the sent data is handled as a set of u16. In this case, Size must indicate the number
  *         of u16 provided through pData.
  * @param  Instance Specifies UART peripheral
  * @param  pData Pointer to data buffer (u8 or u16 data elements).
  * @param  Size Amount of data elements (u8 or u16) to be sent.
  * @param  Timeout Timeout duration.
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_Transmit_CPU(UART_TypeDef *Instance, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
    uint8_t  *pdata8bits;
    uint16_t *pdata16bits;
    uint32_t tickstart;
    uint32_t TxXferCount = Size;

    /* Check the UART initiation struct allocation */
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    /* In case of 9bits, pRxData needs to be handled as a uint16_t pointer */
    if (__LL_UART_IsDatLen9bitsEn(Instance) && __LL_UART_TxModeSta_Get(Instance)) {
        pdata8bits  = NULL;
        pdata16bits = (uint16_t *) pData;
    } else {
        pdata8bits  = pData;
        pdata16bits = NULL;
    }

    //Transmit data loop
    while (TxXferCount) {
        //Wait TxFIFO to be not full
        tickstart = LL_GetTick();

        while (!__LL_UART_IsTxFIFONotFull(Instance)) {
            if ((LL_GetTick() - tickstart) > Timeout) {
                return LL_TIMEOUT;
            }
        }

        if (pdata16bits) {
            __LL_UART_TxBuf9bits_Write(Instance, *pdata16bits++);
        } else {
            __LL_UART_TxBuf8bits_Write(Instance, *pdata8bits++);
        }

        /* Wait for UART Transmit completed */
        while (!__LL_UART_IsTxEmpty(Instance));

        TxXferCount--;
    }

    return LL_OK;
}

/**
  * @brief  Receive an amount of data in blocking mode.
  * @note   When UART parity is not enabled, and Word Length is configured to 9 bits ,
  *         the received data is handled as a set of u16. In this case, Size must indicate the number
  *         of u16 available through pData.
  * @param  Instance Specifies UART peripheral
  * @param  pData Pointer to data buffer (u8 or u16 data elements).
  * @param  Size Amount of data elements (u8 or u16) to be received.
  * @param  Timeout Timeout duration.
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_Receive_CPU(UART_TypeDef *Instance, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
    uint8_t  *pdata8bits;
    uint16_t *pdata16bits;
    uint32_t tickstart;
    uint32_t RxXferCount = Size;

    //Check the UART initiation struct allocation
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    //In case of 9bits, pRxData needs to be handled as a uint16_t pointer
    if (__LL_UART_IsDatLen9bitsEn(Instance)) {
        pdata8bits  = NULL;
        pdata16bits = (uint16_t *) pData;
    } else {
        pdata8bits  = pData;
        pdata16bits = NULL;
    }

    //Receive data loop
    while (RxXferCount) {
        //Wait RxFIFO to be not empty
        tickstart = LL_GetTick();

        while (!__LL_UART_IsRxFIFONotEmpty(Instance)) {
            if ((LL_GetTick() - tickstart) > Timeout) {
                return LL_TIMEOUT;
            }
        }

        //Read data from RxFIFO
        if (pdata16bits) {
            *pdata16bits++ = (uint16_t)(__LL_UART_RxBuf9bits_Read(Instance));
        } else {
            *pdata8bits++ = (uint8_t)(__LL_UART_RxBuf8bits_Read(Instance));
        }

        RxXferCount--;
    }

    return LL_OK;
}

/**
  * @brief  Send an amount of data in interrupt mode.
  * @param  Instance Specifies UART peripheral
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_Transmit_IT(UART_TypeDef *Instance)
{
    /* Check the UART initiation struct allocation */
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    /* Enable Transmit Holding Register Empty Interrupt */
    __LL_UART_TxHoldEmpyt_INT_En(Instance);

    return LL_OK;
}

/**
  * @brief  Receive an amount of data in blocking mode.
  * @param  Instance Specifies UART peripheral
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_Receive_IT(UART_TypeDef *Instance)
{
    /* Check the UART initiation struct allocation */
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    /* RX data available interrupt */
    __LL_UART_RxDatAvl_INT_En(Instance);

    return LL_OK;
}

#ifdef LL_DMA_MODULE_ENABLED
/**
  * @brief  UART LL Transmit by DMA
  * @param  Instance Specifies UART peripheral
  * @param  dma_user_cfg user dma config pointer
  * @param  huart uart transmit by DMA handle pointer
  * @param  Timeout Limited operation time
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_Transmit_DMA(UART_TypeDef *Instance, DMA_UserCfgTypeDef *dma_user_cfg,
                                       UART_DMAHandleTypeDef *huart, uint32_t Timeout)
{
    uint8_t  *pdata8bits;
    uint16_t *pdata16bits;
    huart->dma_tx_ch = DMA_CHANNEL_INVALID;
    uint32_t tickstart;

    /* Check the UART initiation struct allocation */
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    if ((huart->buf == NULL) || (huart->buf_len == 0U)) {
        return LL_ERROR;
    }

    //In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer
    if (__LL_UART_IsDatLen9bitsEn(Instance) && __LL_UART_TxModeSta_Get(Instance)) {
        pdata8bits  = NULL;
        pdata16bits = (uint16_t *) huart->buf;
    } else {
        pdata8bits  = huart->buf;
        pdata16bits = NULL;
    }

    if (pdata16bits) {
        dma_user_cfg->dst_data_width  = DMA_DST_TRANS_WIDTH_16b;
    } else {
        dma_user_cfg->dst_data_width  = DMA_DST_TRANS_WIDTH_8b;
    }

    huart->Instance = Instance;
    huart->TXdma_status = UART_DMA_STATE_RESET;

    dma_user_cfg->end_arg = (void *)huart;
    dma_user_cfg->end_callback = LL_UART_DMA_TXCHEndCallback;
    dma_user_cfg->err_arg = (void *)huart;
    dma_user_cfg->err_callback = LL_UART_DMA_TXCHErrorCallBck;

    tickstart = LL_GetTick();

    while (huart->dma_tx_ch == DMA_CHANNEL_INVALID) {
        huart->dma_tx_ch = LL_UART_DMA_Init(dma_user_cfg);

        if ((LL_GetTick() - tickstart) > Timeout) {
            return LL_TIMEOUT;
        }
    }

    huart->TXdma_status = UART_DMA_STATE_READY;

    if (pdata16bits) {
        LL_DMA_Start_IT(DMA, huart->dma_tx_ch, (uint32_t)pdata16bits, (uint32_t)&Instance->THR, huart->buf_len);
    } else {
        LL_DMA_Start_IT(DMA, huart->dma_tx_ch, (uint32_t)pdata8bits, (uint32_t)&Instance->THR, huart->buf_len);
    }

    huart->TXdma_status = UART_DMA_STATE_BUSY;

    return LL_OK;
}

/**
  * @brief  UART LL Receive through DMA
  * @param  Instance Specifies UART peripheral
  * @param  huart uart transmit by DMA handle pointer
  * @param  dma_user_cfg user dma config pointer
  * @param  Timeout Limited operation time
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_Receive_DMA(UART_TypeDef *Instance, DMA_UserCfgTypeDef *dma_user_cfg,
                                      UART_DMAHandleTypeDef *huart, uint32_t Timeout)
{
    uint8_t  *pdata8bits;
    uint16_t *pdata16bits;
    huart->dma_rx_ch = DMA_CHANNEL_INVALID;
    uint32_t tickstart;

    /* Check the UART initiation struct allocation */
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    if ((huart->buf == NULL) || (huart->buf_len == 0U)) {
        return LL_ERROR;
    }

    //In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer
    if (__LL_UART_IsDatLen9bitsEn(Instance)) {
        pdata8bits  = NULL;
        pdata16bits = (uint16_t *) huart->buf;
    } else {
        pdata8bits  = huart->buf;
        pdata16bits = NULL;
    }

    if (pdata16bits) {
        dma_user_cfg->src_data_width  = DMA_SRC_TRANS_WIDTH_16b;
    } else {
        dma_user_cfg->src_data_width  = DMA_SRC_TRANS_WIDTH_8b;
    }

    huart->Instance = Instance;
    huart->RXdma_status = UART_DMA_STATE_RESET;

    dma_user_cfg->end_arg = (void *)huart;
    dma_user_cfg->end_callback = LL_UART_DMA_RXCHEndCallback;
    dma_user_cfg->err_arg = (void *)huart;
    dma_user_cfg->err_callback = LL_UART_DMA_RXCHErrorCallBck;

    tickstart = LL_GetTick();

    while (huart->dma_rx_ch == DMA_CHANNEL_INVALID) {
        huart->dma_rx_ch = LL_UART_DMA_Init(dma_user_cfg);

        if ((LL_GetTick() - tickstart) > Timeout) {
            return LL_TIMEOUT;
        }
    }

    huart->RXdma_status = UART_DMA_STATE_READY;

    if (pdata16bits) {
        LL_DMA_Start_IT(DMA, huart->dma_rx_ch, (uint32_t)&Instance->RBR, (uint32_t)pdata16bits, huart->buf_len);
    } else {
        LL_DMA_Start_IT(DMA, huart->dma_rx_ch, (uint32_t)&Instance->RBR, (uint32_t)pdata8bits, huart->buf_len);
    }

    huart->RXdma_status = UART_DMA_STATE_BUSY;

    return LL_OK;
}
#endif

/**
  * @}
  */


/** @defgroup UART_LL_Exported_Functions_Group3 UART Misc Functions
  * @brief    UART Misc Functions
  * @{
  */

/**
  * @brief  UART LL 9BIT Transmit Addr Send
  * @param  Instance Specifies UART peripheral
  * @param  TxAddr 9BIT Transmit Addr Send
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_Uart_9bit_SendAddress(UART_TypeDef *Instance, uint8_t TxAddr)
{
    /* Check the UART initiation struct allocation */
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    if (__LL_UART_IsDatLen9bitsEn(Instance)) {
        if (!(__LL_UART_TxModeSta_Get(Instance))) {
            /* Transmit Address set */
            __LL_UART_TAR_Set(Instance, TxAddr);
            /* Send address */
            __LL_UART_SendAddr_Start(Instance);
            return LL_OK;
        } else {
            __LL_UART_TxBuf9bits_Write(Instance, TxAddr | UART_THR_MSB_9thbit);
            __LL_UART_SendDat_Start(Instance);
            return LL_OK;
        }

    } else {
        return LL_ERROR;
    }
}


/**
  * @brief  UART LL RS485 Config
  * @param  Instance Specifies UART peripheral
  * @param  Cfg RS485 config pointer
    @note  The receive funtion is Enable by default
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_RS485Cfg(UART_TypeDef *Instance, UART_Rs485CfgTypeDef *Cfg)
{
    /* Check the UART initiation struct allocation */
    assert_param(IS_UART_ALL_INSTANCE(Instance));

    //RS485 mode enable
    __LL_UART_RS485Mode_En(Instance);

    //DE signal enable and palarity config
    UART_FUNC_ALTER(Cfg->de_en == ENABLE, __LL_UART_DE_En(Instance), __LL_UART_DE_Dis(Instance));

    /* The receive funtion is Enable by default*/
    __LL_UART_RE_En(Instance);

    UART_FUNC_ALTER(Cfg->de_polarity == UART_DE_POL_ACT_LOW, __LL_UART_DE_ActLow_Set(Instance), __LL_UART_DE_ActHigh_Set(Instance));

    //DE assert/deassert time config
    __LL_UART_DE_AssertTime_Set(Instance, Cfg->de_assert_time);
    __LL_UART_DE_DeAssertTime_Set(Instance, Cfg->de_deassert_time);

    return LL_OK;
}

/**
  * @brief  This indicates the number of data entries in the transmit FIFO
  * @param  Instance UART peripheral
  * @return the number of data entries in the transmit FIFO
  */
uint8_t LL_UART_TxFIFOLVL_GET(UART_TypeDef *Instance)
{
    return __LL_UART_TxFIFOLevel_Get(Instance);
}

/**
  * @brief  This indicates the number of data entries in the receive FIFO.
  * @param  Instance UART peripheral
  * @return the number of data entries in the receive FIFO.
  */
uint8_t LL_UART_RxFIFOLVL_GET(UART_TypeDef *Instance)
{
    return __LL_UART_RxFIFOLevel_Get(Instance);
}

/**
  * @}
  */


/** @defgroup UART_LL_Exported_Functions_Interrupt UART Interrupt handler and callback
  * @brief    UART Interrupt handler and callback
  * @{
  */

/**
  * @brief  UART LL IRQ Handler
  * @param  Instance &#&
  * @return None
  */
void LL_UART_IRQHandler(UART_TypeDef *Instance)
{
    uint8_t int_id = __LL_UART_INT_ID_Get(Instance);

    switch (int_id) {
        case UART_INT_ID_MODEM_STA:
            LL_UART_ModemStaCallback(Instance);
            break;

        case UART_INT_ID_TX_EMPTY:
            LL_UART_TxEmptyCallback(Instance);
            break;

        case UART_INT_ID_RX_AVL:
            LL_UART_RxAvailableCallback(Instance);
            break;

        case UART_INT_ID_RX_LINE_STA:
            LL_UART_RxLineStaCallback(Instance);
            break;

        case UART_INT_ID_BUSY_DET:
            LL_UART_BusyDetCallback(Instance);
            break;

        case UART_INT_ID_CHAR_TIMEOUT:
            LL_UART_CharTimeOutCallback(Instance);
            break;

        default:
            LOG_E("Error interrupt ID!\n");
            break;
    }
}


/**
  * @brief  UART LL Modem Status Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_ModemStaCallback(UART_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_UART_ModemStaCallback could be implemented in the user file
     */
}

/**
  * @brief  UART LL Tx Empty Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_TxEmptyCallback(UART_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_UART_TxEmptyCallback could be implemented in the user file
     */

}

/**
  * @brief  UART LL Rx Available Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_RxAvailableCallback(UART_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_UART_RxAvailableCallback could be implemented in the user file
     */
}

/**
  * @brief  UART LL RX Line Status Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_RxLineStaCallback(UART_TypeDef *Instance)
{
    uint32_t line_sta = __LL_UART_LineSta_Get(Instance);

    if (line_sta & UART_LSR_RFE_Msk) {
        if (line_sta & UART_LSR_BI_Msk) {
            LL_UART_BreakErrCallback(Instance);
        }

        if (line_sta & UART_LSR_FE_Msk) {
            LL_UART_FrameErrCallback(Instance);
        }

        if (line_sta & UART_LSR_PE_Msk) {
            LL_UART_ParityErrCallback(Instance);
        }

        if (line_sta & UART_LSR_OE_Msk) {
            LL_UART_RxOverrunErrCallback(Instance);
        }
    }
}

/**
  * @brief  UART LL Busy Detect Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_BusyDetCallback(UART_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_UART_BusyDetCallback could be implemented in the user file
     */
}

/**
  * @brief  UART LL Character Timeout Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_CharTimeOutCallback(UART_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_UART_CharTimeOutCallback could be implemented in the user file
     */
}


/**
  * @brief  UART LL Break Error Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_BreakErrCallback(UART_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_UART_BreakErrCallback could be implemented in the user file
     */

}

/**
  * @brief  UART LL Frame Error Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_FrameErrCallback(UART_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_UART_FrameErrCallback could be implemented in the user file
     */

}

/**
  * @brief  UART LL Parity Error Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_ParityErrCallback(UART_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_UART_ParityErrCallback could be implemented in the user file
     */

}

/**
  * @brief  UART LL RX Overrun Error Interrupt Callback
  * @param  Instance Specifies UART peripheral
  * @return None
  */
__WEAK void LL_UART_RxOverrunErrCallback(UART_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_UART_RxOverrunErrCallback could be implemented in the user file
     */

}

/**
  * @}
  */

/**
  * @}
  */


/* Private functions ---------------------------------------------------------*/
/** @defgroup UART_LL_Private_Functions UART LL Private function
  * @brief    UART LL Private function
  * @{
  */


#ifdef LL_DMA_MODULE_ENABLED
/**
  * @brief  UART LL DMA Init
  * @param  dma_user_cfg user dma config pointer
  * @param  Timeout Limited operation time
  * @return DMA_ChannelETypeDef
  */
DMA_ChannelETypeDef LL_UART_DMA_Init(DMA_UserCfgTypeDef *dma_user_cfg)
{
    DMA_ChannelETypeDef ch = DMA_CHANNEL_INVALID;
    int ret;

    /* User DAM channel request */
    ch = LL_DMA_ChannelRequest();

    if (ch == DMA_CHANNEL_INVALID) {
        LOG_E("Requset DMA channel Fail!\n");
        return DMA_CHANNEL_INVALID;
    }

    /* User DMA init */
    ret = LL_DMA_Init(DMA, ch, dma_user_cfg);

    if (ret) {
        LOG_E("DMA LL init fail!\n");
        LL_DMA_ChannelRelease(ch);
        ch = DMA_CHANNEL_INVALID;
        return DMA_CHANNEL_INVALID;
    }

    return ch;
}

/**
  * @brief  UART LL DMA Deinit
  * @param  Instance Specifies UART peripheral
  * @param  frame frame pointer
  * @return LL_StatusETypeDef
  */
LL_StatusETypeDef LL_UART_DMA_Deinit(DMA_ChannelETypeDef ch)
{
    /* Deinit DMA after tranfer completed */
    LL_DMA_DeInit(DMA, ch);

    /* DMA LL channel release */
    LL_DMA_ChannelRelease(ch);

    /* DMA stop work */
    LL_DMA_Stop_IT(DMA, ch);

    return LL_OK;
}

/**
  * @brief  UART DMA TX Channel complete callback
  * @param  arg Unlimited parameter
  * @return None
  */
void LL_UART_DMA_TXCHEndCallback(void *arg)
{
    UART_DMAHandleTypeDef *p_frame = ((UART_DMAHandleTypeDef *)arg);

    /* Wait for UART Transmit completed */
    while (!__LL_UART_IsTxEmpty(p_frame->Instance));

    /* UART LL DMA Deinit */
    LL_UART_DMA_Deinit(p_frame->dma_tx_ch);

    /* Disable Transmit DMA func */
    p_frame->dma_tx_ch = DMA_CHANNEL_INVALID;

    /* process has been finished */
    p_frame->TXdma_status = UART_DMA_STATE_FINISH;
}

/**
  * @brief  UART DMA RX Channel complete callback
  * @param  arg Unlimited parameter
  * @return None
  */
void LL_UART_DMA_RXCHEndCallback(void *arg)
{
    UART_DMAHandleTypeDef *p_frame = ((UART_DMAHandleTypeDef *)arg);

    /* UART LL DMA Deinit */
    LL_UART_DMA_Deinit(p_frame->dma_rx_ch);

    p_frame->dma_rx_ch = DMA_CHANNEL_INVALID;

    /* process has been finished */
    p_frame->RXdma_status = UART_DMA_STATE_FINISH;
}

/**
  * @brief  UART DMA TX Channel Error callback
  * @param  arg Unlimited parameter
  * @return None
  */
void LL_UART_DMA_TXCHErrorCallBck(void *arg)
{
    UART_DMAHandleTypeDef *p_frame = ((UART_DMAHandleTypeDef *)arg);

    /* UART LL DMA Deinit */
    LL_UART_DMA_Deinit(p_frame->dma_tx_ch);

    p_frame->dma_tx_ch = DMA_CHANNEL_INVALID;

    /* process has been Error */
    p_frame->TXdma_status = UART_DMA_STATE_ERROR;
}

/**
  * @brief  UART DMA RX Channel Error callback
  * @param  arg Unlimited parameter
  * @return None
  */
void LL_UART_DMA_RXCHErrorCallBck(void *arg)
{
    UART_DMAHandleTypeDef *p_frame = ((UART_DMAHandleTypeDef *)arg);

    /* UART LL DMA Deinit */
    LL_UART_DMA_Deinit(p_frame->dma_rx_ch);

    p_frame->dma_rx_ch = DMA_CHANNEL_INVALID;

    /* process has been Error */
    p_frame->RXdma_status = UART_DMA_STATE_ERROR;
}

#endif  /* LL_DMA_MODULE_ENABLED */
/**
  * @}
  */


#endif  /* LL_UART_MODULE_ENABLED */


/**
  * @}
  */

/**
  * @}
  */


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