/** * @brief UART configuration parameters for uart_param_config function */ typedef struct { int baud_rate; /*!< UART baud rate*/ uart_word_length_t data_bits; /*!< UART byte size*/ uart_parity_t parity; /*!< UART parity mode*/ uart_stop_bits_t stop_bits; /*!< UART stop bits*/ uart_hw_flowcontrol_t flow_ctrl; /*!< UART HW flow control mode (cts/rts)*/ uint8_t rx_flow_ctrl_thresh; /*!< UART HW RTS threshold*/ union { uart_sclk_t source_clk; /*!< UART source clock selection */
bool use_ref_tick __attribute__((deprecated)); /*!< Deprecated method to select ref tick clock source, set source_clk field instead */ }; } uart_config_t;
ESP32總共有三個串口分別是UART_NUM0,UART_NUM1,UART_NUM2。串口0作為軟件日志打印接口,其他串口可以為我們所用,比較靈活的是
所有串口的IO都可以自由設置。上面結構體是串口配置結構體包含了串口的波特率,數據長度,奇偶檢驗,停止位,硬件流控制,還有時鍾源。
esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, QueueHandle_t* uart_queue, int intr_alloc_flags);
串口驅動安裝函數,參數有串口號,接收緩存大小,發送緩存大小,隊列大小,隊列句柄,中斷標志位
uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config);
串口參數配置函數,參數有串口號,以及串口參數配置結構體
uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)
串口引腳設置,可以設置串口4個引腳,一般設置TX RX即可。
int uart_read_bytes(uart_port_t uart_num, void *buf, uint32_t length, TickType_t ticks_to_wait) int uart_write_bytes(uart_port_t uart_num, const void *src, size_t size)
第一個函數是讀函數,參數有串口號,緩存地址,緩存長度,等待時間
第二個函數是寫函數,也就是發送函數,參數有串口號,數據源首地址,發送數據長度