]> granicus.if.org Git - esp-idf/commitdiff
uart/driver: Add module reset before enabling
authorKonstantin Kondrashov <konstantin@espressif.com>
Mon, 22 Apr 2019 06:09:24 +0000 (14:09 +0800)
committerKonstantinKondrashov <konstantin@espressif.com>
Fri, 11 Oct 2019 11:13:44 +0000 (19:13 +0800)
This commit prevents infinite restarts caused due to an interrupt flag
was left uncleared.

Closes: https://github.com/espressif/esp-idf/issues/1981
Closes: IDF-188
components/driver/uart.c

index 188be1418dc0de670728db612991e9c3865b7342..f45e417de1b9b12ed55fc1d471d3d175367be226 100644 (file)
@@ -649,18 +649,31 @@ esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num)
     return ESP_OK;
 }
 
+static periph_module_t get_periph_module(uart_port_t uart_num)
+{
+    periph_module_t periph_module = PERIPH_UART0_MODULE;
+    if (uart_num == UART_NUM_0) {
+        periph_module = PERIPH_UART0_MODULE;
+    } else if (uart_num == UART_NUM_1) {
+        periph_module = PERIPH_UART1_MODULE;
+    } else if (uart_num == UART_NUM_2) {
+        periph_module = PERIPH_UART2_MODULE;
+    } else {
+        assert(0 && "uart_num error");
+    }
+    return periph_module;
+}
+
 esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)
 {
     esp_err_t r;
     UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL);
     UART_CHECK((uart_config), "param null", ESP_FAIL);
-    if(uart_num == UART_NUM_0) {
-        periph_module_enable(PERIPH_UART0_MODULE);
-    } else if(uart_num == UART_NUM_1) {
-        periph_module_enable(PERIPH_UART1_MODULE);
-    } else if(uart_num == UART_NUM_2) {
-        periph_module_enable(PERIPH_UART2_MODULE);
+    periph_module_t periph_module = get_periph_module(uart_num);
+    if (uart_num != CONFIG_CONSOLE_UART_NUM) {
+        periph_module_reset(periph_module);
     }
+    periph_module_enable(periph_module);
     r = uart_set_hw_flow_ctrl(uart_num, uart_config->flow_ctrl, uart_config->rx_flow_ctrl_thresh);
     if (r != ESP_OK) return r;
 
@@ -1445,14 +1458,9 @@ esp_err_t uart_driver_delete(uart_port_t uart_num)
     free(p_uart_obj[uart_num]);
     p_uart_obj[uart_num] = NULL;
 
-    if (uart_num != CONFIG_CONSOLE_UART_NUM ) {
-       if(uart_num == UART_NUM_0) {
-           periph_module_disable(PERIPH_UART0_MODULE);
-       } else if(uart_num == UART_NUM_1) {
-           periph_module_disable(PERIPH_UART1_MODULE);
-       } else if(uart_num == UART_NUM_2) {
-           periph_module_disable(PERIPH_UART2_MODULE);
-       }
+    if (uart_num != CONFIG_CONSOLE_UART_NUM) {
+       periph_module_t periph_module = get_periph_module(uart_num);
+       periph_module_disable(periph_module);
     }
     return ESP_OK;
 }