PAN1080 Peripheral API
pan_uart.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Panchip Technology Corp. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
14#ifndef __PAN_UART_H__
15#define __PAN_UART_H__
16
22#ifdef __cplusplus
23extern "C"
24{
25#endif
26
27
28
32typedef enum UART_TxTrigger
33{
44typedef enum UART_RxTrigger
45{
58typedef enum UART_LineCtrl
59{
110typedef enum UART_Irq
111{
116 UART_IRQ_ALL = 0x0f
130typedef enum UART_Event
131{
137#if 0
138 UART_EVENT_BUSY = 0x7,
139#endif
140 UART_EVENT_TIMEOUT = 0xc
151typedef enum UART_LineStatus
152{
161 UART_LINE_ADDR_RCVD = 0x100
169{
177 UART_MODEM_DCD = 0x80
188typedef struct
189{
190 uint32_t UART_BaudRate; // set initial baudrate
191 UART_LineCtrlDef UART_LineCtrl; // set initial line control parameter
193
194
195
196
203__STATIC_INLINE void UART_EnableStickParity(UART_T* UARTx)
204{
205 UARTx->LCR |= UART_LCR_SP_Msk;
206}
207
214__STATIC_INLINE void UART_DisableStickParity(UART_T* UARTx)
215{
216 UARTx->LCR &= ~UART_LCR_SP_Msk;
217}
218
225__STATIC_INLINE UART_LineStatusDef UART_GetLineStatus(UART_T* UARTx)
226{
227 return (UART_LineStatusDef)(UARTx->LSR & UART_LSR_LINE_STATUS_Msk);
228}
229
230__STATIC_INLINE UART_ModemStatusDef UART_GetModemStatus(UART_T* UARTx)
231{
232 return (UART_ModemStatusDef)(UARTx->MSR & UART_MSR_MODEM_STATUS_Msk);
233}
234
242__STATIC_INLINE void UART_EnableFifo(UART_T* UARTx)
243{
244 // SCR(r/w) stores a copy of FCR(wo)
245 UARTx->SCR |= UART_FCR_FIFOE_Msk;
246
247 UARTx->IIR_FCR = UARTx->SCR;
248}
249
257__STATIC_INLINE void UART_DisableFifo(UART_T* UARTx)
258{
259 // SCR(r/w) stores a copy of FCR(wo)
260 UARTx->SCR &= ~UART_FCR_FIFOE_Msk;
261
262 UARTx->IIR_FCR = UARTx->SCR;
263}
264
271__STATIC_INLINE bool UART_IsFifoEnabled(UART_T* UARTx)
272{
273 return (UARTx->IIR_FCR & UART_IIR_FIFOSE_Msk) ? true : false;
274}
275
282__STATIC_INLINE void UART_ResetRxFifo(UART_T* UARTx)
283{
284 // SCR(r/w) stores a copy of FCR(wo)
285 UARTx->SCR |= UART_FCR_RFIFOR_Msk;
286
287 UARTx->IIR_FCR = UARTx->SCR;
288 UARTx->SCR &= ~UART_FCR_RFIFOR_Msk; //Clear RFIFOR bit in SCR after use
289
290 // Read Rx FIFO once again to de-assert rx interrupt if there is
291 volatile uint32_t val = UARTx->RBR_THR_DLL;
292 val = val;
293}
294
301__STATIC_INLINE void UART_ResetTxFifo(UART_T* UARTx)
302{
303 // SCR(r/w) stores a copy of FCR(wo)
304 UARTx->SCR |= UART_FCR_XFIFOR_Msk;
305
306 UARTx->IIR_FCR = UARTx->SCR;
307 UARTx->SCR &= ~UART_FCR_XFIFOR_Msk; //Clear XFIFOR bit in SCR after use
308}
309
317__STATIC_INLINE void UART_SetTxTrigger(UART_T* UARTx, UART_TxTriggerDef level)
318{
319 // SCR(r/w) stores a copy of FCR(wo)
320 UARTx->SCR &= ~UART_FCR_TET_Msk;
321 UARTx->SCR |= (level << UART_FCR_TET_Pos);
322
323 UARTx->IIR_FCR = UARTx->SCR;
324}
325
326
334__STATIC_INLINE void UART_SetRxTrigger(UART_T* UARTx, UART_RxTriggerDef level)
335{
336 // SCR(r/w) stores a copy of FCR(wo)
337 UARTx->SCR &= ~UART_FCR_RT_Msk;
338 UARTx->SCR |= (level << UART_FCR_RT_Pos);
339
340 UARTx->IIR_FCR = UARTx->SCR;
341}
342
349__STATIC_INLINE uint8_t UART_GetTxFifoLevel(UART_T* UARTx)
350{
351 return (uint8_t)((UARTx->TFL & UART_TFL_Msk) >> UART_TFL_Pos);
352}
353
360__STATIC_INLINE uint8_t UART_GetRxFifoLevel(UART_T* UARTx)
361{
362 return (uint8_t)((UARTx->RFL & UART_RFL_Msk) >> UART_RFL_Pos);
363}
364
371__STATIC_INLINE void UART_EnableAfc(UART_T *UARTx)
372{
373 UARTx->MCR |= UART_MCR_AFCE_Msk;
374}
375
382__STATIC_INLINE void UART_DisableAfc(UART_T *UARTx)
383{
384 UARTx->MCR &= ~UART_MCR_AFCE_Msk;
385}
386
393__STATIC_INLINE bool UART_IsAfcEnabled(UART_T *UARTx)
394{
395 return (UARTx->MCR & UART_MCR_AFCE_Msk) ? true : false;
396}
397
405__STATIC_INLINE void UART_SendData(UART_T* UARTx, uint8_t Data)
406{
407 UARTx->RBR_THR_DLL = Data;
408}
409
416__STATIC_INLINE uint8_t UART_ReceiveData(UART_T* UARTx)
417{
418 return (uint8_t)UARTx->RBR_THR_DLL;
419}
420
427__STATIC_INLINE void UART_EnablePtime(UART_T* UARTx)
428{
429 UARTx->IER_DLH |= UART_IER_EPTI_Msk;
430}
431
438__STATIC_INLINE void UART_DisablePtime(UART_T* UARTx)
439{
440 UARTx->IER_DLH &= ~UART_IER_EPTI_Msk;
441}
442
449__STATIC_INLINE bool UART_IsPtimeEnabled(UART_T* UARTx)
450{
451 return (UARTx->IER_DLH & UART_IER_EPTI_Msk) ? true : false;
452}
453
461__STATIC_INLINE void UART_EnableIrq(UART_T* UARTx, UART_IrqDef irq)
462{
463 UARTx->IER_DLH |= ((irq << UART_IER_ALL_IRQ_Pos) & UART_IER_ALL_IRQ_Msk);
464}
465
473__STATIC_INLINE void UART_DisableIrq(UART_T* UARTx, UART_IrqDef irq)
474{
475 UARTx->IER_DLH &= ~((irq << UART_IER_ALL_IRQ_Pos) & UART_IER_ALL_IRQ_Msk);
476}
477
485__STATIC_INLINE bool UART_IsIrqEnabled(UART_T* UARTx, UART_IrqDef irq)
486{
487 return (UARTx->IER_DLH & ((irq << UART_IER_ALL_IRQ_Pos) & UART_IER_ALL_IRQ_Msk)) ? true : false;
488}
489
496__STATIC_INLINE uint8_t UART_GetIrqMasked(UART_T* UARTx)
497{
498 return (uint8_t)((UARTx->IER_DLH & UART_IER_ALL_IRQ_Msk) >> UART_IER_ALL_IRQ_Pos);
499}
500
507__STATIC_INLINE UART_EventDef UART_GetActiveEvent(UART_T * UARTx)
508{
509 return (UART_EventDef)((UARTx->IIR_FCR & UART_IIR_IID_Msk) >> UART_IIR_IID_Pos);
510}
511
518__STATIC_INLINE bool UART_IsTxFifoEmpty(UART_T* UARTx)
519{
520 return (UARTx->USR & UART_USR_TFE_Msk) ? true : false;
521}
522
529__STATIC_INLINE bool UART_IsTxFifoFull(UART_T* UARTx)
530{
531 return (UARTx->USR & UART_USR_TFNF_Msk) ? false : true;
532}
533
540__STATIC_INLINE bool UART_IsRxFifoEmpty(UART_T* UARTx)
541{
542 return (UARTx->USR & UART_USR_RFNE_Msk) ? false : true;
543}
544
551__STATIC_INLINE bool UART_IsRxFifoFull(UART_T* UARTx)
552{
553 return (UARTx->USR & UART_USR_RFF_Msk) ? true : false;
554}
555
562__STATIC_INLINE void UART_EnableHaltTX(UART_T* UARTx)
563{
564 UARTx->HTX = 0x1;
565}
566
573__STATIC_INLINE void UART_DisableHaltTX(UART_T* UARTx)
574{
575 UARTx->HTX = 0x0;
576}
577
584__STATIC_INLINE void UART_EnableRts(UART_T* UARTx)
585{
586 UARTx->MCR |= UART_MCR_RTS_Msk;
587}
588
595__STATIC_INLINE void UART_DisableRts(UART_T* UARTx)
596{
597 UARTx->MCR &= ~UART_MCR_RTS_Msk;
598}
599
606__STATIC_INLINE void UART_Enable9BitData( UART_T* UARTx)
607{
608 UARTx->LCR_EXT |= UART_LCR_EXT_DLS_E_Msk;
609}
610
617__STATIC_INLINE void UART_Disable9BitData( UART_T* UARTx)
618{
619 UARTx->LCR_EXT &= ~UART_LCR_EXT_DLS_E_Msk;
620}
621
628__STATIC_INLINE void UART_EnableAddrMatchMode(UART_T* UARTx)
629{
630 UARTx->LCR_EXT |= UART_LCR_EXT_ADDR_MATCH_Msk;
631}
632
639__STATIC_INLINE void UART_DisableAddrMatchMode(UART_T* UARTx)
640{
641 UARTx->LCR_EXT &= ~UART_LCR_EXT_ADDR_MATCH_Msk;
642}
643
651__STATIC_INLINE void UART_SendAddr(UART_T* UARTx)
652{
653 UARTx->LCR_EXT |= UART_LCR_EXT_SEND_ADDR_Msk;
654}
655
663__STATIC_INLINE void UART_SetReceiveAddress( UART_T* UARTx,uint32_t addr)
664{
665 UARTx->RAR = addr;
666}
667
674__STATIC_INLINE void UART_SetTransmitAddress( UART_T* UARTx,uint32_t addr)
675{
676 UARTx->TAR = addr;
677}
678
699extern bool UART_Init(UART_T* UARTx, UART_InitTypeDef* UART_InitStruct);
706extern void UART_DeInit(UART_T* UARTx );
707
717extern bool UART_SendMultipleData(UART_T* UARTx, uint8_t* Buf, size_t BufSize, size_t SendSize);
718
728extern bool UART_ReceiveMultipleData(UART_T* UARTx, uint8_t* Buf, size_t BufSize, size_t ExpectSize);
729
730
733#ifdef __cplusplus
734}
735#endif
736
737#endif /* __PAN_UART_H__ */
738
739
740
741
742
743
744
745
746
enum UART_LineStatus UART_LineStatusDef
UART_LineStatus
Definition: pan_uart.h:152
@ UART_LINE_PARITY_ERR
Definition: pan_uart.h:155
@ UART_LINE_BREAK_INT
Definition: pan_uart.h:157
@ UART_LINE_THRE
Definition: pan_uart.h:158
@ UART_LINE_ADDR_RCVD
Definition: pan_uart.h:161
@ UART_LINE_DATA_RDY
Definition: pan_uart.h:153
@ UART_LINE_FRAME_ERR
Definition: pan_uart.h:156
@ UART_LINE_RECV_FIFO_EMPTY
Definition: pan_uart.h:160
@ UART_LINE_OVERRUN_ERR
Definition: pan_uart.h:154
@ UART_LINE_TXSR_EMPTY
Definition: pan_uart.h:159
UART_ModemStatus
Definition: pan_uart.h:169
enum UART_ModemStatus UART_ModemStatusDef
@ UART_MODEM_DDSR
Definition: pan_uart.h:171
@ UART_MODEM_DCTS
Definition: pan_uart.h:170
@ UART_MODEM_DSR
Definition: pan_uart.h:175
@ UART_MODEM_CTS
Definition: pan_uart.h:174
@ UART_MODEM_DCD
Definition: pan_uart.h:177
@ UART_MODEM_DDCD
Definition: pan_uart.h:173
@ UART_MODEM_TERI
Definition: pan_uart.h:172
@ UART_MODEM_RI
Definition: pan_uart.h:176
UART_Event
Definition: pan_uart.h:131
enum UART_Event UART_EventDef
@ UART_EVENT_MODEM
Definition: pan_uart.h:132
@ UART_EVENT_TIMEOUT
Definition: pan_uart.h:140
@ UART_EVENT_THR_EMPTY
Definition: pan_uart.h:134
@ UART_EVENT_NONE
Definition: pan_uart.h:133
@ UART_EVENT_LINE
Definition: pan_uart.h:136
@ UART_EVENT_DATA
Definition: pan_uart.h:135
UART_Irq
Definition: pan_uart.h:111
enum UART_Irq UART_IrqDef
@ UART_IRQ_ALL
Definition: pan_uart.h:116
@ UART_IRQ_MODEM_STATUS
Definition: pan_uart.h:115
@ UART_IRQ_LINE_STATUS
Definition: pan_uart.h:114
@ UART_IRQ_RECV_DATA_AVL
Definition: pan_uart.h:112
@ UART_IRQ_THR_EMPTY
Definition: pan_uart.h:113
UART_LineCtrl
Definition: pan_uart.h:59
enum UART_LineCtrl UART_LineCtrlDef
@ Uart_Line_5e1_5
Definition: pan_uart.h:63
@ Uart_Line_6e1
Definition: pan_uart.h:72
@ Uart_Line_8e1
Definition: pan_uart.h:92
@ Uart_Line_7o2
Definition: pan_uart.h:85
@ Uart_Line_8m2
Definition: pan_uart.h:99
@ Uart_Line_5m1
Definition: pan_uart.h:68
@ Uart_Line_8n1
Definition: pan_uart.h:90
@ Uart_Line_7m2
Definition: pan_uart.h:89
@ Uart_Line_6m1
Definition: pan_uart.h:78
@ Uart_Line_7n1
Definition: pan_uart.h:80
@ Uart_Line_8m1
Definition: pan_uart.h:98
@ Uart_Line_5o1_5
Definition: pan_uart.h:65
@ Uart_Line_5e1
Definition: pan_uart.h:62
@ Uart_Line_5n1
Definition: pan_uart.h:60
@ Uart_Line_6e2
Definition: pan_uart.h:73
@ Uart_Line_6n2
Definition: pan_uart.h:71
@ Uart_Line_5s1
Definition: pan_uart.h:66
@ Uart_Line_8s2
Definition: pan_uart.h:97
@ Uart_Line_8o1
Definition: pan_uart.h:94
@ Uart_Line_5m1_5
Definition: pan_uart.h:69
@ Uart_Line_7e2
Definition: pan_uart.h:83
@ Uart_Line_6n1
Definition: pan_uart.h:70
@ Uart_Line_6s1
Definition: pan_uart.h:76
@ Uart_Line_5s1_5
Definition: pan_uart.h:67
@ Uart_Line_8e2
Definition: pan_uart.h:93
@ Uart_Line_8s1
Definition: pan_uart.h:96
@ Uart_Line_5n1_5
Definition: pan_uart.h:61
@ Uart_Line_6m2
Definition: pan_uart.h:79
@ Uart_Line_6o2
Definition: pan_uart.h:75
@ Uart_Line_8n2
Definition: pan_uart.h:91
@ Uart_Line_7e1
Definition: pan_uart.h:82
@ Uart_Line_7o1
Definition: pan_uart.h:84
@ Uart_Line_7s1
Definition: pan_uart.h:86
@ Uart_Line_7m1
Definition: pan_uart.h:88
@ Uart_Line_6o1
Definition: pan_uart.h:74
@ Uart_Line_8o2
Definition: pan_uart.h:95
@ Uart_Line_7n2
Definition: pan_uart.h:81
@ Uart_Line_7s2
Definition: pan_uart.h:87
@ Uart_Line_6s2
Definition: pan_uart.h:77
@ Uart_Line_5o1
Definition: pan_uart.h:64
enum UART_RxTrigger UART_RxTriggerDef
UART_RxTrigger
Definition: pan_uart.h:45
@ UART_RX_FIFO_TWO_LESS_THAN_FULL
Definition: pan_uart.h:49
@ UART_RX_FIFO_HALF_FULL
Definition: pan_uart.h:48
@ UART_RX_FIFO_ONE_CHAR
Definition: pan_uart.h:46
@ UART_RX_FIFO_QUARTER_FULL
Definition: pan_uart.h:47
enum UART_TxTrigger UART_TxTriggerDef
UART_TxTrigger
Definition: pan_uart.h:33
@ UART_TX_FIFO_QUARTER_FULL
Definition: pan_uart.h:36
@ UART_TX_FIFO_EMPTY
Definition: pan_uart.h:34
@ UART_TX_FIFO_HALF_FULL
Definition: pan_uart.h:37
@ UART_TX_FIFO_TWO_CHARS
Definition: pan_uart.h:35
__STATIC_INLINE void UART_DisableIrq(UART_T *UARTx, UART_IrqDef irq)
Disable interrupt.
Definition: pan_uart.h:473
__STATIC_INLINE uint8_t UART_GetTxFifoLevel(UART_T *UARTx)
Get the transmit fifo level.
Definition: pan_uart.h:349
__STATIC_INLINE bool UART_IsRxFifoEmpty(UART_T *UARTx)
Check rx fifo empty or not.
Definition: pan_uart.h:540
__STATIC_INLINE UART_LineStatusDef UART_GetLineStatus(UART_T *UARTx)
This function is used to get line status.
Definition: pan_uart.h:225
__STATIC_INLINE void UART_EnableStickParity(UART_T *UARTx)
This function is used to enable stick parity.
Definition: pan_uart.h:203
bool UART_SendMultipleData(UART_T *UARTx, uint8_t *Buf, size_t BufSize, size_t SendSize)
This function is used to send multiple data.
__STATIC_INLINE void UART_SetReceiveAddress(UART_T *UARTx, uint32_t addr)
This is an address matching register during receive mode,Set Receive Address.
Definition: pan_uart.h:663
__STATIC_INLINE bool UART_IsTxFifoEmpty(UART_T *UARTx)
Check tx fifo empty or not.
Definition: pan_uart.h:518
__STATIC_INLINE void UART_Enable9BitData(UART_T *UARTx)
Enable 9-bit data transmit and receive transfer.
Definition: pan_uart.h:606
void UART_DeInit(UART_T *UARTx)
Deinit the UARTx peripheral.
__STATIC_INLINE void UART_Disable9BitData(UART_T *UARTx)
Disable 9-bit data transmit and receive transfer.
Definition: pan_uart.h:617
__STATIC_INLINE void UART_ResetRxFifo(UART_T *UARTx)
This function is used to reset rx fifo.
Definition: pan_uart.h:282
__STATIC_INLINE void UART_SetTransmitAddress(UART_T *UARTx, uint32_t addr)
This is an address matching register during transmit mode,Set transmit address.
Definition: pan_uart.h:674
__STATIC_INLINE void UART_EnableRts(UART_T *UARTx)
Enable request to send in flow control mode.
Definition: pan_uart.h:584
__STATIC_INLINE void UART_ResetTxFifo(UART_T *UARTx)
This function is used to reset tx fifo.
Definition: pan_uart.h:301
__STATIC_INLINE void UART_DisableAfc(UART_T *UARTx)
Disable auto flow control mode.
Definition: pan_uart.h:382
__STATIC_INLINE bool UART_IsPtimeEnabled(UART_T *UARTx)
Check programmable THRE interrupt mode enable or not.
Definition: pan_uart.h:449
__STATIC_INLINE UART_ModemStatusDef UART_GetModemStatus(UART_T *UARTx)
Definition: pan_uart.h:230
__STATIC_INLINE uint8_t UART_GetRxFifoLevel(UART_T *UARTx)
Get the receive fifo level.
Definition: pan_uart.h:360
__STATIC_INLINE UART_EventDef UART_GetActiveEvent(UART_T *UARTx)
Get active interrupt ID.
Definition: pan_uart.h:507
__STATIC_INLINE bool UART_IsIrqEnabled(UART_T *UARTx, UART_IrqDef irq)
Check interrupt enable or not.
Definition: pan_uart.h:485
__STATIC_INLINE bool UART_IsRxFifoFull(UART_T *UARTx)
Check rx fifo full or not.
Definition: pan_uart.h:551
bool UART_Init(UART_T *UARTx, UART_InitTypeDef *UART_InitStruct)
Initializes the UARTx peripheral according to the specified parameters in the UART_InitStruct .
__STATIC_INLINE void UART_DisablePtime(UART_T *UARTx)
Programmable THRE interrupt mode disable.
Definition: pan_uart.h:438
__STATIC_INLINE void UART_EnableIrq(UART_T *UARTx, UART_IrqDef irq)
Enable interrupt,include Modem status linestatus,THRE,receive data.
Definition: pan_uart.h:461
__STATIC_INLINE bool UART_IsFifoEnabled(UART_T *UARTx)
Adjust whether the FIFOs enabled or not.
Definition: pan_uart.h:271
__STATIC_INLINE void UART_SetTxTrigger(UART_T *UARTx, UART_TxTriggerDef level)
Set the trigger level of the transmitter FIFO empty interrupt.
Definition: pan_uart.h:317
__STATIC_INLINE void UART_EnableAddrMatchMode(UART_T *UARTx)
Enable address match feature during receive.
Definition: pan_uart.h:628
__STATIC_INLINE void UART_EnableHaltTX(UART_T *UARTx)
Enable halt transmit,this function is used to halt transmit for test.
Definition: pan_uart.h:562
__STATIC_INLINE void UART_DisableRts(UART_T *UARTx)
Disable request to send in flow control mode.
Definition: pan_uart.h:595
__STATIC_INLINE bool UART_IsTxFifoFull(UART_T *UARTx)
Check tx fifo full or not.
Definition: pan_uart.h:529
__STATIC_INLINE uint8_t UART_GetIrqMasked(UART_T *UARTx)
Get which interrupt is actived.
Definition: pan_uart.h:496
__STATIC_INLINE void UART_DisableAddrMatchMode(UART_T *UARTx)
Disable address match feature during receive.
Definition: pan_uart.h:639
bool UART_ReceiveMultipleData(UART_T *UARTx, uint8_t *Buf, size_t BufSize, size_t ExpectSize)
This function is used to receive multiple data.
__STATIC_INLINE void UART_DisableFifo(UART_T *UARTx)
This function disables receive and transmit FIFOs.
Definition: pan_uart.h:257
__STATIC_INLINE uint8_t UART_ReceiveData(UART_T *UARTx)
Uart receive data function.
Definition: pan_uart.h:416
__STATIC_INLINE bool UART_IsAfcEnabled(UART_T *UARTx)
Check auto flow control mode enable or not.
Definition: pan_uart.h:393
__STATIC_INLINE void UART_DisableStickParity(UART_T *UARTx)
This function is used to disable stick parity.
Definition: pan_uart.h:214
__STATIC_INLINE void UART_SendAddr(UART_T *UARTx)
UART Send Address,9-bit character will be transmitted with 9-th bit set to 1 and the remaining 8-bits...
Definition: pan_uart.h:651
__STATIC_INLINE void UART_DisableHaltTX(UART_T *UARTx)
Disable halt transmit.
Definition: pan_uart.h:573
__STATIC_INLINE void UART_SendData(UART_T *UARTx, uint8_t Data)
Uart send data function.
Definition: pan_uart.h:405
__STATIC_INLINE void UART_EnablePtime(UART_T *UARTx)
Programmable THRE interrupt mode enable.
Definition: pan_uart.h:427
__STATIC_INLINE void UART_EnableAfc(UART_T *UARTx)
Enable auto flow control mode.
Definition: pan_uart.h:371
__STATIC_INLINE void UART_EnableFifo(UART_T *UARTx)
This function enables receive and transmit FIFOs, if they are available.
Definition: pan_uart.h:242
__STATIC_INLINE void UART_SetRxTrigger(UART_T *UARTx, UART_RxTriggerDef level)
Sets the trigger level of the receiver FIFO full interrupt.
Definition: pan_uart.h:334
Structure with uart init feature.
Definition: pan_uart.h:189
UART_LineCtrlDef UART_LineCtrl
Definition: pan_uart.h:191
uint32_t UART_BaudRate
Definition: pan_uart.h:190