driver-platform-uart-des.md 16.4 KB
Newer Older
A
Annie_wang 已提交
1
# UART
W
wenjun 已提交
2

A
Annie_wang 已提交
3 4
## Overview

A
Annie_wang 已提交
5
### Function
A
Annie_wang 已提交
6

A
Annie_wang 已提交
7
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.
A
Annie_wang 已提交
8 9 10

A UART is connected to other modules through two wires (as shown in Figure 1) or four wires (as shown in Figure 2).

A
Annie_wang 已提交
11 12 13 14
  - TX: UART transmitter. It is connected to the RX of the peer UART.
  - RX: UART receiver. It is connected to the TX of the peer UART.
  - RTS: Request to Send signal, indicating whether the local UART is ready to receive data. It is connected to the CTS of the peer UART.
  - CTS: Clear to Send signal, indicating whether the local UART is allowed to send data to the peer end. It is connected to the RTS of the peer UART.
A
Annie_wang 已提交
15

A
Annie_wang 已提交
16
**Figure 1** Two-wire UART communication
A
Annie_wang 已提交
17

A
Annie_wang 已提交
18
![image1](figures/2-wire-uart-communication.png "2-wire-uart-communication")
A
Annie_wang 已提交
19

A
Annie_wang 已提交
20
**Figure 2** Four-wire UART communication
A
Annie_wang 已提交
21

A
Annie_wang 已提交
22
 ![image2](figures/4-wire-uart-communication.png "4-wire-uart-communication")
A
Annie_wang 已提交
23

A
Annie_wang 已提交
24
The UART transmitter and receiver must have the same settings on particular attributes, such as the baud rate and data format (start bit, data bits, parity bit, and stop bit) before they start to communicate. A UART sends data to the peer end over the TX and receives data from the peer end over the RX. 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.
A
Annie_wang 已提交
25

A
Annie_wang 已提交
26
The UART module provides APIs for operating UART ports, including:
A
Annie_wang 已提交
27

A
Annie_wang 已提交
28 29 30 31
- Opening or closing a UART device
- Reading or writing data
- Setting or obtaining the baud rate of a UART device
- Setting or obtaining UART device attributes
A
Annie_wang 已提交
32

A
Annie_wang 已提交
33
### Basic Concepts
A
Annie_wang 已提交
34

A
Annie_wang 已提交
35 36 37 38 39 40 41 42 43 44 45
- Asynchronous communication

  In asynchronous communication, data is transmitted in frames of characters or bytes. Frames are sent and received one by one through the transmission line. The transmitter and receiver have their own clocks to control data sending and receiving. The two clock sources are independent and not synchronized with each other. 

  When data is sent one character at a time, the time interval between two characters is not fixed, but the time interval between two adjacent bits in a character frame is fixed.

- Full-duplex transmission

  A duplex communication mode allows data to be transmitted in both directions at the same time. A duplex communication channel is equivalent to two simplex communication channels operating in opposite directions at the same time. In full-duplex mode, signals can be transmitted bidirectionally at the same time.

### Working Principles
A
Annie_wang 已提交
46

A
Annie_wang 已提交
47
In the Hardware Driver Foundation (HDF), the UART uses the independent service mode (see Figure 3) for API adaptation. In this mode, each device independently publishes a service to process external access requests. When receiving an access request, the HDF DeviceManager extracts parameters from the request to call the internal APIs of the target device. In the independent service mode, the HDF DeviceManager provides service management capabilities. However, you need to configure a node for each device, which increases memory usage.
A
Annie_wang 已提交
48

A
Annie_wang 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
In the independent service mode, the core layer does not publish a service for the upper layer. Therefore, a service must be published for each controller. To achieve this purpose:

- You need to implement the **Bind()** function in **HdfDriverEntry** to bind services.
- The **policy** field of **deviceNode** in the **device_info.hcs** file can be **1** or **2**, but not **0**.

The UART module is divided into the following layers:

- Interface layer: provides APIs for opening or closing a UART device, reading or writing data of the specified length, setting or obtaining the baud rate or attributes of a UART device, and setting the transmission mode.
- Core layer: provides the capabilities of adding or removing a UART controller, and managing UART devices. The core layer interacts with the adaptation layer through hook functions.
- Adaptation layer: instantiates the hook functions to implement specific features.

**Figure 3** Independent service mode

![image3](figures/independent-service-mode.png)
A
Annie_wang 已提交
63 64 65

## Usage Guidelines

A
Annie_wang 已提交
66
### When to Use
A
Annie_wang 已提交
67

A
Annie_wang 已提交
68
The UART module is widely used to implement low-speed serial communication between devices, for example, output the printing information. It can also connect to a variety of external GPS and Bluetooth devices.
D
duangavin123 已提交
69

A
Annie_wang 已提交
70
### Available APIs
D
duangavin123 已提交
71

A
Annie_wang 已提交
72
**Table 1** UART driver APIs
A
Annie_wang 已提交
73

A
Annie_wang 已提交
74 75 76 77 78 79 80 81 82 83 84
| API| Description|
| -------- | -------- |
| DevHandle UartOpen(uint32_t port) | Opens a UART device.|
| void UartClose(DevHandle handle) | Closes a UART device.|
| int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size) | Reads data of the specified length from a UART device.|
| int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size) | Writes data of the specified length to a UART device.|
| int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate) | Obtains the UART baud rate.|
| int32_t UartSetBaud(DevHandle handle, uint32_t baudRate) | Sets the UART baud rate.|
| int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute) | Obtains UART device attributes.|
| int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute) | Sets UART device attributes.|
| int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode) | Sets the UART transmission mode.|
A
Annie_wang 已提交
85

A
Annie_wang 已提交
86 87 88
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
>
> All the UART APIs described in this document can be used in kernel mode and user mode.
A
Annie_wang 已提交
89

A
Annie_wang 已提交
90
### How to Develop
A
Annie_wang 已提交
91

A
Annie_wang 已提交
92
The following figure illustrates how to use the UART APIs.
A
Annie_wang 已提交
93

A
Annie_wang 已提交
94 95 96 97 98 99 100 101 102 103
**Figure 4** Using UART driver APIs

![image4](figures/using-UART-process.png)


#### Opening a UART Device

Before performing UART communication, use **UartOpen()** to obtain a UART device handle based on the port number.

```c
A
Annie_wang 已提交
104 105 106
DevHandle UartOpen(uint32_t port);
```

A
Annie_wang 已提交
107
**Table 2** Description of UartOpen
A
Annie_wang 已提交
108

A
Annie_wang 已提交
109
| Parameter| Description|
A
Annie_wang 已提交
110
| -------- | -------- |
A
Annie_wang 已提交
111 112 113 114 115 116 117 118 119 120
| port | UART port number.|
| **Return Value**| **Description**|
| NULL | The operation fails.|
| Device handle| The operation is successful. The obtained UART device handle is returned.|

Example: Obtain the device handle of UART port 1.

```c
DevHandle handle = NULL;    // UART device handle.
uint32_t port = 1;          // UART device port number.
A
Annie_wang 已提交
121

W
wenjun 已提交
122 123
handle = UartOpen(port);
if (handle == NULL) {
A
Annie_wang 已提交
124
    HDF_LOGE("UartOpen: open uart_%u failed!\n", port);
W
wenjun 已提交
125 126 127 128
    return;
}
```

A
Annie_wang 已提交
129
#### Setting the UART Baud Rate
W
wenjun 已提交
130

A
Annie_wang 已提交
131
Use **UartSetBaud()** to set the UART baud rate.
A
Annie_wang 已提交
132

A
Annie_wang 已提交
133
```c
A
Annie_wang 已提交
134 135 136
int32_t UartSetBaud(DevHandle handle, uint32_t baudRate);
```

A
Annie_wang 已提交
137
**Table 3** Description of UartSetBaud
A
Annie_wang 已提交
138

A
Annie_wang 已提交
139
| Parameter| Description|
A
Annie_wang 已提交
140
| -------- | -------- |
A
Annie_wang 已提交
141 142 143 144 145
| handle | UART device handle.|
| baudRate | Baud rate to set.|
| **Return Value**| **Description**|
| HDF_SUCCESS | The operation is successful.|
| Negative value| The operation fails.|
A
Annie_wang 已提交
146 147 148

Example: Set the UART baud rate to **9600**.

A
Annie_wang 已提交
149
```c
W
wenjun 已提交
150
int32_t ret;
A
Annie_wang 已提交
151 152 153

ret = UartSetBaud(handle, 9600); // Set the UART baud rate.
if (ret != HDF_SUCCESS) {
W
wenjun 已提交
154
    HDF_LOGE("UartSetBaud: failed, ret %d\n", ret);
A
Annie_wang 已提交
155
    return ret;
W
wenjun 已提交
156 157 158
}
```

A
Annie_wang 已提交
159
#### Obtaining the UART Baud Rate
W
wenjun 已提交
160

A
Annie_wang 已提交
161
Use **UartGetBaud()** to obtain the UART baud rate.
A
Annie_wang 已提交
162

A
Annie_wang 已提交
163
```c
A
Annie_wang 已提交
164 165 166
int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate);
```

A
Annie_wang 已提交
167
**Table 4** Description of UartGetBaud
A
Annie_wang 已提交
168

A
Annie_wang 已提交
169
| Parameter| Description|
A
Annie_wang 已提交
170
| -------- | -------- |
A
Annie_wang 已提交
171 172 173 174 175
| handle | UART device handle.|
| baudRate | Pointer to the UART baud rate obtained.|
| **Return Value**| **Description**|
| HDF_SUCCESS | The operation is successful.|
| Negative value| The operation fails.|
A
Annie_wang 已提交
176 177 178

Example: Obtain the UART baud rate.

A
Annie_wang 已提交
179
```c
W
wenjun 已提交
180 181
int32_t ret;
uint32_t baudRate;
A
Annie_wang 已提交
182 183 184

ret = UartGetBaud(handle, &baudRate);    // Obtain the UART baud rate.
if (ret != HDF_SUCCESS) {
W
wenjun 已提交
185
    HDF_LOGE("UartGetBaud: failed, ret %d\n", ret);
A
Annie_wang 已提交
186
    return ret;
W
wenjun 已提交
187 188 189
}
```

A
Annie_wang 已提交
190
#### Setting UART Device Attributes
W
wenjun 已提交
191

A
Annie_wang 已提交
192
Use **UartSetAttribute()** to set UART device attributes.
A
Annie_wang 已提交
193

A
Annie_wang 已提交
194
```c
A
Annie_wang 已提交
195 196 197
int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute);
```

A
Annie_wang 已提交
198
**Table 5** Description of UartSetAttribute
A
Annie_wang 已提交
199

A
Annie_wang 已提交
200
| Parameter| Description|
A
Annie_wang 已提交
201
| -------- | -------- |
A
Annie_wang 已提交
202 203 204 205 206
| handle | UART device handle.|
| attribute | Pointer to the UART device attributes to set.|
| **Return Value**| **Description**|
| HDF_SUCCESS | The operation is successful.|
| Negative value| The operation fails.|
A
Annie_wang 已提交
207 208 209

Example: Set UART device attributes.

A
Annie_wang 已提交
210
```c
W
wenjun 已提交
211 212
int32_t ret;
struct UartAttribute attribute;
A
Annie_wang 已提交
213 214 215 216 217 218 219 220 221 222 223

attribute.dataBits = UART_ATTR_DATABIT_7;     // Transfer 7 bits each time.
attribute.parity = UART_ATTR_PARITY_NONE;     // Disable parity check for the data to transfer.
attribute.stopBits = UART_ATTR_STOPBIT_1;     // Set the stop bit to 1.
attribute.rts = UART_ATTR_RTS_DIS;            // Disable RTS.
attribute.cts = UART_ATTR_CTS_DIS;            // Disable CTS.
attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;    // Enable RX FIFO.
attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;    // Enable TX FIFO.

ret = UartSetAttribute(handle, &attribute);   // Set UART device attributes.
if (ret != HDF_SUCCESS) {
W
wenjun 已提交
224
    HDF_LOGE("UartSetAttribute: failed, ret %d\n", ret);
A
Annie_wang 已提交
225
turn ret;
W
wenjun 已提交
226 227 228
}
```

A
Annie_wang 已提交
229
#### Obtaining UART Device Attributes
W
wenjun 已提交
230

A
Annie_wang 已提交
231
Use **UartGetAttribute()** to obtain the UART device attributes.
A
Annie_wang 已提交
232

A
Annie_wang 已提交
233
```c
A
Annie_wang 已提交
234 235 236
int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute);
```

A
Annie_wang 已提交
237
**Table 6** Description of UartGetAttribute
A
Annie_wang 已提交
238

A
Annie_wang 已提交
239
| Parameter| Description|
A
Annie_wang 已提交
240
| -------- | -------- |
A
Annie_wang 已提交
241 242 243 244 245
| handle | UART device handle.|
| attribute | Pointer to the UART device attributes obtained.|
| **Return Value**| **Description**|
| HDF_SUCCESS | The operation is successful.|
| Negative value| The operation fails.|
A
Annie_wang 已提交
246 247 248

Example: Obtain UART device attributes.

A
Annie_wang 已提交
249
```c
W
wenjun 已提交
250 251
int32_t ret;
struct UartAttribute attribute;
A
Annie_wang 已提交
252 253 254

ret = UartGetAttribute(handle, &attribute);    // Obtain the attributes of the UART device.
if (ret != HDF_SUCCESS) {
W
wenjun 已提交
255
    HDF_LOGE("UartGetAttribute: failed, ret %d\n", ret);
A
Annie_wang 已提交
256
    return ret;
W
wenjun 已提交
257 258 259
}
```

A
Annie_wang 已提交
260
#### Setting the UART Transmission Mode
W
wenjun 已提交
261

A
Annie_wang 已提交
262
Use **UartSetTransMode()** to set the UART transmission mode.
A
Annie_wang 已提交
263

A
Annie_wang 已提交
264
```c
A
Annie_wang 已提交
265 266 267
int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode);
```

A
Annie_wang 已提交
268
**Table 7** Description of UartSetTransMode
A
Annie_wang 已提交
269

A
Annie_wang 已提交
270
| Parameter| Description|
A
Annie_wang 已提交
271
| -------- | -------- |
A
Annie_wang 已提交
272 273 274 275 276
| handle | UART device handle.|
| mode | UART transmission mode to set.|
| **Return Value**| **Description**|
| HDF_SUCCESS | The operation is successful.|
| Negative value| The operation fails.|
A
Annie_wang 已提交
277 278 279

Example: Set the UART transmission mode to **UART_MODE_RD_BLOCK**.

A
Annie_wang 已提交
280
```c
W
wenjun 已提交
281
int32_t ret;
A
Annie_wang 已提交
282 283 284

ret = UartSetTransMode(handle, UART_MODE_RD_BLOCK);    // Sets the UART transmission mode.
if (ret != HDF_SUCCESS) {
W
wenjun 已提交
285
    HDF_LOGE("UartSetTransMode: failed, ret %d\n", ret);
A
Annie_wang 已提交
286
    return ret;
W
wenjun 已提交
287 288 289
}
```

A
Annie_wang 已提交
290
#### Writing Data to a UART Device
W
wenjun 已提交
291

A
Annie_wang 已提交
292
Use **UartWrite()** to write data of the specified length to a UART device.
A
Annie_wang 已提交
293

A
Annie_wang 已提交
294
```c
A
Annie_wang 已提交
295 296 297
int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size);
```

A
Annie_wang 已提交
298
**Table 8** Description of UartWrite
A
Annie_wang 已提交
299

A
Annie_wang 已提交
300
| Parameter| Description|
A
Annie_wang 已提交
301
| -------- | -------- |
A
Annie_wang 已提交
302 303 304 305 306 307
| handle | UART device handle.|
| data | Pointer to the data to write.|
| size | Length of the data to write.|
| **Return Value**| **Description**|
| HDF_SUCCESS | The operation is successful.|
| Negative value| The operation fails.|
A
Annie_wang 已提交
308 309 310

Example: Write data to a UART device.

A
Annie_wang 已提交
311
```c
W
wenjun 已提交
312 313
int32_t ret;
uint8_t wbuff[5] = {1, 2, 3, 4, 5};
A
Annie_wang 已提交
314 315 316

ret = UartWrite(handle, wbuff, 5);    // Write data of the specified length to the UART device.
if (ret != HDF_SUCCESS) {
W
wenjun 已提交
317
    HDF_LOGE("UartWrite: failed, ret %d\n", ret);
A
Annie_wang 已提交
318
    return ret;
W
wenjun 已提交
319 320 321
}
```

A
Annie_wang 已提交
322
#### Reading Data from a UART Device
W
wenjun 已提交
323

A
Annie_wang 已提交
324
Use **UartRead()** to read data of the specified length from a UART device.
A
Annie_wang 已提交
325

A
Annie_wang 已提交
326
```c
A
Annie_wang 已提交
327 328 329
int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size);
```

A
Annie_wang 已提交
330
**Table 9** Description of UartRead
A
Annie_wang 已提交
331

A
Annie_wang 已提交
332
| Parameter| Description|
A
Annie_wang 已提交
333
| -------- | -------- |
A
Annie_wang 已提交
334 335 336 337 338 339
| handle | UART device handle.|
| data | Pointer to the buffer for receiving the data.|
| size | Length of the data to read.|
| **Return Value**| **Description**|
| Non-negative value| The operation is successful. The length of the data read is returned.|
| Negative value| The operation fails.|
A
Annie_wang 已提交
340 341 342

Example: Read data of the specified length from a UART device.

A
Annie_wang 已提交
343
```c
W
wenjun 已提交
344 345
int32_t ret;
uint8_t rbuff[5] = {0};
A
Annie_wang 已提交
346 347

ret = UartRead(handle, rbuff, 5);    // Read data of the specified length from the UART device.
W
wenjun 已提交
348 349
if (ret < 0) {
    HDF_LOGE("UartRead: failed, ret %d\n", ret);
A
Annie_wang 已提交
350
	return ret;
W
wenjun 已提交
351 352 353
}
```

A
Annie_wang 已提交
354 355
> ![icon-caution.gif](../public_sys-resources/icon-caution.gif) **CAUTION**<br/>
> 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.
W
wenjun 已提交
356 357


A
Annie_wang 已提交
358
#### Closing a UART Device
W
wenjun 已提交
359

A
Annie_wang 已提交
360
Use **UartClose()** to close a UART device.
W
wenjun 已提交
361

A
Annie_wang 已提交
362
```c
A
Annie_wang 已提交
363 364
void UartClose(DevHandle handle);
```
W
wenjun 已提交
365

A
Annie_wang 已提交
366
This function releases the resources requested by **UartOpen**.
W
wenjun 已提交
367

A
Annie_wang 已提交
368
**Table 10** Description of UartClose
W
wenjun 已提交
369

A
Annie_wang 已提交
370
| Parameter| Description|
A
Annie_wang 已提交
371
| -------- | -------- |
A
Annie_wang 已提交
372
| handle | UART device handle to close.|
W
wenjun 已提交
373

A
Annie_wang 已提交
374
Example: Close a UART device.
A
Annie_wang 已提交
375

A
Annie_wang 已提交
376 377
```c
UartClose(handle);    // Close a UART device to release resources.
W
wenjun 已提交
378
```
D
duangavin123 已提交
379

A
Annie_wang 已提交
380
## Example
D
duangavin123 已提交
381

A
Annie_wang 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394
The following uses the Hi3516D V300 development board as an example to describe how to manage the UART device. The procedure is as follows: 

1. Open a UART device based on the port number. The handle of the UART device opened is returned.
2. Set the baud rate of the UART device.
3. Obtain the baud rate of the UART device.
4. Set the attributes of the UART device.
5. Obtain the attributes of the UART device.
6. Set the transmission mode of the UART device.
7. Transfer data of the specified length.
8. Receive data of the specified length.
9. Closes the UART device.

```c
D
duangavin123 已提交
395 396 397 398 399 400
#include "hdf_log.h"
#include "uart_if.h"

void UartTestSample(void)
{
    int32_t ret;
A
Annie_wang 已提交
401 402
    uint32_t port;
    uint32_t baud;
D
duangavin123 已提交
403 404 405 406
    DevHandle handle = NULL;
    uint8_t wbuff[5] = { 1, 2, 3, 4, 5 };
    uint8_t rbuff[5] = { 0 };
    struct UartAttribute attribute;
A
Annie_wang 已提交
407 408 409 410 411 412 413 414 415 416 417 418

    attribute.dataBits = UART_ATTR_DATABIT_7;                  // Transfer 7 bits each time.
    attribute.parity = UART_ATTR_PARITY_NONE;                  // Disable parity check.
    attribute.stopBits = UART_ATTR_STOPBIT_1;                  // Set the stop bit to 1.
    attribute.rts = UART_ATTR_RTS_DIS;                         // Disable RTS.
    attribute.cts = UART_ATTR_CTS_DIS;                         // Disable CTS.
    attribute.fifoRxEn = UART_ATTR_RX_FIFO_EN;                 // Enable RX FIFO.
    attribute.fifoTxEn = UART_ATTR_TX_FIFO_EN;                 // Enable TX FIFO.

    port = 1;                                                  // UART device port number.

    handle = UartOpen(port);                                   // Open a UART device.
D
duangavin123 已提交
419
    if (handle == NULL) {
A
Annie_wang 已提交
420
        HDF_LOGE("UartOpen: open uart_%u failed!\n", port);
D
duangavin123 已提交
421 422
        return;
    }
A
Annie_wang 已提交
423 424 425 426 427

    ret = UartSetBaud(handle, 9600);                           // Set the UART baud rate to 9600.
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("UartSetBaud: set baud failed, ret %d\n", ret);
        goto ERR;
D
duangavin123 已提交
428
    }
A
Annie_wang 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

    ret = UartGetBaud(handle, &baud);                          // Obtain the UART baud rate.
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("UartGetBaud: get baud failed, ret %d\n", ret);
        goto ERR;
    }

    ret = UartSetAttribute(handle, &attribute);                // Set the attributes of the UART device.
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("UartSetAttribute: set attribute failed, ret %d\n", ret);
        goto ERR;
    }

    ret = UartGetAttribute(handle, &attribute);                // Obtain the attributes of the UART device.
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("UartGetAttribute: get attribute failed, ret %d\n", ret);
        goto ERR;
D
duangavin123 已提交
446
    }
A
Annie_wang 已提交
447 448 449 450 451

    ret = UartSetTransMode(handle, UART_MODE_RD_NONBLOCK);     // Set the UART transmission mode to non-block mode.
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("UartSetTransMode: set trans mode failed, ret %d\n", ret);
        goto ERR;
D
duangavin123 已提交
452
    }
A
Annie_wang 已提交
453 454 455 456 457

    ret = UartWrite(handle, wbuff, 5);                         // Write 5-byte data to the UART device.
    if (ret != HDF_SUCCESS) {
        HDF_LOGE("UartWrite: write data failed, ret %d\n", ret);
        goto ERR;
D
duangavin123 已提交
458
    }
A
Annie_wang 已提交
459 460

    ret = UartRead(handle, rbuff, 5);                          // Read 5-byte data from the UART device.
D
duangavin123 已提交
461
    if (ret < 0) {
A
Annie_wang 已提交
462 463
        HDF_LOGE("UartRead: read data failed, ret %d\n", ret);
        goto ERR;
D
duangavin123 已提交
464
    }
A
Annie_wang 已提交
465 466 467
ERR:
    UartClose(handle);                                         // Close the UART device.
	return ret;
D
duangavin123 已提交
468
}
A
Annie_wang 已提交
469
```