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