Skip to content

ESP8266: Support power pin in custom wiring #11343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion components/wifi/esp8266-driver/ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion components/wifi/esp8266-driver/ESP8266Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
* @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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a binary compatibility break, but I guess this isn't a core enough component for that to be an issue.


/**
* @brief ESP8266Interface default destructor
Expand Down Expand Up @@ -392,6 +392,16 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
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 */
Expand Down
17 changes: 17 additions & 0 deletions components/wifi/esp8266-driver/mbed_lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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
Expand Down