]> granicus.if.org Git - esp-idf/commitdiff
uart: Add api call to switch on hardware support for 'software' flow control.
authorAlex Burr <alex.burr@kano.me>
Tue, 1 Aug 2017 15:02:44 +0000 (16:02 +0100)
committerAngus Gratton <gus@projectgus.com>
Tue, 5 Sep 2017 06:30:21 +0000 (16:30 +1000)
Cherry-picks https://github.com/espressif/esp-idf/pull/890

components/driver/include/driver/uart.h
components/driver/uart.c

index 9bd35e4359035869e09aa9b462810fb2f76fd2e2..1dfcd50c9cec8e922a64daa49f207afc44d7a6be 100644 (file)
@@ -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
  *
index 5371a447c8feb40371b9f2276b0ad05b250a5d64..fffae17344cd24f88bca85de4551ea482bf3fd31 100644 (file)
@@ -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)
 {