• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# UART
2
3
4## Overview
5
6The 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.
7
8UART is widely used to print information for debugging or to connect to various external modules such as GPS and Bluetooth.
9
10A UART is connected to other modules through two wires (as shown in Figure 1) or four wires (as shown in Figure 2).
11  - TX: TX pin of the transmitting UART. It is connected to the RX pin of the peer UART.
12  - RX: RX pin of the receiving UART. It is connected to the TX pin of the peer UART.
13  - 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.
14  - 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.
15
16       **Figure 1** Two-wire UART communication
17
18       ![](figures/2-wire-uart-communication.png "2-wire-uart-communication")
19
20       **Figure 2** Four-wire UART communication
21
22       ![](figures/4-wire-uart-communication.png "4-wire-uart-communication")
23
24- 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.
25
26- 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.
27
28
29## Available APIs
30
31  **Table 1** UART driver APIs
32
33| API| Description|
34| -------- | -------- |
35| UartOpen | Obtains a UART device handle.|
36| UartClose | Releases a UART device handle.|
37| UartRead | Reads data of the specified length from a UART device.|
38| UartWrite | Writes data of the specified length to a UART device.|
39| UartGetBaud | Obtains the UART baud rate.|
40| UartSetBaud | Sets the UART baud rate.|
41| UartGetAttribute | Obtains UART device attributes.|
42| UartSetAttribute | Sets UART device attributes.|
43| UartSetTransMode | Sets the UART transmission mode.|
44
45> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
46> All APIs described in this document can be called only in kernel mode.
47
48
49## Usage Guidelines
50
51
52### How to Use
53
54The figure below illustrates how to use the APIs.
55
56  **Figure 3** Using UART driver APIs
57
58  ![](figures/using-UART-process.png)
59
60
61### Opening a UART Device Handle
62
63Before performing UART communication, call **UartOpen** to obtain a UART device handle. This function returns the pointer to the UART device handle with the specified port number.
64
65
66```
67DevHandle UartOpen(uint32_t port);
68```
69
70  **Table 2** Description of UartOpen
71
72| Parameter| Description|
73| -------- | -------- |
74| port | UART port number.|
75| **Return Value**| **Description**|
76| NULL | The operation failed.|
77| Device handle| The operation is successful. The obtained UART device handle is returned.|
78
79  Example: Obtain the device handle of UART port 3.
80
81```
82DevHandle handle = NULL;    /* UART device handle */
83uint32_t port = 3;          /* UART port number */
84handle = UartOpen(port);
85if (handle == NULL) {
86    HDF_LOGE("UartOpen: failed!\n");
87    return;
88}
89```
90
91
92### Setting the UART Baud Rate
93
94Call **UartSetBaud()** to set the UART baud rate.
95
96
97```
98int32_t UartSetBaud(DevHandle handle, uint32_t baudRate);
99```
100
101  **Table 3** Description of UartSetBaud
102
103| Parameter| Description|
104| -------- | -------- |
105| handle | UART device handle.|
106| baudRate | Baud rate to set.|
107| **Return Value**| **Description**|
108| 0 | The operation is successful.|
109| Negative value| The operation failed.|
110
111Example: Set the UART baud rate to **9600**.
112
113
114```
115int32_t ret;
116/* Set the UART baud rate to 9600. */
117ret = UartSetBaud(handle, 9600);
118if (ret != 0) {
119    HDF_LOGE("UartSetBaud: failed, ret %d\n", ret);
120}
121```
122
123
124### Obtaining the UART Baud Rate
125
126Call **UartGetBaud()** to obtain the UART baud rate.
127
128
129```
130int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate);
131```
132
133  **Table 4** Description of UartGetBaud
134
135| Parameter| Description|
136| -------- | -------- |
137| handle | UART device handle.|
138| baudRate | Pointer to the UART baud rate obtained.|
139| **Return Value**| **Description**|
140| 0 | The operation is successful.|
141| Negative value| The operation failed.|
142
143Example: Obtain the UART baud rate.
144
145
146```
147int32_t ret;
148uint32_t baudRate;
149/* Obtain the UART baud rate. */
150ret = UartGetBaud(handle, &baudRate);
151if (ret != 0) {
152    HDF_LOGE("UartGetBaud: failed, ret %d\n", ret);
153}
154```
155
156
157### Setting UART Device Attributes
158
159Call **UartSetAttribute()** to set UART device attributes.
160
161
162```
163int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute);
164```
165
166  **Table 5** Description of UartSetAttribute
167
168| Parameter| Description|
169| -------- | -------- |
170| handle | UART device handle.|
171| attribute | Pointer to the UART device attributes to set.|
172| **Return Value**| **Description**|
173| 0 | The operation is successful.|
174| Negative value| The operation failed.|
175
176Example: Set UART device attributes.
177
178
179```
180int32_t ret;
181struct UartAttribute attribute;
182attribute.dataBits = UART_ATTR_DATABIT_7;   /* Enable 7 bits to be transferred each time. */
183attribute.parity = UART_ATTR_PARITY_NONE;   /* Disable parity check. */
184attribute.stopBits = UART_ATTR_STOPBIT_1;   /* Set the stop bit to 1. */
185attribute.rts = UART_ATTR_RTS_DIS;          /* Disable the RTS signal. */
186attribute.cts = UART_ATTR_CTS_DIS;          /* Disable the CTS signal. */
187attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;  /* Enable RX FIFO. */
188attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;  /* Enable TX FIFO. */
189/* Set UART device attributes. */
190ret = UartSetAttribute(handle, &attribute);
191if (ret != 0) {
192    HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
193}
194```
195
196
197### Obtaining UART Device Attributes
198
199Call **UartGetAttribute()** to obtain the current UART device attributes.
200
201
202```
203int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute);
204```
205
206  **Table 6** Description of UartGetAttribute
207
208| Parameter| Description|
209| -------- | -------- |
210| handle | UART device handle.|
211| attribute | Pointer to the UART device attributes obtained.|
212| **Return Value**| **Description**|
213| 0 | The operation is successful.|
214| Negative value| The operation failed.|
215
216Example: Obtain UART device attributes.
217
218
219```
220int32_t ret;
221struct UartAttribute attribute;
222/* Obtain UART device attributes. */
223ret = UartGetAttribute(handle, &attribute);
224if (ret != 0) {
225    HDF_LOGE("UartGetAttribute: failed, ret %d\n", ret);
226}
227```
228
229
230### Setting the UART Transmission Mode
231
232Call **UartSetTransMode()** to set the UART transmission mode.
233
234
235```
236int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode);
237```
238
239  **Table 7** Description of UartSetTransMode
240
241| Parameter| Description|
242| -------- | -------- |
243| handle | UART device handle.|
244| mode | UART transmission mode to set.|
245| **Return Value**| **Description**|
246| 0 | The operation is successful.|
247| Negative value| The operation failed.|
248
249Example: Set the UART transmission mode to **UART_MODE_RD_BLOCK**.
250
251
252```
253int32_t ret;
254/* Set the UART transmission mode. */
255ret = UartSetTransMode(handle, UART_MODE_RD_BLOCK);
256if (ret != 0) {
257    HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret);
258}
259```
260
261
262### Writing Data to a UART Device
263
264Call **UartWrite()** to write data of the specified length to a UART device.
265
266
267```
268int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size);
269```
270
271  **Table 8** Description of UartWrite
272
273| Parameter| Description|
274| -------- | -------- |
275| handle | UART device handle.|
276| data | Pointer to the data to write.|
277| size | Length of the data to write.|
278| **Return Value**| **Description**|
279| 0 | The operation is successful.|
280| Negative value| The operation failed.|
281
282Example: Write data to a UART device.
283
284
285```
286int32_t ret;
287uint8_t wbuff[5] = {1, 2, 3, 4, 5};
288/* Write 5-byte data to the UART device. */
289ret = UartWrite(handle, wbuff, 5);
290if (ret != 0) {
291    HDF_LOGE("UartWrite: failed, ret %d\n", ret);
292}
293```
294
295
296### Reading Data from a UART Device
297
298Call **UartRead()** to read data of the specified length from a UART device.
299
300
301```
302int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size);
303```
304
305  **Table 9** Description of UartRead
306
307| Parameter| Description|
308| -------- | -------- |
309| handle | UART device handle.|
310| data | Pointer to the buffer for receiving the data.|
311| size | Length of the data to read.|
312| **Return Value**| **Description**|
313| Non-negative value| The operation is successful. The length of the data read is returned.|
314| Negative value| The operation failed.|
315
316Example: Read data of the specified length from a UART device.
317
318
319```
320int32_t ret;
321uint8_t rbuff[5] = {0};
322/* Read 5-byte data from the UART device. */
323ret = UartRead(handle, rbuff, 5);
324if (ret < 0) {
325    HDF_LOGE("UartRead: failed, ret %d\n", ret);
326}
327```
328
329> ![icon-caution.gif](../public_sys-resources/icon-caution.gif) **CAUTION**<br/>
330> Data is successfully read from the UART device if a non-negative value is returned. If **0** is returned, no valid data can be read from the UART device. A value greater than **0** indicates the length of the data read from the UART device. The data length must be less than or equal to the value of **size** and cannot exceed the maximum length of the data to read at a time specified by the UART controller in use.
331
332
333### Closing a UART Device Handle
334
335Call **UartClose()** to close a UART device handle.
336
337
338```
339void UartClose(DevHandle handle);
340```
341
342This function releases the resources requested by **UartOpen**.
343
344  **Table 10** Description of UartClose
345
346| Parameter| Description|
347| -------- | -------- |
348| handle | UART device handle to close.|
349
350Example: Close a UART device handle.
351
352
353```
354UartClose(handle); /* Close the UART device handle. */
355```
356
357
358## Example
359
360  The following example shows how to open a UART device handle, set the baud rate, device attributes, and transmission mode, read data from or write data into the UART device, and then close the UART device handle.
361
362```
363#include "hdf_log.h"
364#include "uart_if.h"
365
366void UartTestSample(void)
367{
368    int32_t ret;
369    uint32_t port;
370    DevHandle handle = NULL;
371    uint8_t wbuff[5] = { 1, 2, 3, 4, 5 };
372    uint8_t rbuff[5] = { 0 };
373    struct UartAttribute attribute;
374    attribute.dataBits = UART_ATTR_DATABIT_7;   /* Enable 7 bits to be transferred each time. */
375    attribute.parity = UART_ATTR_PARITY_NONE;   /* Disable parity check. */
376    attribute.stopBits = UART_ATTR_STOPBIT_1;   /* Set the stop bit to 1. */
377    attribute.rts = UART_ATTR_RTS_DIS;          /* Disable the RTS signal. */
378    attribute.cts = UART_ATTR_CTS_DIS;          /* Disable the CTS signal. */
379    attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;  /* Enable RX FIFO. */
380    attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;  /* Enable TX FIFO. */
381    /* Enter the UART port number. */
382    port = 1;
383    /* Open the UART device handle based on the port number. */
384    handle = UartOpen(port);
385    if (handle == NULL) {
386        HDF_LOGE("UartOpen: failed!\n");
387        return;
388    }
389    /* Set the UART baud rate to 9600. */
390    ret = UartSetBaud(handle, 9600);
391    if (ret != 0) {
392        HDF_LOGE("UartSetBaud: failed, ret %d\n", ret);
393        goto _ERR;
394    }
395    /* Set UART device attributes. */
396    ret = UartSetAttribute(handle, &attribute);
397    if (ret != 0) {
398        HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
399        goto _ERR;
400    }
401    /* Set the UART transmission mode to non-blocking mode. */
402    ret = UartSetTransMode(handle, UART_MODE_RD_NONBLOCK);
403    if (ret != 0) {
404        HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret);
405        goto _ERR;
406    }
407    /* Write 5-byte data to the UART device. */
408    ret = UartWrite(handle, wbuff, 5);
409    if (ret != 0) {
410        HDF_LOGE("UartWrite: failed, ret %d\n", ret);
411        goto _ERR;
412    }
413    /* Read 5-byte data from the UART device. */
414    ret = UartRead(handle, rbuff, 5);
415    if (ret < 0) {
416        HDF_LOGE("UartRead: failed, ret %d\n", ret);
417        goto _ERR;
418    }
419_ERR:
420    /* Close the UART device handle. */
421    UartClose(handle);
422}
423```
424