UART
-
- How to Use
- Obtaining a UART Device Handle
- Setting the UART Baud Rate
- Obtaining the UART Baud Rate
- Setting the UART Device Attributes
- Obtaining UART Device Attributes
- Setting the UART Transmission Mode
- Writing Data of a Specified Length into a UART Device
- Reading Data of a Specified Length from a UART Device
- Destroying the UART Device Handle
Overview
-
The Universal Asynchronous Receiver/Transmitter (UART) is a universal serial data bus used for asynchronous communication. It enables bi-directional communication between devices in full-duplex mode.
-
UART is widely used to print information for debugging or to connect to various external modules such as GPS and Bluetooth.
-
A UART is connected to other modules through two wires (as shown in Figure 1) or four wires (as shown in Figure 2).
-
TX: TX pin of the transmitting UART. It is connected to the RX pin of the peer UART.
-
RX: RX pin of the receiving UART. It is connected to the TX pin of the peer UART.
-
RTS: Request to Send signal pin. It is connected to the CTS pin of the peer UART and is used to indicate whether the local UART is ready to receive data.
-
CTS: Clear to Send signal pin. It is connected to the RTS pin of the peer UART and is used to indicate whether the local UART is allowed to send data to the peer end.
-
-
The transmitting and receiving UARTs must ensure that they have the same settings on particular attributes such as the baud rate and data format (start bit, data bit, parity bit, and stop bit) before they start to communicate. During data transmission, a UART sends data to the peer end over the TX pin and receives data from the peer end over the RX pin. When the size of the buffer used by a UART for storing received data reaches the preset threshold, the RTS signal of the UART changes to 1 (data cannot be received), and the peer UART stops sending data to it because its CTS signal does not allow it to send data.
-
The UART interface defines a set of common functions for operating a UART port, including obtaining and releasing device handles, reading and writing data of a specified length, and obtaining and setting the baud rate, as well as the device attributes.
Available APIs
Table 1 APIs for the UART driver
NOTE: All functions provided in this document can be called only in kernel space.
Usage Guidelines
How to Use
Figure 3 shows the process of using a UART device.
Figure 3 Process of using a UART device
Obtaining a UART Device Handle
Before performing UART communication, call UartOpen to obtain a UART device handle. This function returns the pointer to the UART device handle with a specified port number.
DevHandle UartOpen(uint32_t port);
Table 2 Description of UartOpen
The following example shows how to obtain a UART device handle based on the assumption that the UART port number is 3:
DevHandle handle = NULL; /* The UART device handle */
uint32_t port = 3; /* UART port number */
handle = UartOpen(port);
if (handle == NULL) {
HDF_LOGE("UartOpen: failed!\n");
return;
}
Setting the UART Baud Rate
After obtaining the UART device handle, set the UART baud rate by calling the following function:
int32_t UartSetBaud(DevHandle handle, uint32_t baudRate);
Table 3 Description of UartSetBaud
The following example shows how to set the UART baud rate to 9600:
int32_t ret;
/* Set the UART baud rate to 9600. */
ret = UartSetBaud(handle, 9600);
if (ret != 0) {
HDF_LOGE("UartSetBaud: failed, ret %d\n", ret);
}
Obtaining the UART Baud Rate
After setting the UART baud rate, obtain the current baud rate by calling the following function:
int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate);
Table 4 Description of UartGetBaud
The following example shows how to obtain the UART baud rate:
int32_t ret;
uint32_t baudRate;
/* Obtain the UART baud rate. */
ret = UartGetBaud(handle, &baudRate);
if (ret != 0) {
HDF_LOGE("UartGetBaud: failed, ret %d\n", ret);
}
Setting the UART Device Attributes
Before performing UART communication, set the UART device attributes by calling the following function:
int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute);
Table 5 Description of UartSetAttribute
The following example shows how to set the UART device attributes:
int32_t ret;
struct UartAttribute attribute;
attribute.dataBits = UART_ATTR_DATABIT_7; /* Set the number of data bits to 7. */
attribute.parity = UART_ATTR_PARITY_NONE; /* Set the parity bit to no parity. */
attribute.stopBits = UART_ATTR_STOPBIT_1; /* Set the stop bit to 1. */
attribute.rts = UART_ATTR_RTS_DIS; /* Disable the RTS signal. */
attribute.cts = UART_ATTR_CTS_DIS; /* Disable the CTS signal. */
attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN; /* Enable RX FIFO. */
attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN; /* Enable TX FIFO. */
/* Set the UART device attributes. */
ret = UartSetAttribute(handle, &attribute);
if (ret != 0) {
HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
}
Obtaining UART Device Attributes
After setting the UART device attributes, obtain the current device attributes by calling the following function:
int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute);
Table 6 Description of UartGetAttribute
The following example shows how to obtain the UART device attributes:
int32_t ret;
struct UartAttribute attribute;
/* Obtain the UART attributes. */
ret = UartGetAttribute(handle, &attribute);
if (ret != 0) {
HDF_LOGE("UartGetAttribute: failed, ret %d\n", ret);
}
Setting the UART Transmission Mode
Before performing UART communication, set the UART transmission mode by calling the following function:
int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode);
Table 7 Description of UartSetTransMode
The following example shows how to set the transmission mode to UART_MODE_RD_BLOCK:
int32_t ret;
/* Set the UART transmission mode. */
ret = UartSetTransMode(handle, UART_MODE_RD_BLOCK);
if (ret != 0) {
HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret);
}
Writing Data of a Specified Length into a UART Device
To write data into a UART device, call the following function:
int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size);
Table 8 Description of UartWrite
The following example shows how to write data of a specified length into the UART device:
int32_t ret;
uint8_t wbuff[5] = {1, 2, 3, 4, 5};
/* Write 5-byte data into the UART device. */
ret = UartWrite(handle, wbuff, 5);
if (ret != 0) {
HDF_LOGE("UartWrite: failed, ret %d\n", ret);
}
Reading Data of a Specified Length from a UART Device
To write data into a UART device, call the following function:
int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size);
Table 9 Description of UartRead
The following example shows how to read data of a specified length from the UART device:
int32_t ret;
uint8_t rbuff[5] = {0};
/* Read 5-byte data from the UART device. */
ret = UartRead(handle, rbuff, 5);
if (ret < 0) {
HDF_LOGE("UartRead: failed, ret %d\n", ret);
}
CAUTION: Data is successfully read from the UART device if a non-negative value is returned. If the return value is 0, no valid data can be read from the UART device. If the return value is greater than 0, the return value is the length of the data actually read from the UART device. The length is less than or equal to the value of size and does not exceed the maximum length of data to read at a time specified by the UART controller in use.
Destroying the UART Device Handle
After the UART communication, destroy the UART device handle by calling the following function:
void UartClose(DevHandle handle);
This function will release the resources previously obtained.
Table 10 Description of UartClose
The following example shows how to destroy the UART device handle:
UartClose(handle); /* Destroy the UART device handle. */
Usage Example
The following is a usage example of a UART device, including how to obtain the UART device handle, set the baud rate, device attributes, and transmission mode, read data from or write data into the UART device, and then destroy the UART device handle.
#include "hdf_log.h"
#include "uart_if.h"
void UartTestSample(void)
{
int32_t ret;
uint32_t port;
DevHandle handle = NULL;
uint8_t wbuff[5] = { 1, 2, 3, 4, 5 };
uint8_t rbuff[5] = { 0 };
struct UartAttribute attribute;
attribute.dataBits = UART_ATTR_DATABIT_7; /* Set the number of data bits to 7. */
attribute.parity = UART_ATTR_PARITY_NONE; /* Set the parity bit to no parity. */
attribute.stopBits = UART_ATTR_STOPBIT_1; /* Set the stop bit to 1. */
attribute.rts = UART_ATTR_RTS_DIS; /* Disable the RTS signal. */
attribute.cts = UART_ATTR_CTS_DIS; /* Disable the CTS signal. */
attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN; /* Enable RX FIFO. */
attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN; /* Enable TX FIFO. */
/* Set the UART port number actually used. */
port = 1;
/* Obtain the UART device handle. */
handle = UartOpen(port);
if (handle == NULL) {
HDF_LOGE("UartOpen: failed!\n");
return;
}
/* Set the UART baud rate to 9600. */
ret = UartSetBaud(handle, 9600);
if (ret != 0) {
HDF_LOGE("UartSetBaud: failed, ret %d\n", ret);
goto _ERR;
}
/* Set the UART device attributes. */
ret = UartSetAttribute(handle, &attribute);
if (ret != 0) {
HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
goto _ERR;
}
/* Set the UART transmission mode to non-blocking mode. */
ret = UartSetTransMode(handle, UART_MODE_RD_NONBLOCK);
if (ret != 0) {
HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret);
goto _ERR;
}
/* Write 5-byte data into the UART device. */
ret = UartWrite(handle, wbuff, 5);
if (ret != 0) {
HDF_LOGE("UartWrite: failed, ret %d\n", ret);
goto _ERR;
}
/* Read 5-byte data from the UART device. */
ret = UartRead(handle, rbuff, 5);
if (ret < 0) {
HDF_LOGE("UartRead: failed, ret %d\n", ret);
goto _ERR;
}
_ERR:
/* Destroy the UART device handle. */
UartClose(handle);
}