From 0d2de997781b43eb7a465f9da0888f6951ba3ead Mon Sep 17 00:00:00 2001 From: Chun-Chieh Li Date: Thu, 3 Mar 2022 14:26:55 +0800 Subject: [PATCH] ESP8266: Fix serial flow control inconsistency on reconnect The commit will address the test failure of connectivity-netsocket-tests-tests-network-interface. In the test, serial channel will break with the sequence: ESP8266Interface::connect() > disconnect() > connect() In the first connect, both board's and ESP8266's serial flow control default to disabled, and then enabled. In the second connect, board's serial flow control keeps enabled but ESP8266's resets to disabled, causing inconsistency between two ends. The approach: Explicitly disable board's serial flow control on re-power or reset in (re-)connect --- .../wifi/esp8266-driver/ESP8266/ESP8266.cpp | 10 ++++++---- .../drivers/wifi/esp8266-driver/ESP8266/ESP8266.h | 3 ++- .../wifi/esp8266-driver/ESP8266Interface.cpp | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.cpp b/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.cpp index 56dd02ae99..12439e9180 100644 --- a/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.cpp +++ b/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.cpp @@ -193,7 +193,7 @@ struct ESP8266::fw_at_version ESP8266::at_version() return _at_v; } -bool ESP8266::stop_uart_hw_flow_ctrl(void) +bool ESP8266::stop_uart_hw_flow_ctrl(bool board_only) { bool done = true; #if DEVICE_SERIAL_FC @@ -202,9 +202,11 @@ bool ESP8266::stop_uart_hw_flow_ctrl(void) // Stop board's flow control _serial.set_flow_control(SerialBase::Disabled, _serial_rts, _serial_cts); - // Stop ESP8266's flow control - done = _parser.send("AT+UART_CUR=%u,8,1,0,0", MBED_CONF_ESP8266_SERIAL_BAUDRATE) - && _parser.recv("OK\n"); + if (!board_only) { + // Stop ESP8266's flow control + done = _parser.send("AT+UART_CUR=%u,8,1,0,0", MBED_CONF_ESP8266_SERIAL_BAUDRATE) + && _parser.recv("OK\n"); + } } #endif diff --git a/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.h b/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.h index 4b5b189434..963e8619b8 100644 --- a/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.h +++ b/connectivity/drivers/wifi/esp8266-driver/ESP8266/ESP8266.h @@ -455,9 +455,10 @@ public: /** * Stop board's and ESP8266's UART flow control * + * @param board_only true to apply to board only, false to apply both * @return true if started */ - bool stop_uart_hw_flow_ctrl(); + bool stop_uart_hw_flow_ctrl(bool board_only = false); /* * From AT firmware v1.7.0.0 onwards enables TCP passive mode diff --git a/connectivity/drivers/wifi/esp8266-driver/ESP8266Interface.cpp b/connectivity/drivers/wifi/esp8266-driver/ESP8266Interface.cpp index fc9ed25d24..ed040325ce 100644 --- a/connectivity/drivers/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/connectivity/drivers/wifi/esp8266-driver/ESP8266Interface.cpp @@ -722,8 +722,14 @@ bool ESP8266Interface::_get_firmware_ok() nsapi_error_t ESP8266Interface::_init(void) { if (!_initialized) { - _pwr_pin.power_off(); - _pwr_pin.power_on(); + if (_pwr_pin.is_connected()) { + _pwr_pin.power_off(); + _pwr_pin.power_on(); + /* Align board's serial flow control with ESP8266's, resetting to disabled on re-power or reset */ + if (!_esp.stop_uart_hw_flow_ctrl(true)) { + return NSAPI_ERROR_DEVICE_ERROR; + } + } if (_reset() != NSAPI_ERROR_OK) { return NSAPI_ERROR_DEVICE_ERROR; @@ -780,6 +786,10 @@ nsapi_error_t ESP8266Interface::_reset() ThisThread::sleep_for(delay); _esp.flush(); _rst_pin.rst_deassert(); + /* Align board's serial flow control with ESP8266's, resetting to disabled on re-power or reset */ + if (!_esp.stop_uart_hw_flow_ctrl(true)) { + return NSAPI_ERROR_DEVICE_ERROR; + } } else { _esp.flush(); if (!_esp.at_available()) {