UART 驅動 API 接口如下所示,具體的 API 詳見 drivers/hdf_core/framework/include/platform/uart_if.h 文件。 ![]() (1) UartOpen 在使用 UART 進行通信時,首先要調用 UartOpen 獲取 UART 設備句柄,該函數會返回指定端口號的 UART 設備句柄。函數原型如下所示: DevHandle UartOpen(uint32_t port); 其中,參數 port 是 UART 設備號。UartOpen 返回值為 NULL 表示獲取 UART 設備句柄失敗,正常情況下返回 UART 設備句柄。 假設系統重的 UART 端口號為 4,獲取該 UART 設備句柄的示例如下所示: ![]() (2) UartSetBaud 在通信之前,需要設置 UART 的波特率,函數原型如下所示: int32_t UartSetBaud(DevHandle handle, uint32_t baudRate); 其中,參數 handle 表示 UART 設備句柄,baudRate 表示待設置的波特率值。UartSetBaud 返回值為 HDF_SUCCESS 表示波特率設置成功,返回值為負數表示 UART 設置波特率失敗。 (3) UartGetBaud 設置 UART 的波特率后,可以通過獲取波特率接口來查看 UART 當前的波特率。函數原型如下所示: int32_t UartGetBaud(DevHandle handle, uint32_t *baudRate); 其中,參數 handle 表示 UART 設備句柄,baudRate 表示待設置的波特率值。UartSetBaud 返回值為 HDF_SUCCESS 表示獲取波特率成功,返回值為負數表示 UART 獲取波特率失敗。 (4) UartSetAttribute 在通信之前,需要設置 UART 的設備屬性。函數原型如下所示: int32_t UartSetAttribute(DevHandle handle, struct UartAttribute *attribute); 其中,handle 表示 UARt 設備句柄,attribute 表示待設置的設備屬性。UartSetAttribute 返回值為 HDF_SUCCESS 表示 UART 設置屬性成功,返回值為負數表示 UART 設置設備屬性失敗。 (5) UartGetAttribute 設置 UART 的設備屬性后,可以通過獲取設備屬性接口來查看 UART 當前的設備屬性。函數原型如下所示: int32_t UartGetAttribute(DevHandle handle, struct UartAttribute *attribute); 其中,handle 表示 UART 設備句柄,attribute 表示接收 UART 設備屬性的指針。UartGetAttribute返回值為 HDF_SUCCESS 表示 UART 獲取屬性成功,返回值為負數表示 UART 獲取設備屬性失敗。 (6) UartSetTransMode 在通信之前,需要設置 UART 的傳輸模式。函數原型如下所示: int32_t UartSetTransMode(DevHandle handle, enum UartTransMode mode); 其中,handle 表示 UART 設備句柄,mode 表示待設置的傳輸模式。UartSetTransMode 返回值為 HDF_SUCCESS 表示 UART 設置傳輸模式成功,返回值返回負數表示 UART 設置傳輸模式失敗。 (7) UartWrite 向 UART 設備寫入指定長度的數據。函數原型如下所示: int32_t UartWrite(DevHandle handle, uint8_t *data, uint32_t size); 其中,handle 表示 UART 設備句柄,data 表示待寫入數據的指針,size 表示待寫入數據的長度。UartWrite 返回值為 HDF_SUCCESS 表示 UART 寫數據成功,返回值為負數表示 UART 寫數據失敗。 (8) UartRead 從 UART 設備中讀取指定長度的數據,函數原型如下所示: int32_t UartRead(DevHandle handle, uint8_t *data, uint32_t size); 其中,參數 handle 表示 UART 設備句柄,data 表示接收讀取數據的指針,size 表示待讀取數據的長度。UartRead 返回值為非負數表示 UART 讀取到的數據長度,返回值為負數,表示 UART讀取數據失敗。 (9) UartClose UART 通信完成之后,需要銷毀 UART 設備句柄,函數原型如下所示: void UartClose(DevHandle handle); 其中,參數 handle 表示 UART 設備句柄。 |