WXK
2024-12-16 78e84fcf264afd731cd66c807d9fcb690fe12126
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
/**************************************************************************
 * @file     pan_uart.h
 * @version  V1.10
 * $Revision: 2 $
 * $Date:    2023/11/08  $  
 * @brief    Panchip series UART driver header file
 *
 * @note
 * Copyright (C) 2023 Panchip Technology Corp. All rights reserved.
 *****************************************************************************/
 
#ifndef __PAN_UART_H__
#define __PAN_UART_H__
 
/**
 * @brief Uart Interface
 * @defgroup uart_interface Uart Interface
 * @{
 */
#ifdef __cplusplus
extern "C"
{
#endif
 
 
 
/**@defgroup UART_TX_TRIG_FLAG Uart transmit fifo threshold 
 * @brief       Uart transmit fifo threshold definition
 * @{ */
typedef enum UART_TxTrigger
{
    UART_TX_FIFO_EMPTY              = 0x0,
    UART_TX_FIFO_TWO_CHARS          = 0x1,
    UART_TX_FIFO_QUARTER_FULL       = 0x2,
    UART_TX_FIFO_HALF_FULL          = 0x3
} UART_TxTriggerDef;
/**@} */
 
/**@defgroup UART_RX_TRIG_FLAG Uart receive fifo threshold 
 * @brief       Uart receive fifo threshold definition
 * @{ */
typedef enum UART_RxTrigger
{
    UART_RX_FIFO_ONE_CHAR           = 0x0,
    UART_RX_FIFO_QUARTER_FULL       = 0x1,
    UART_RX_FIFO_HALF_FULL          = 0x2,
    UART_RX_FIFO_TWO_LESS_THAN_FULL = 0x3
} UART_RxTriggerDef;
/**@} */
 
 
/**@defgroup UART_LINE_CONTROL_FLAG Uart line control 
 * @brief       This is the data type used for manipulation of the UART line control settings
 * @{ */
 
typedef enum UART_LineCtrl
{
    Uart_Line_5n1       = 0x00,     // 5 data bits, no parity, 1 stop bit
    Uart_Line_5n1_5     = 0x04,     // 5 data bits, no parity, 1.5 stop bits
    Uart_Line_5e1       = 0x18,     // 5 data bits, even parity, 1 stop bit
    Uart_Line_5e1_5     = 0x1c,     // 5 data bits, even parity, 1.5 stop bits
    Uart_Line_5o1       = 0x08,     // 5 data bits, odd parity, 1 stop bit
    Uart_Line_5o1_5     = 0x0c,     // 5 data bits, odd parity, 1.5 stop bits
    Uart_Line_5s1       = 0x38,     // 5 data bits, space parity, 1 stop bit
    Uart_Line_5s1_5     = 0x3c,     // 5 data bits, space parity, 1.5 stop bits
    Uart_Line_5m1       = 0x28,     // 5 data bits, mark parity, 1 stop bit
    Uart_Line_5m1_5     = 0x2c,     // 5 data bits, mark parity, 1.5 stop bits
    Uart_Line_6n1       = 0x01,     // 6 data bits, no parity, 1 stop bit
    Uart_Line_6n2       = 0x05,     // 6 data bits, no parity, 2 stop bits
    Uart_Line_6e1       = 0x19,     // 6 data bits, even parity, 1 stop bit
    Uart_Line_6e2       = 0x1d,     // 6 data bits, even parity, 2 stop bits
    Uart_Line_6o1       = 0x09,     // 6 data bits, odd parity, 1 stop bit
    Uart_Line_6o2       = 0x0d,     // 6 data bits, odd parity, 2 stop bits
    Uart_Line_6s1       = 0x39,     // 6 data bits, space parity, 1 stop bit
    Uart_Line_6s2       = 0x3d,     // 6 data bits, space parity, 2 stop bits
    Uart_Line_6m1       = 0x29,     // 6 data bits, mark parity, 1 stop bit
    Uart_Line_6m2       = 0x2d,     // 6 data bits, mark parity, 2 stop bits
    Uart_Line_7n1       = 0x02,     // 7 data bits, no parity, 1 stop bit
    Uart_Line_7n2       = 0x06,     // 7 data bits, no parity, 2 stop bits
    Uart_Line_7e1       = 0x1a,     // 7 data bits, even parity, 1 stop bit
    Uart_Line_7e2       = 0x1e,     // 7 data bits, even parity, 2 stop bits
    Uart_Line_7o1       = 0x0a,     // 7 data bits, odd parity, 1 stop bit
    Uart_Line_7o2       = 0x0e,     // 7 data bits, odd parity, 2 stop bits
    Uart_Line_7s1       = 0x3a,     // 7 data bits, space parity, 1 stop bit
    Uart_Line_7s2       = 0x3e,     // 7 data bits, space parity, 2 stop bits
    Uart_Line_7m1       = 0x2a,     // 7 data bits, mark parity, 1 stop bit
    Uart_Line_7m2       = 0x2e,     // 7 data bits, mark parity, 2 stop bits
    Uart_Line_8n1       = 0x03,     // 8 data bits, no parity, 1 stop bit
    Uart_Line_8n2       = 0x07,     // 8 data bits, no parity, 2 stop bits
    Uart_Line_8e1       = 0x1b,     // 8 data bits, even parity, 1 stop bit
    Uart_Line_8e2       = 0x1f,     // 8 data bits, even parity, 2 stop bits
    Uart_Line_8o1       = 0x0b,     // 8 data bits, odd parity, 1 stop bit
    Uart_Line_8o2       = 0x0f,     // 8 data bits, odd parity, 2 stop bits
    Uart_Line_8s1       = 0x3b,     // 8 data bits, space parity, 1 stop bit
    Uart_Line_8s2       = 0x3f,     // 8 data bits, space parity, 2 stop bits
    Uart_Line_8m1       = 0x2b,     // 8 data bits, mark parity, 1 stop bit
    Uart_Line_8m2       = 0x2f,     // 8 data bits, mark parity, 2 stop bits
} UART_LineCtrlDef;
/**@} */
 
/**@defgroup UART_INT_FLAG Uart interrupts status 
 * @brief    These are the bit definitions used for managing UART interrupts.
 *  The functionality of ETBEI and ERBFI alters when programmable THRE
 *  interrupt mode is active (dw_uart_enablePtime()).  See the
 *  DW_apb_uart databook for a detailed description.
 * @{ */
 
typedef enum UART_Irq
{
    UART_IRQ_RECV_DATA_AVL  = 0x01,     // receive data available
    UART_IRQ_THR_EMPTY      = 0x02,     // transmitter holding register empty
    UART_IRQ_LINE_STATUS    = 0x04,     // receiver line status
    UART_IRQ_MODEM_STATUS   = 0x08,     // modem status
    UART_IRQ_ALL            = 0x0f      // all interrupts
} UART_IrqDef;
/**@} */
 
 
/**@defgroup UART_EVENT_FLAG Uart event mode
 * @brief    This is the data type used for specifying UART events.  An event is
 *  the occurrence of an interrupt which must be handled appropriately.
 *  One of these events is passed at a time to the user listener
 *  function to be processed.  The exception to this are the
 *  UART_EVENT_THR_EMPTY and UART_EVENT_TIMEOUT interrupts which are handled
 *  internally by the interrupt handler.
 * @{ */
 
typedef enum UART_Event
{
    UART_EVENT_MODEM        = 0x0,  // CTR, DSR, RI or DCD status changed.
    UART_EVENT_NONE         = 0x1,  // No event/interrupt pending.
    UART_EVENT_THR_EMPTY    = 0x2,  // Transmit holding register empty or TX
                                    // FIFO at or below trigger threshold.
    UART_EVENT_DATA         = 0x4,  // Receive buffer register data
                                    // available (non-FIFO mode) or RX FIFO
                                    // trigger level reached.
    UART_EVENT_LINE         = 0x6,  // Overrun/parity/framing error or break
                                    // interrupt occurred.
#if 0
    UART_EVENT_BUSY         = 0x7,  // Attempt to write to the LCR[7] while
                                    // DW_apb_uart was busy (DLAB).
#endif
    UART_EVENT_TIMEOUT      = 0xc   // No characters in or out of the
                                    // receiver FIFO during the last four
                                    // character times and there is at least
                                    // one character in it during this time.
} UART_EventDef;
/**@} */
 
 
/**@defgroup TIMER_LINE_STATUS_FLAG Uart line status
 * @brief    These are the definitions used for reporting the line status
 *  including errors, if any.  Note that they represent bits of an 8-bit
 *  register and more than one can be active at any time.
 * @{ */
typedef enum UART_LineStatus
{
    UART_LINE_DATA_RDY          = 0x01,     // data ready
    UART_LINE_OVERRUN_ERR       = 0x02,     // overrun error
    UART_LINE_PARITY_ERR        = 0x04,     // parity error
    UART_LINE_FRAME_ERR         = 0x08,     // framing error
    UART_LINE_BREAK_INT         = 0x10,     // break interrupt
    UART_LINE_THRE              = 0x20,     // transmit holding register empty (or tx fifo full)
    UART_LINE_TXSR_EMPTY        = 0x40,     // transmitter shift register empty
    UART_LINE_RECV_FIFO_EMPTY   = 0x80,     // receiver FIFO error
    UART_LINE_ADDR_RCVD         = 0x100     // address received (9-bit mode)
} UART_LineStatusDef;
/**@} */
 
/**@defgroup TIMER_MODEM_STATUS_FLAG Uart modem status
 * @brief    Uart modem status definitions
 * @{ */
typedef enum UART_ModemStatus
{
    UART_MODEM_DCTS     = 0x01,     // delta clear to send
    UART_MODEM_DDSR     = 0x02,     // delta data set ready
    UART_MODEM_TERI     = 0x04,     // trailing edge of ring indicator
    UART_MODEM_DDCD     = 0x08,     // delta data carrier detect
    UART_MODEM_CTS      = 0x10,     // clear to send
    UART_MODEM_DSR      = 0x20,     // data set ready
    UART_MODEM_RI       = 0x40,     // ring indicator
    UART_MODEM_DCD      = 0x80      // data carrier detect
} UART_ModemStatusDef;
/**@} */
 
 
/** 
 * @brief Structure with uart init feature.
 *
 * @param UART_BaudRate     set initial baudrate.
 * @param UART_LineCtrl        set initial line control parameter. \ref UART_LineCtrlDef
 */  
typedef struct
{
    uint32_t UART_BaudRate;             // set initial baudrate
    UART_LineCtrlDef UART_LineCtrl;     // set initial line control parameter
} UART_InitTypeDef;
 
 
 
 
/**
  * @brief  This function is used to enable stick parity
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_EnableStickParity(UART_T* UARTx)
{
    UARTx->LCR |= UART_LCR_SP_Msk;
}
 
/**
  * @brief  This function is used to disable stick parity
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_DisableStickParity(UART_T* UARTx)
{
    UARTx->LCR &= ~UART_LCR_SP_Msk;
}
 
/**
  * @brief  This function is used to get line status
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE UART_LineStatusDef UART_GetLineStatus(UART_T* UARTx)
{
    return (UART_LineStatusDef)(UARTx->LSR & UART_LSR_LINE_STATUS_Msk);
}
 
__STATIC_INLINE UART_ModemStatusDef UART_GetModemStatus(UART_T* UARTx)
{
    return (UART_ModemStatusDef)(UARTx->MSR & UART_MSR_MODEM_STATUS_Msk);
}
 
/**
 * @brief     This function enables receive and transmit FIFOs, if they are
 *  available.
 * @param  UARTx: where x can be 1, 2 to select the
 *         UART peripheral.
 * @retval None
 */
__STATIC_INLINE void UART_EnableFifo(UART_T* UARTx)
{
    // SCR(r/w) stores a copy of FCR(wo)
    UARTx->SCR |= UART_FCR_FIFOE_Msk;
 
    UARTx->IIR_FCR = UARTx->SCR;
}
 
/**
 * @brief     This function disables receive and transmit FIFOs.
 * @param  UARTx: where x can be 1, 2 to select the
 *         UART peripheral.
 * @retval None
 */
 
__STATIC_INLINE void UART_DisableFifo(UART_T* UARTx)
{
    // SCR(r/w) stores a copy of FCR(wo)
    UARTx->SCR &= ~UART_FCR_FIFOE_Msk;
 
    UARTx->IIR_FCR = UARTx->SCR;
}
 
/**
 * @brief     Adjust whether the FIFOs enabled or not.
 * @param  UARTx: where x can be 1, 2 to select the
 *         UART peripheral.
 * @retval None
 */
__STATIC_INLINE bool UART_IsFifoEnabled(UART_T* UARTx)
{
    return (UARTx->IIR_FCR & UART_IIR_FIFOSE_Msk) ? true : false;
}
 
/**
    * @brief  This function is used to reset rx fifo
    * @param  UARTx: where x can be 1, 2 to select the
    *         UART peripheral.
    * @return None
    */
__STATIC_INLINE void UART_ResetRxFifo(UART_T* UARTx)
{
    // SCR(r/w) stores a copy of FCR(wo)
    UARTx->SCR |= UART_FCR_RFIFOR_Msk;
 
    UARTx->IIR_FCR = UARTx->SCR;
    UARTx->SCR &= ~UART_FCR_RFIFOR_Msk; //Clear RFIFOR bit in SCR after use
 
    // Read Rx FIFO once again to de-assert rx interrupt if there is
    volatile uint32_t val = UARTx->RBR_THR_DLL;
    val = val;
}
 
/**
    * @brief  This function is used to reset tx fifo
    * @param  UARTx: where x can be 1, 2 to select the
    *         UART peripheral.
    * @return None
    */
__STATIC_INLINE void UART_ResetTxFifo(UART_T* UARTx)
{
    // SCR(r/w) stores a copy of FCR(wo)
    UARTx->SCR |= UART_FCR_XFIFOR_Msk;
 
    UARTx->IIR_FCR = UARTx->SCR;
    UARTx->SCR &= ~UART_FCR_XFIFOR_Msk; //Clear XFIFOR bit in SCR after use
}
 
/**
    * @brief    Set the trigger level of the transmitter FIFO empty interrupt.
    * @param  UARTx: where x can be 1, 2 to select the
    *         UART peripheral.
    * @param  level: Uart transmit fifo threshold \ref UART_TxTriggerDef
    * @return None
    */
__STATIC_INLINE void UART_SetTxTrigger(UART_T* UARTx, UART_TxTriggerDef level)
{
    // SCR(r/w) stores a copy of FCR(wo)
    UARTx->SCR &= ~UART_FCR_TET_Msk;
    UARTx->SCR |= (level << UART_FCR_TET_Pos);
 
    UARTx->IIR_FCR = UARTx->SCR;
}
 
 
/**
    * @brief    Sets the trigger level of the receiver FIFO full interrupt.
    * @param  UARTx: where x can be 1, 2 to select the
    *         UART peripheral.
    * @param  level: Uart receive fifo threshold \ref UART_RxTriggerDef
    * @return None
    */
__STATIC_INLINE void UART_SetRxTrigger(UART_T* UARTx, UART_RxTriggerDef level)
{
    // SCR(r/w) stores a copy of FCR(wo)
    UARTx->SCR &= ~UART_FCR_RT_Msk;
    UARTx->SCR |= (level << UART_FCR_RT_Pos);
 
    UARTx->IIR_FCR = UARTx->SCR;
}
 
/**
    * @brief    Get the transmit fifo level.
    * @param  UARTx: where x can be 1, 2 to select the
    *         UART peripheral.
    * @return how many bytes deep the transmitter and receiver FIFOs are
    */
__STATIC_INLINE uint8_t UART_GetTxFifoLevel(UART_T* UARTx)
{
    return (uint8_t)((UARTx->TFL & UART_TFL_Msk) >> UART_TFL_Pos);
}
 
/**
    * @brief    Get the receive fifo level.
    * @param  UARTx: where x can be 1, 2 to select the
    *         UART peripheral.
    * @return the number of characters currently present in the Rx fifo.
    */
__STATIC_INLINE uint8_t UART_GetRxFifoLevel(UART_T* UARTx)
{
     return (uint8_t)((UARTx->RFL & UART_RFL_Msk) >> UART_RFL_Pos);
}
 
/**
  * @brief  Enable auto flow control mode
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_EnableAfc(UART_T *UARTx)
{
    UARTx->MCR |= UART_MCR_AFCE_Msk;
}
 
/**
  * @brief  Disable auto flow control mode
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_DisableAfc(UART_T *UARTx)
{
    UARTx->MCR &= ~UART_MCR_AFCE_Msk;
}
 
/**
  * @brief  check auto flow control mode enable or not
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE bool UART_IsAfcEnabled(UART_T *UARTx)
{
    return (UARTx->MCR & UART_MCR_AFCE_Msk) ? true : false;
}
 
/**
  * @brief  uart send data function
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  Data: data need to seed
  * @retval None
  */
__STATIC_INLINE void UART_SendData(UART_T* UARTx, uint8_t Data)
{
    UARTx->RBR_THR_DLL = Data;
}
 
/**
  * @brief  uart receive data function
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval received data
  */
__STATIC_INLINE uint8_t UART_ReceiveData(UART_T* UARTx)
{
    return (uint8_t)UARTx->RBR_THR_DLL;
}
 
/**
  * @brief  programmable THRE interrupt mode enable
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval none
  */
__STATIC_INLINE void UART_EnablePtime(UART_T* UARTx)
{
    UARTx->IER_DLH |= UART_IER_EPTI_Msk;
}
 
/**
  * @brief  programmable THRE interrupt mode disable
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval none
  */
__STATIC_INLINE void UART_DisablePtime(UART_T* UARTx)
{
    UARTx->IER_DLH &= ~UART_IER_EPTI_Msk;
}
 
/**
  * @brief  check programmable THRE interrupt mode enable or not
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval none
  */
__STATIC_INLINE bool UART_IsPtimeEnabled(UART_T* UARTx)
{
    return (UARTx->IER_DLH & UART_IER_EPTI_Msk) ? true : false;
}
 
/**
  * @brief  enable interrupt,include Modem status linestatus,THRE,receive data
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  irq: interrupt type
  * @retval none
  */
__STATIC_INLINE void UART_EnableIrq(UART_T* UARTx, UART_IrqDef irq)
{
    UARTx->IER_DLH |= ((irq << UART_IER_ALL_IRQ_Pos) & UART_IER_ALL_IRQ_Msk);
}
 
/**
  * @brief  disable interrupt
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  irq: interrupt type
  * @retval none
  */
__STATIC_INLINE void UART_DisableIrq(UART_T* UARTx, UART_IrqDef irq)
{
    UARTx->IER_DLH &= ~((irq << UART_IER_ALL_IRQ_Pos) & UART_IER_ALL_IRQ_Msk);
}
 
/**
  * @brief  check interrupt enable or not
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  irq: interrupt type
  * @retval none
  */
__STATIC_INLINE bool UART_IsIrqEnabled(UART_T* UARTx, UART_IrqDef irq)
{
    return (UARTx->IER_DLH & ((irq << UART_IER_ALL_IRQ_Pos) & UART_IER_ALL_IRQ_Msk)) ? true : false;
}
 
/**
  * @brief  get which interrupt is actived 
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval interrupt type
  */
__STATIC_INLINE uint8_t UART_GetIrqMasked(UART_T* UARTx)
{
    return (uint8_t)((UARTx->IER_DLH & UART_IER_ALL_IRQ_Msk) >> UART_IER_ALL_IRQ_Pos);
}
 
/**
  * @brief  get active interrupt ID 
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval interrupt ID
  */
__STATIC_INLINE UART_EventDef UART_GetActiveEvent(UART_T * UARTx)
{
    return (UART_EventDef)((UARTx->IIR_FCR & UART_IIR_IID_Msk) >> UART_IIR_IID_Pos);
}
 
/**
  * @brief  check tx fifo empty or not
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval true or false
  */
__STATIC_INLINE bool UART_IsTxFifoEmpty(UART_T* UARTx)
{
    return (UARTx->USR & UART_USR_TFE_Msk) ? true : false;
}
 
/**
  * @brief  check tx fifo full or not
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval true or false
  */
__STATIC_INLINE bool UART_IsTxFifoFull(UART_T* UARTx)
{
    return (UARTx->USR & UART_USR_TFNF_Msk) ? false : true;
}
 
/**
  * @brief  check rx fifo empty or not
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval true or false
  */
__STATIC_INLINE bool UART_IsRxFifoEmpty(UART_T* UARTx)
{
    return (UARTx->USR & UART_USR_RFNE_Msk) ? false : true;
}
 
/**
  * @brief  check rx fifo full or not
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval true or false
  */
__STATIC_INLINE bool UART_IsRxFifoFull(UART_T* UARTx)
{
    return (UARTx->USR & UART_USR_RFF_Msk) ? true : false;
}
 
/**
  * @brief  enable halt transmit,this function is used to halt transmit for test
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval true or false
  */
__STATIC_INLINE void UART_EnableHaltTX(UART_T* UARTx)
{
    UARTx->HTX = 0x1;
}
 
/**
  * @brief  disable halt transmit
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval true or false
  */
__STATIC_INLINE void UART_DisableHaltTX(UART_T* UARTx)
{
    UARTx->HTX = 0x0;
}
 
/**
  * @brief  Enable request to send in flow control mode
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_EnableRts(UART_T* UARTx)
{
    UARTx->MCR |= UART_MCR_RTS_Msk;
}
 
/**
  * @brief  Disable request to send in flow control mode
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @return None
  */
__STATIC_INLINE void UART_DisableRts(UART_T* UARTx)
{
    UARTx->MCR &= ~UART_MCR_RTS_Msk;
}
 
/**
  * @brief  Enable 9-bit data transmit and receive transfer
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_Enable9BitData( UART_T* UARTx)
{
    UARTx->LCR_EXT |= UART_LCR_EXT_DLS_E_Msk;
}
 
/**
  * @brief  Disable 9-bit data transmit and receive transfer
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_Disable9BitData( UART_T* UARTx)
{
    UARTx->LCR_EXT &= ~UART_LCR_EXT_DLS_E_Msk;
}
 
/**
  * @brief  Enable address match feature during receive
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_EnableAddrMatchMode(UART_T* UARTx)
{
   UARTx->LCR_EXT |= UART_LCR_EXT_ADDR_MATCH_Msk;
}
 
/**
  * @brief  Disable address match feature during receive
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_DisableAddrMatchMode(UART_T* UARTx)
{
   UARTx->LCR_EXT &= ~UART_LCR_EXT_ADDR_MATCH_Msk;
}
 
/**
  * @brief  UART Send Address,9-bit character will be transmitted with 9-th bit set to 1 
  * and the remaining 8-bits will match to what is being programmed in "Transmit Address Register"
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @retval None
  */
__STATIC_INLINE void UART_SendAddr(UART_T* UARTx)
{
   UARTx->LCR_EXT |= UART_LCR_EXT_SEND_ADDR_Msk;
}
 
/**
  * @brief  This is an address matching register during receive mode,Set Receive Address
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  addr: Receive Address
  * @retval None
  */
__STATIC_INLINE void UART_SetReceiveAddress( UART_T* UARTx,uint32_t addr)
{
    UARTx->RAR = addr;
}
 
/**
  * @brief  This is an address matching register during transmit mode,Set Transmit Address
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  mode: Transmit Address
  * @retval None
  */
__STATIC_INLINE void UART_SetTransmitAddress( UART_T* UARTx,uint32_t addr)
{
    UARTx->TAR = addr;
}
 
/**
  * @brief  Initializes the UARTx peripheral according to the specified
  *         parameters in the UART_InitStruct .
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  UART_InitStruct: pointer to a UART_InitTypeDef structure that contains
  *         the configuration information for the specified UART peripheral.
  * @return UART init success (true) or fail (false)
  * @code
  *
  *    UART_InitTypeDef Init_Struct;
    *
    *    Init_Struct.UART_BaudRate = 115200;
  *    Init_Struct.UART_LineCtrl = Uart_Line_8n1;
    *
  *    UART_Init(UART1, &Init_Struct);
  *    UART_EnableFifo(UART1);
  *
  * @endcode
  */
extern bool UART_Init(UART_T* UARTx, UART_InitTypeDef* UART_InitStruct);
/**
  * @brief  Deinit the UARTx peripheral
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @return None
  */
extern void UART_DeInit(UART_T* UARTx );
 
/**
  * @brief  This function is used to send multiple data
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  Buf: pointer to send data storage location. 
  * @param  BufSize: used to indicate the offset of buf.
  * @param  SendSize: used to indicate the size need to send.     
  * @return Send success (true) or fail (false)
  */
extern bool UART_SendMultipleData(UART_T* UARTx, uint8_t* Buf, size_t BufSize, size_t SendSize);
 
/**
  * @brief  This function is used to receive multiple data
  * @param  UARTx: where x can be 1, 2 to select the
  *         UART peripheral.
  * @param  Buf: pointer to receive data storage location 
  * @param  BufSize: used to indicate the offset of buf
  * @param  ExpectSize: used to indicate the size expect to receive  
  * @return Receive success (true) or fail (false)
  */
extern bool UART_ReceiveMultipleData(UART_T* UARTx, uint8_t* Buf, size_t BufSize, size_t ExpectSize);
 
 
/**@} */
 
#ifdef __cplusplus
}
#endif
 
#endif //__PAN_UART_H__
 
/*** (C) COPYRIGHT 2023 Panchip Technology Corp. ***/