From c0476d847f4e34530803450cf7b8a9c67795d946 Mon Sep 17 00:00:00 2001 From: toyowata Date: Tue, 23 Jun 2020 10:48:54 +0900 Subject: [PATCH 1/3] Migrate to Mbed OS 6 APIs and add reset feature --- ESP32/ESP32.cpp | 18 ++++++++++--- ESP32/ESP32.h | 15 ++++++++--- ESP32Interface.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++ ESP32Interface.h | 22 +++++++++++++++- 4 files changed, 110 insertions(+), 8 deletions(-) diff --git a/ESP32/ESP32.cpp b/ESP32/ESP32.cpp index fc68316..9984386 100644 --- a/ESP32/ESP32.cpp +++ b/ESP32/ESP32.cpp @@ -24,6 +24,8 @@ using namespace mbed; using namespace rtos; +using namespace std::chrono; +using std::milli; ESP32 * ESP32::instESP32 = NULL; @@ -191,7 +193,7 @@ void ESP32::_startup_common() } if (_p_wifi_en != NULL) { _p_wifi_en->write(0); - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); _p_wifi_en->write(1); _parser.recv("ready"); } else { @@ -351,7 +353,7 @@ bool ESP32::accept(int * p_id) } _smutex.unlock(); if (!ret) { - ThisThread::sleep_for(5); + ThisThread::sleep_for(5ms); } } @@ -392,7 +394,7 @@ bool ESP32::reset(void) #endif } - ThisThread::sleep_for(5); + ThisThread::sleep_for(5ms); uint8_t wk_ver[4+1]; /* It needs 1 byte extra. */ @@ -654,7 +656,7 @@ int ESP32::scan(WiFiAccessPoint *res, unsigned limit) _smutex.lock(); _startup_wifi(); _smutex.unlock(); - ThisThread::sleep_for(1500); + ThisThread::sleep_for(1500ms); } _smutex.lock(); @@ -1109,6 +1111,14 @@ int8_t ESP32::get_wifi_status() const return _wifi_status; } +void ESP32::flush() +{ + _smutex.lock(); + _parser.flush(); + _smutex.unlock(); +} + + #if defined(TARGET_ESP32AT_BLE) bool ESP32::ble_set_role(int role) { diff --git a/ESP32/ESP32.h b/ESP32/ESP32.h index 5618235..2181bab 100644 --- a/ESP32/ESP32.h +++ b/ESP32/ESP32.h @@ -246,6 +246,15 @@ class ESP32 */ int8_t get_wifi_status() const; + /** + * 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; @@ -352,11 +361,11 @@ class ESP32 typedef struct { uint16_t adv_int_min; /**< minimum value of advertising interval; range: 0x0020 ~ 0x4000 */ uint16_t adv_int_max; /**< maximum value of advertising interval; range: 0x0020 ~ 0x4000 */ - uint8_t adv_type; /**< 0:ADV_TYPE_IND, 2:ADV_TYPE_SCAN_IND, 3:ADV_TYPE_NONCONN_IND */ - uint8_t own_addr_type; /**< own BLE address type; 0:BLE_ADDR_TYPE_PUBLIC, 1:BLE_ADDR_TYPE_RANDOM */ + uint8_t adv_type; /**< 0:FADV_TYPE_IND, 2:FADV_TYPE_SCAN_IND, 3:FADV_TYPE_NONCONN_IND */ + uint8_t own_addr_type; /**< own BLE address type; 0:FBLE_ADDR_TYPE_PUBLIC, 1:FBLE_ADDR_TYPE_RANDOM */ uint8_t channel_map; /**< channel of advertising; ADV_CHNL_~ */ uint8_t adv_filter_policy; /**< filter policy of advertising; ADV_FILTER_ALLOW_SCAN_~ */ - uint8_t peer_addr_type; /**< remote BLE address type; 0:PUBLIC, 1:RANDOM */ + uint8_t peer_addr_type; /**< remote BLE address type; 0:FPUBLIC, 1:FRANDOM */ uint8_t peer_addr[6]; /**< remote BLE address */ } advertising_param_t; diff --git a/ESP32Interface.cpp b/ESP32Interface.cpp index b93feb6..f975573 100644 --- a/ESP32Interface.cpp +++ b/ESP32Interface.cpp @@ -21,6 +21,8 @@ ESP32Interface::ESP32Interface() : ESP32Stack(MBED_CONF_ESP32_WIFI_EN, MBED_CONF_ESP32_WIFI_IO0, MBED_CONF_ESP32_WIFI_TX, MBED_CONF_ESP32_WIFI_RX, MBED_CONF_ESP32_WIFI_DEBUG, MBED_CONF_ESP32_WIFI_RTS, MBED_CONF_ESP32_WIFI_CTS, MBED_CONF_ESP32_WIFI_BAUDRATE, 0), + _rst_pin(MBED_CONF_ESP32_WIFI_EN), + _initialized(false), _dhcp(true), _ap_ssid(), _ap_pass(), @@ -38,6 +40,8 @@ ESP32Interface::ESP32Interface() : ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug, PinName rts, PinName cts, int baudrate) : ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate, 0), + _rst_pin(en), + _initialized(false), _dhcp(true), _ap_ssid(), _ap_pass(), @@ -54,6 +58,8 @@ ESP32Interface::ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, ESP32Interface::ESP32Interface(PinName tx, PinName rx, bool debug) : ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400, 0), + _rst_pin(MBED_CONF_ESP32_WIFI_EN), + _initialized(false), _dhcp(true), _ap_ssid(), _ap_pass(), @@ -108,6 +114,8 @@ int ESP32Interface::connect(const char *ssid, const char *pass, nsapi_security_t if (ret != NSAPI_ERROR_OK) { return ret; } + + _init(); return connect(); } @@ -201,6 +209,7 @@ int ESP32Interface::set_channel(uint8_t channel) int ESP32Interface::disconnect() { + _initialized = false; if (_connection_status == NSAPI_STATUS_DISCONNECTED) { return NSAPI_ERROR_NO_CONNECTION; } @@ -263,6 +272,7 @@ int8_t ESP32Interface::get_rssi() int ESP32Interface::scan(WiFiAccessPoint *res, unsigned count) { + _init(); return _esp->scan(res, count); } @@ -311,3 +321,56 @@ WiFiInterface *WiFiInterface::get_default_instance() { #endif +ESP32Interface::~ESP32Interface() +{ + // Power down the modem + _rst_pin.rst_assert(); +} + +ESP32Interface::ResetPin::ResetPin(PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1)) +{ +} + +void ESP32Interface::ResetPin::rst_assert() +{ + if (_rst_pin.is_connected()) { + _rst_pin = 0; + //tr_debug("rst_assert(): HW reset asserted."); + } +} + +void ESP32Interface::ResetPin::rst_deassert() +{ + if (_rst_pin.is_connected()) { + _rst_pin = 1; + //tr_debug("rst_deassert(): HW reset deasserted."); + } +} + +bool ESP32Interface::ResetPin::is_connected() +{ + return _rst_pin.is_connected(); +} + +nsapi_error_t ESP32Interface::_init(void) +{ + if (!_initialized) { + if (_reset() != NSAPI_ERROR_OK) { + return NSAPI_ERROR_DEVICE_ERROR; + } + _initialized = true; + } + return NSAPI_ERROR_OK; +} + +nsapi_error_t ESP32Interface::_reset(void) +{ + if (_rst_pin.is_connected()) { + _rst_pin.rst_assert(); + rtos::ThisThread::sleep_for(2ms); + _esp->flush(); + _rst_pin.rst_deassert(); + } + + return NSAPI_ERROR_OK; +} diff --git a/ESP32Interface.h b/ESP32Interface.h index 74d5f2c..d5909ce 100644 --- a/ESP32Interface.h +++ b/ESP32Interface.h @@ -44,6 +44,11 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC, int baudrate = 230400); + /** + * @brief ESP32Interface default destructor + */ + virtual ~ESP32Interface(); + /** ESP32Interface lifetime * @param tx TX pin * @param rx RX pin @@ -241,6 +246,19 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface } private: + + // HW reset pin + class ResetPin { + public: + ResetPin(PinName rst_pin); + void rst_assert(); + void rst_deassert(); + bool is_connected(); + private: + mbed::DigitalOut _rst_pin; + } _rst_pin; + + int _initialized; bool _dhcp; char _ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */ char _ap_pass[64]; /* The longest allowed passphrase */ @@ -249,10 +267,12 @@ class ESP32Interface : public ESP32Stack, public WiFiInterface SocketAddress _netmask; SocketAddress _gateway; nsapi_connection_status_t _connection_status; - Callback _connection_status_cb; + mbed::Callback _connection_status_cb; void set_connection_status(nsapi_connection_status_t connection_status); void wifi_status_cb(int8_t wifi_status); + nsapi_error_t _init(void); + nsapi_error_t _reset(void); }; #endif From e7d1d7ced1b17b838976802fad73261149ad3209 Mon Sep 17 00:00:00 2001 From: toyowata Date: Fri, 26 Jun 2020 10:16:11 +0900 Subject: [PATCH 2/3] Add old version API support --- ESP32/ESP32.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/ESP32/ESP32.h b/ESP32/ESP32.h index 2181bab..be1d4e2 100644 --- a/ESP32/ESP32.h +++ b/ESP32/ESP32.h @@ -24,16 +24,22 @@ #include #include "drivers/DigitalOut.h" #include "drivers/SerialBase.h" -#include "drivers/BufferedSerial.h" #include "features/netsocket/nsapi_types.h" #include "features/netsocket/WiFiAccessPoint.h" #include "PinNames.h" #include "platform/ATCmdParser.h" #include "platform/Callback.h" #include "platform/mbed_error.h" +#include "platform/mbed_version.h" #include "rtos/Mutex.h" #include "rtos/ThisThread.h" +#if (MBED_MAJOR_VERSION >= 6) +#include "drivers/BufferedSerial.h" +#else +#include "drivers/UARTSerial.h" +#endif + #ifndef ESP32_CONNECT_TIMEOUT #define ESP32_CONNECT_TIMEOUT 15000 #endif @@ -269,7 +275,11 @@ class ESP32 mbed::DigitalOut * _p_wifi_io0; bool _init_end_common; bool _init_end_wifi; +#if (MBED_MAJOR_VERSION >= 6) mbed::BufferedSerial _serial; +#else + mbed::UARTSerial _serial; +#endif mbed::ATCmdParser _parser; struct packet { struct packet *next; From dde4c11b2a6ef44505d3ac20dc56d35e8d5240ff Mon Sep 17 00:00:00 2001 From: toyowata Date: Fri, 10 Jul 2020 11:30:30 +0900 Subject: [PATCH 3/3] Fix version reference --- ESP32/ESP32.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/ESP32/ESP32.h b/ESP32/ESP32.h index be1d4e2..2f6d971 100644 --- a/ESP32/ESP32.h +++ b/ESP32/ESP32.h @@ -34,10 +34,10 @@ #include "rtos/Mutex.h" #include "rtos/ThisThread.h" -#if (MBED_MAJOR_VERSION >= 6) -#include "drivers/BufferedSerial.h" -#else +#if (MBED_MAJOR_VERSION < 6) #include "drivers/UARTSerial.h" +#else +#include "drivers/BufferedSerial.h" #endif #ifndef ESP32_CONNECT_TIMEOUT @@ -275,10 +275,10 @@ class ESP32 mbed::DigitalOut * _p_wifi_io0; bool _init_end_common; bool _init_end_wifi; -#if (MBED_MAJOR_VERSION >= 6) - mbed::BufferedSerial _serial; -#else +#if (MBED_MAJOR_VERSION < 6) mbed::UARTSerial _serial; +#else + mbed::BufferedSerial _serial; #endif mbed::ATCmdParser _parser; struct packet {