UART (串口通信)
简介
ET6001 提供 2 组 UART 接口,定义为 UART0~1。支持 RS485 以及 2 线普通串口以及带流控的串口模式。
功能概述
支持最高波特率大于等于 1 MBaud
支持 9 位串行数据
支持起始位检测
支持中断上报
支持 FIFO TEST 模式,TX、RX FIFO 各16个
支持硬件DMA握手请求
支持配置为软件可编程RS485模式
兼容 ISO 16750 标准的自动流控制模式
软件配置流程
基本收发配置流程如下:
配置IOMUX
配置对应串口引脚为UART模式
UART软件复位
调用
UART_performSoftwareReset()传入UARTx配置串口
波特率
数据位
停止位
奇偶校验
使能FIFO (可选)
调用
UART_enableFIFO()使能FIFO使能中断 (可选)
调用
UART_enableInterrupt()传入对应中断MaskUART_IT_RECV_DATA_AVAILABLE_FLAG
UART_IT_TRANS_DATA_EMPTY_FLAG
UART_IT_LINE_STATUS_FLAG
UART_IT_MODEM_STATUS_FLAG
UART_IT_PROMGRAM_THRE_FLAG
发送字符或字符数组 调用
UART_writeCharArray()或UART_writeCharBlockingFIFO()传入相关参数接收字符或字符数组 调用
UART_readCharArray()或UART_readCharBlockingFIFO()传入相关参数
中断配置说明
UART的中断使能、中断清除、中断禁止传入的参数均为中断掩码值,具体中断掩码值定义如下:
#define UART_IT_NONE (0x00U)
#define UART_IT_RECV_DATA_AVAILABLE_FLAG (0x01U)
#define UART_IT_TRANS_DATA_EMPTY_FLAG (0x02U)
#define UART_IT_LINE_STATUS_FLAG (0x04U)
#define UART_IT_MODEM_STATUS_FLAG (0x08U)
#define UART_IT_PROMGRAM_THRE_FLAG (0x80U)
UART的中断状态值与上述中断掩码值 不同 ,具体中断状态值定义如下:
#define UART_IT_STA_MODEM_STATUS (0x00U) //!< Modem status
#define UART_IT_STA_NO_INTERRUPT (0x01U) //!< No interrupt pending
#define UART_IT_STA_THR (0x02U) //!< Transmit holding register empty
#define UART_IT_STA_RECV_AVI (0x04U) //!< Receive data available
#define UART_IT_STA_RECV_LINE (0x06U) //!< Receive line status
#define UART_IT_STA_BUSY_DETECT (0x07U) //!< Busy detect
#define UART_IT_STA_CHAR_TIMEOUT (0x0CU) //!< Character timeout
编写程序时,需要注意中断掩码值和中断状态值的区别。
UART_IT_STA_THR 和 UART_IT_STA_RECV_AVI 的状态清除无需调用 UART_clearInterruptStatus() 函数。
当读取IIR寄存器或者向 THR 寄存器写入数据时,UART_IT_STA_THR 和 UART_IT_STA_RECV_AVI 的状态会自动清除。
小心
UART_IT_TRANS_DATA_EMPTY_FLAG 中断和其他中断不同,只有在发送缓存寄存器或者发送FIFO中没有数据时才会触发。 也就是说,发送空中断为脉冲中断。所以需要先手动发送数据才能触发该中断。 另外,如果有其他更高优先级的中断到来,则该中断可能不会被触发。
Q&A
UART_IT_STA_CHAR_TIMEOUT 中断在什么情况下会触发?
当接收FIFO中有数据,但在一定时间内没有新的数据到来时,会触发 UART_IT_STA_CHAR_TIMEOUT 中断。 该中断可以用来检测接收数据的结束或者接收数据的异常情况。
该中断生效的前提: UART_IT_RECV_DATA_AVAILABLE_FLAG 中断使能,并且使能了 FIFO 模式。
该中断产生的条件: 在最后 4 个字符时间内,接收FIFO 中没有数据接收或读取,并且在此期间至少有 1 个字符在FIFO中。
应用示例
UART数据收发示例:UART/UART_init_TxRx。
UART中断发送接收示例:UART/UART_IT_receive。
API Reference
Header File
Functions
-
void UART_setParityMode(UART_Type *UARTx, UART_ParityType parity)
Set the parity mode of the UART port.
- 参数:
UARTx – The base address of the UART port.
parity – The parity mode to be set.
-
UART_ParityType UART_getParityMode(UART_Type *UARTx)
Get the parity mode of the UART port.
- 参数:
UARTx – The base address of the UART port.
- 返回:
The parity mode of the UART port.
-
void UART_setFIFOInterruptLevel(UART_Type *UARTx, UART_TxFIFOThre txLevel, UART_RxFIFOThre rxLevel)
Set the FIFO interrupt level of the UART port.
- 参数:
UARTx – The base address of the UART port.
txLevel – The transmit FIFO interrupt level.
rxLevel – The receive FIFO interrupt level.
-
void UART_getFIFOInterruptLevel(UART_Type *UARTx, UART_TxFIFOThre *txLevel, UART_RxFIFOThre *rxLevel)
Get the FIFO interrupt level of the UART port.
- 参数:
UARTx – The base address of the UART port.
txLevel – The transmit FIFO interrupt level.
rxLevel – The receive FIFO interrupt level.
-
void UART_getConfig(UART_Type *UARTx, uint32_t *lspclkHz, uint32_t *baud, uint32_t *config)
Get the configuration information of the UART port.
- 参数:
UARTx – The base address of the UART port.
lspclkHz – The frequency of the UART clock.
baud – The baud rate of the UART port.
config – The configuration information of the UART port.
-
void UART_enableFIFO(UART_Type *UARTx)
Enable the FIFO of the UART port.
- 参数:
UARTx – The base address of the UART port.
-
void UART_disableFIFO(UART_Type *UARTx)
Disable the FIFO of the UART port.
- 参数:
UARTx – The base address of the UART port.
-
bool UART_isFIFOEnabled(UART_Type *UARTx)
Check if the FIFO is enabled for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
true if FIFO is enabled, false otherwise.
-
void UART_resetRxFIFO(UART_Type *UARTx)
Reset the receive FIFO for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
-
void UART_resetTxFIFO(UART_Type *UARTx)
Reset the transmit FIFO for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
-
void UART_resetChannels(UART_Type *UARTx)
Reset both the receive and transmit FIFOs for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
-
bool UART_isDataAvailableNonFIFO(UART_Type *UARTx)
Check if data is available in the receive buffer for non-FIFO mode.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
true if data is available, false otherwise.
-
bool UART_isSpaceAvailableNonFIFO(UART_Type *UARTx)
Check if space is available in the transmit buffer for non-FIFO mode.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
true if space is available, false otherwise.
-
UART_TxFIFOLevel UART_getTxFIFOStatus(UART_Type *UARTx)
Get the status of the transmit FIFO for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
The transmit FIFO level.
-
UART_RxFIFOLevel UART_getRxFIFOStatus(UART_Type *UARTx)
Get the status of the receive FIFO for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
The receive FIFO level.
-
bool UART_isTransmitterBusy(UART_Type *UARTx)
Check if the transmitter is busy for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
true if the transmitter is busy, false otherwise.
-
void UART_writeCharBlockingFIFO(UART_Type *UARTx, uint8_t data)
Write a character to the transmit buffer in blocking mode with FIFO enabled.
- 参数:
UARTx – Pointer to the UART peripheral instance.
data – The character to be written.
-
void UART_writeCharBlockingNonFIFO(UART_Type *UARTx, uint8_t data)
Write a character to the transmit buffer in blocking mode without FIFO.
- 参数:
UARTx – Pointer to the UART peripheral instance.
data – The character to be written.
-
void UART_writeCharNonBlocking(UART_Type *UARTx, uint8_t data)
Write a character to the transmit buffer in non-blocking mode.
- 参数:
UARTx – Pointer to the UART peripheral instance.
data – The character to be written.
-
uint16_t UART_getRxStatus(UART_Type *UARTx)
Get the receive status for the given UART peripheral.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
The receive status.
-
uint16_t UART_getModemStatus(UART_Type *UARTx)
Get the modem status of the UART.
This function retrieves the modem status of the specified UART peripheral.
- 参数:
UARTx – Pointer to the UART_Type structure, which contains the registers of the UART module.
- 返回:
The modem status as a 16-bit value, which includes various flags indicating the state of the modem.
-
uint8_t UART_readCharBlockingFIFO(UART_Type *UARTx)
Read a character from the receive buffer in blocking mode with FIFO enabled.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
The character read from the buffer.
-
uint8_t UART_readCharBlockingNonFIFO(UART_Type *UARTx)
Read a character from the receive buffer in blocking mode without FIFO.
- 参数:
UARTx – Pointer to the UART peripheral instance.
- 返回:
The character read from the buffer.
-
uint8_t UART_readCharNonBlocking(UART_Type *UARTx)
Reads a character from the UART receive buffer in non-blocking mode.
- 参数:
UARTx – The UART peripheral instance.
- 返回:
The character read from the receive buffer.
-
void UART_performSoftwareReset(UART_Type *UARTx)
Performs a software reset of the UART peripheral.
- 参数:
UARTx – The UART peripheral instance.
-
void UART_enableLoopback(UART_Type *UARTx)
Enables the loopback mode of the UART peripheral.
- 参数:
UARTx – The UART peripheral instance.
-
void UART_disableLoopback(UART_Type *UARTx)
Disables the loopback mode of the UART peripheral.
- 参数:
UARTx – The UART peripheral instance.
-
void UART_setConfig(UART_Type *UARTx, uint32_t baud, uint32_t config)
Set the UART configuration.
This function configures the UART with the specified baud rate and other settings.
- 参数:
UARTx – Pointer to the UART peripheral instance.
baud – The desired baud rate.
config – The configuration bits for the UART line control register.
-
void UART_writeCharArray(UART_Type *UARTx, const uint8_t *const array, uint16_t length)
Write an array of characters to the UART.
This function writes a specified number of characters from an array to the UART.
- 参数:
UARTx – Pointer to the UART peripheral instance.
array – Pointer to the array of characters to be written.
length – The number of characters to write.
-
void UART_readCharArray(UART_Type *UARTx, uint8_t *const array, uint16_t length)
Read an array of characters from the UART.
This function reads a specified number of characters from the UART into an array.
- 参数:
UARTx – Pointer to the UART peripheral instance.
array – Pointer to the array where the received characters will be stored.
length – The number of characters to read.
-
void UART_enableInterrupt(UART_Type *UARTx, uint32_t intFlags)
Enable UART interrupts.
This function enables the specified UART interrupts.
- 参数:
UARTx – Pointer to the UART peripheral instance.
intFlags – The interrupt flags to be enabled, ref to UART_IT_xxxx
-
void UART_disableInterrupt(UART_Type *UARTx, uint32_t intFlags)
Disable UART interrupts.
This function disables the specified UART interrupts.
- 参数:
UARTx – Pointer to the UART peripheral instance.
intFlags – The interrupt flags to be disabled, ref to UART_IT_xxxx
-
uint32_t UART_getInterruptStatus(UART_Type *UARTx)
Get the interrupt status of the UART.
- 参数:
UARTx – Pointer to the UART_Type structure
- 返回:
Interrupt status ref to !! UART_IT_STA_xxxx
-
void UART_clearInterruptStatus(UART_Type *UARTx, uint32_t intFlags)
Clears the interrupt status of the UART.
This function clears the interrupt status of the UART based on the provided interrupt flags.
- 参数:
UARTx – Pointer to the UART_Type structure, which contains the registers of the UART module.
intFlags – Interrupt flags to be cleared. Can be a combination of the following:
UART_IT_TRANS_DATA_EMPTY_FLAG: Clear the transmit data empty interrupt flag.
UART_IT_MODEM_STATUS_FLAG: Clear the modem status interrupt flag.
UART_IT_LINE_STATUS_FLAG: Clear the line status interrupt flag.
-
void UART_setBaud(UART_Type *UARTx, uint32_t baud)
Sets the baud rate of the UART.
This function sets the baud rate of the UART based on the provided clock frequency and baud rate.
- 参数:
UARTx – Pointer to the UART_Type structure, which contains the registers of the UART module.
baud – The desired baud rate.
Macros
-
UART_IT_NONE
interrupt flags
-
UART_IT_RECV_DATA_AVAILABLE_FLAG
-
UART_IT_TRANS_DATA_EMPTY_FLAG
-
UART_IT_LINE_STATUS_FLAG
-
UART_IT_MODEM_STATUS_FLAG
-
UART_IT_PROMGRAM_THRE_FLAG
-
UART_IT_STA_MODEM_STATUS
Interrupt status flags.
Modem status
-
UART_IT_STA_NO_INTERRUPT
No interrupt pending.
-
UART_IT_STA_THR
Transmit holding register empty.
-
UART_IT_STA_RECV_AVI
Receive data available.
-
UART_IT_STA_RECV_LINE
Receive line status.
-
UART_IT_STA_BUSY_DETECT
Busy detect.
-
UART_IT_STA_CHAR_TIMEOUT
Character timeout.
-
UART_CONFIG_WLEN_8
uart config word length
8 bit data
-
UART_CONFIG_WLEN_7
7 bit data
-
UART_CONFIG_WLEN_6
6 bit data
-
UART_CONFIG_WLEN_5
5 bit data
-
UART_CONFIG_STOP_ONE
UART config stop bits.
One stop bit
-
UART_CONFIG_STOP_TWO
Two stop bits.
-
UART_CONFIG_PAR_MASK
Parity Mask.
-
UART_RXSTATUS_PARITY
UART RX Status.
Parity error
-
UART_RXSTATUS_OVERRUN
Overrun error.
-
UART_RXSTATUS_FRAMING
Framing error.
-
UART_RXSTATUS_BREAK
Break detect.
-
UART_RXSTATUS_READY
Receiver ready.
-
UART_RXSTATUS_ERROR
Receiver error.
-
UART_MODEM_STA_DCTS
UART Modem Status.
Delta CTS
-
UART_MODEM_STA_DDSR
Delta DSR.
-
UART_MODEM_STA_TERI
Trailing Edge Ring Indicator.
-
UART_MODEM_STA_DDCD
Delta DCD.
-
UART_MODEM_STA_CTS
Clear To Send.
-
UART_MODEM_STA_DSR
Data Set Ready.
-
UART_MODEM_STA_RI
Ring Indicator.
-
UART_MODEM_STA_DCD
Data Carrier Detect.
Enumerations
-
enum UART_ParityType
UART config parity.
Values:
-
enumerator UART_CONFIG_PAR_NONE
No parity.
-
enumerator UART_CONFIG_PAR_EVEN
Even parity.
-
enumerator UART_CONFIG_PAR_ODD
Odd parity.
-
enumerator UART_CONFIG_PAR_NONE
-
enum UART_TxFIFOThre
UART config TxFIFO Thre.
Values:
-
enumerator UART_TX_FIFO_EMPTY
-
enumerator UART_TX_FIFO_CHAR_2
-
enumerator UART_TX_FIFO_QUARTER_FULL
-
enumerator UART_TX_FIFO_HALF_FULL
-
enumerator UART_TX_FIFO_EMPTY
-
enum UART_RxFIFOThre
UART config RxFIFO Thre.
Values:
-
enumerator UART_RX_FIFO_CHAR_1
-
enumerator UART_RX_FIFO_QUARTER_FULL
-
enumerator UART_RX_FIFO_HALF_FULL
-
enumerator UART_RX_FIFO_FULL_2
-
enumerator UART_RX_FIFO_CHAR_1
-
enum UART_TxFIFOLevel
UART TX FIFO Level.
Values:
-
enumerator UART_FIFO_TX0
Transmit interrupt empty.
-
enumerator UART_FIFO_TX1
Transmit interrupt 1/16 full.
-
enumerator UART_FIFO_TX2
Transmit interrupt 2/16 full.
-
enumerator UART_FIFO_TX3
Transmit interrupt 3/16 full.
-
enumerator UART_FIFO_TX4
Transmit interrupt 4/16 full.
-
enumerator UART_FIFO_TX5
Transmit interrupt 5/16 full.
-
enumerator UART_FIFO_TX6
Transmit interrupt 6/16 full.
-
enumerator UART_FIFO_TX7
Transmit interrupt 7/16 full.
-
enumerator UART_FIFO_TX8
Transmit interrupt 8/16 full.
-
enumerator UART_FIFO_TX9
Transmit interrupt 9/16 full.
-
enumerator UART_FIFO_TX10
Transmit interrupt 10/16 full.
-
enumerator UART_FIFO_TX11
Transmit interrupt 11/16 full.
-
enumerator UART_FIFO_TX12
Transmit interrupt 12/16 full.
-
enumerator UART_FIFO_TX13
Transmit interrupt 13/16 full.
-
enumerator UART_FIFO_TX14
Transmit interrupt 14/16 full.
-
enumerator UART_FIFO_TX15
Transmit interrupt 15/16 full.
-
enumerator UART_FIFO_TX16
Transmit interrupt full.
-
enumerator UART_FIFO_TX0
-
enum UART_RxFIFOLevel
UART RX FIFO Level.
Values:
-
enumerator UART_FIFO_RX0
Receive interrupt empty.
-
enumerator UART_FIFO_RX1
Receive interrupt 1/16 full.
-
enumerator UART_FIFO_RX2
Receive interrupt 2/16 full.
-
enumerator UART_FIFO_RX3
Receive interrupt 3/16 full.
-
enumerator UART_FIFO_RX4
Receive interrupt 4/16 full.
-
enumerator UART_FIFO_RX5
Receive interrupt 5/16 full.
-
enumerator UART_FIFO_RX6
Receive interrupt 6/16 full.
-
enumerator UART_FIFO_RX7
Receive interrupt 7/16 full.
-
enumerator UART_FIFO_RX8
Receive interrupt 8/16 full.
-
enumerator UART_FIFO_RX9
Receive interrupt 9/16 full.
-
enumerator UART_FIFO_RX10
Receive interrupt 10/16 full.
-
enumerator UART_FIFO_RX11
Receive interrupt 11/16 full.
-
enumerator UART_FIFO_RX12
Receive interrupt 12/16 full.
-
enumerator UART_FIFO_RX13
Receive interrupt 13/16 full.
-
enumerator UART_FIFO_RX14
Receive interrupt 14/16 full.
-
enumerator UART_FIFO_RX15
Receive interrupt 15/16 full.
-
enumerator UART_FIFO_RX16
Receive interrupt full.
-
enumerator UART_FIFO_RX0