diff --git a/components/driver/include/driver/uart.h b/components/driver/include/driver/uart.h index 9bd35e435..1dfcd50c9 100644 --- a/components/driver/include/driver/uart.h +++ b/components/driver/include/driver/uart.h @@ -268,6 +268,20 @@ esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask); */ esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh); +/** + * @brief Set software flow control. + * + * @param uart_num UART_NUM_0, UART_NUM_1 or UART_NUM_2 + * @param enable switch on or off + * @param rx_thresh_xon low water mark + * @param rx_thresh_xoff high water mark + * + * @return + * - ESP_OK Success + * - ESP_FAIL Parameter error + */ + esp_err_t uart_set_sw_flow_ctrl(uart_port_t uart_num, bool enable, uint8_t rx_thresh_xon, uint8_t rx_thresh_xoff); + /** * @brief Get hardware flow control mode * diff --git a/components/driver/uart.c b/components/driver/uart.c index 5371a447c..fffae1734 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -29,6 +29,9 @@ #include "driver/uart.h" #include "driver/gpio.h" +#define XOFF (char)0x13 +#define XON (char)0x11 + static const char* UART_TAG = "uart"; #define UART_CHECK(a, str, ret_val) \ if (!(a)) { \ @@ -199,6 +202,22 @@ esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask) return ESP_OK; } +esp_err_t uart_set_sw_flow_ctrl(uart_port_t uart_num, bool enable, uint8_t rx_thresh_xon, uint8_t rx_thresh_xoff) +{ + UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); + UART_CHECK((rx_thresh_xon < UART_FIFO_LEN), "rx flow xon thresh error", ESP_FAIL); + UART_CHECK((rx_thresh_xoff < UART_FIFO_LEN), "rx flow xon thresh error", ESP_FAIL); + UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); + UART[uart_num]->flow_conf.sw_flow_con_en = enable? 1:0; + UART[uart_num]->flow_conf.xonoff_del = enable?1:0; + UART[uart_num]->swfc_conf.xon_threshold = rx_thresh_xon; + UART[uart_num]->swfc_conf.xoff_threshold = rx_thresh_xoff; + UART[uart_num]->swfc_conf.xon_char = XON; + UART[uart_num]->swfc_conf.xoff_char = XOFF; + UART_EXIT_CRITICAL(&uart_spinlock[uart_num]); + return ESP_OK; +} + //only when UART_HW_FLOWCTRL_RTS is set , will the rx_thresh value be set. esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh) {