From 7db22924170f7ce3d9b5400d4d4bb6e67ea6a1e8 Mon Sep 17 00:00:00 2001 From: Seppo Takalo Date: Thu, 10 Jan 2019 18:13:32 +0200 Subject: [PATCH] Flush ESP8266 serial input buffers after HW reset --- components/wifi/esp8266-driver/ESP8266/ESP8266.cpp | 7 +++++++ components/wifi/esp8266-driver/ESP8266/ESP8266.h | 9 +++++++++ components/wifi/esp8266-driver/ESP8266Interface.cpp | 13 ++++++++----- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp index b0a5844885..0533c1984f 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.cpp @@ -1114,6 +1114,13 @@ int8_t ESP8266::default_wifi_mode() return 0; } +void ESP8266::flush() +{ + _smutex.lock(); + _parser.flush(); + _smutex.unlock(); +} + bool ESP8266::set_default_wifi_mode(const int8_t mode) { _smutex.lock(); diff --git a/components/wifi/esp8266-driver/ESP8266/ESP8266.h b/components/wifi/esp8266-driver/ESP8266/ESP8266.h index 78879228df..68eb5f969f 100644 --- a/components/wifi/esp8266-driver/ESP8266/ESP8266.h +++ b/components/wifi/esp8266-driver/ESP8266/ESP8266.h @@ -367,6 +367,15 @@ public: */ void bg_process_oob(uint32_t timeout, bool all); + /** + * Flush the serial port input buffers. + * + * If you do HW reset for ESP module, you should + * flush the input buffers from existing responses + * from the device. + */ + void flush(); + static const int8_t WIFIMODE_STATION = 1; static const int8_t WIFIMODE_SOFTAP = 2; static const int8_t WIFIMODE_STATION_SOFTAP = 3; diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index 72c9a7fd07..9ef3c73bfb 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -368,11 +368,14 @@ nsapi_error_t ESP8266Interface::_init(void) void ESP8266Interface::_hw_reset() { - _rst_pin.rst_assert(); - // If you happen to use Pin7 CH_EN as reset pin, not needed otherwise - // https://www.espressif.com/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf - wait_ms(2); // Documentation says 200 us should have been enough, but experimentation shows that 1ms was not enough - _rst_pin.rst_deassert(); + if (_rst_pin.is_connected()) { + _rst_pin.rst_assert(); + // If you happen to use Pin7 CH_EN as reset pin, not needed otherwise + // https://www.espressif.com/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf + wait_ms(2); // Documentation says 200 us should have been enough, but experimentation shows that 1ms was not enough + _esp.flush(); + _rst_pin.rst_deassert(); + } } struct esp8266_socket {