diff --git a/components/wifi/esp8266-driver/ESP8266Interface.cpp b/components/wifi/esp8266-driver/ESP8266Interface.cpp index c9f8f3318d..308de3c979 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.cpp +++ b/components/wifi/esp8266-driver/ESP8266Interface.cpp @@ -46,6 +46,10 @@ #define MBED_CONF_ESP8266_RST NC #endif +#ifndef MBED_CONF_ESP8266_PWR +#define MBED_CONF_ESP8266_PWR NC +#endif + #define TRACE_GROUP "ESPI" // ESP8266 Interface using namespace mbed; @@ -55,6 +59,7 @@ using namespace rtos; ESP8266Interface::ESP8266Interface() : _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS), _rst_pin(MBED_CONF_ESP8266_RST), // Notice that Pin7 CH_EN cannot be left floating if used as reset + _pwr_pin(MBED_CONF_ESP8266_PWR), _ap_sec(NSAPI_SECURITY_UNKNOWN), _if_blocking(true), _if_connected(_cmutex), @@ -87,9 +92,10 @@ ESP8266Interface::ESP8266Interface() #endif // ESP8266Interface implementation -ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName rts, PinName cts, PinName rst) +ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName rts, PinName cts, PinName rst, PinName pwr) : _esp(tx, rx, debug, rts, cts), _rst_pin(rst), + _pwr_pin(pwr), _ap_sec(NSAPI_SECURITY_UNKNOWN), _if_blocking(true), _if_connected(_cmutex), @@ -134,6 +140,8 @@ ESP8266Interface::~ESP8266Interface() // Power down the modem _rst_pin.rst_assert(); + // Power off the modem + _pwr_pin.power_off(); } ESP8266Interface::ResetPin::ResetPin(PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1)) @@ -162,6 +170,33 @@ bool ESP8266Interface::ResetPin::is_connected() return _rst_pin.is_connected(); } +ESP8266Interface::PowerPin::PowerPin(PinName pwr_pin) : _pwr_pin(mbed::DigitalOut(pwr_pin, !MBED_CONF_ESP8266_POWER_ON_POLARITY)) +{ +} + +void ESP8266Interface::PowerPin::power_on() +{ + if (_pwr_pin.is_connected()) { + _pwr_pin = MBED_CONF_ESP8266_POWER_ON_POLARITY; + tr_debug("HW power-on"); + ThisThread::sleep_for(MBED_CONF_ESP8266_POWER_ON_TIME_MS); + } +} + +void ESP8266Interface::PowerPin::power_off() +{ + if (_pwr_pin.is_connected()) { + _pwr_pin = !MBED_CONF_ESP8266_POWER_ON_POLARITY; + tr_debug("HW power-off"); + ThisThread::sleep_for(MBED_CONF_ESP8266_POWER_OFF_TIME_MS); + } +} + +bool ESP8266Interface::PowerPin::is_connected() +{ + return _pwr_pin.is_connected(); +} + int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security, uint8_t channel) { @@ -347,6 +382,8 @@ int ESP8266Interface::disconnect() // Power down the modem _rst_pin.rst_assert(); + // Power off the modem + _pwr_pin.power_off(); return ret; } @@ -425,6 +462,8 @@ bool ESP8266Interface::_get_firmware_ok() nsapi_error_t ESP8266Interface::_init(void) { if (!_initialized) { + _pwr_pin.power_off(); + _pwr_pin.power_on(); if (_reset() != NSAPI_ERROR_OK) { return NSAPI_ERROR_DEVICE_ERROR; } diff --git a/components/wifi/esp8266-driver/ESP8266Interface.h b/components/wifi/esp8266-driver/ESP8266Interface.h index 1f8dfb11c3..f924cd59e1 100644 --- a/components/wifi/esp8266-driver/ESP8266Interface.h +++ b/components/wifi/esp8266-driver/ESP8266Interface.h @@ -78,7 +78,7 @@ public: * @param rx RX pin * @param debug Enable debugging */ - ESP8266Interface(PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC, PinName rst = NC); + ESP8266Interface(PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC, PinName rst = NC, PinName pwr = NC); /** * @brief ESP8266Interface default destructor @@ -392,6 +392,16 @@ private: mbed::DigitalOut _rst_pin; } _rst_pin; + // HW power pin + class PowerPin { + public: + PowerPin(PinName pwr_pin); + void power_on(); + void power_off(); + bool is_connected(); + private: + mbed::DigitalOut _pwr_pin; + } _pwr_pin; // Credentials static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */ diff --git a/components/wifi/esp8266-driver/mbed_lib.json b/components/wifi/esp8266-driver/mbed_lib.json index 60d3aac700..f2877559a5 100644 --- a/components/wifi/esp8266-driver/mbed_lib.json +++ b/components/wifi/esp8266-driver/mbed_lib.json @@ -25,6 +25,23 @@ "help": "RESET pin for the modem, defaults to Not Connected", "value": null }, + "pwr": { + "help": "POWER pin for the modem, defaults to Not Connected", + "value": null + }, + "power-on-polarity": { + "help": "Polarity of power-on for the modem. 0 means 0/1 for power on/off; 1 means 1/0 for power on/off.", + "options": [0, 1], + "value": 0 + }, + "power-on-time-ms": { + "help": "Delay after powering on the modem in ms", + "value": 3 + }, + "power-off-time-ms": { + "help": "Delay after powering off the modem in ms", + "value": 3 + }, "debug": { "help": "Enable debug logs. [true/false]", "value": false