ESP8266: Support power on/off in custom wiring

In custom wiring, there can be a power pin to power on/off the modem. This
commit supports it and allows for the following configurations:

- power pin name
- power pin polarity
- power on/off delay time
pull/11343/head
Chun-Chieh Li 2019-08-27 15:23:10 +08:00
parent 421ad37433
commit 650e2e593c
3 changed files with 68 additions and 2 deletions

View File

@ -46,6 +46,10 @@
#define MBED_CONF_ESP8266_RST NC #define MBED_CONF_ESP8266_RST NC
#endif #endif
#ifndef MBED_CONF_ESP8266_PWR
#define MBED_CONF_ESP8266_PWR NC
#endif
#define TRACE_GROUP "ESPI" // ESP8266 Interface #define TRACE_GROUP "ESPI" // ESP8266 Interface
using namespace mbed; using namespace mbed;
@ -55,6 +59,7 @@ using namespace rtos;
ESP8266Interface::ESP8266Interface() ESP8266Interface::ESP8266Interface()
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS), : _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 _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), _ap_sec(NSAPI_SECURITY_UNKNOWN),
_if_blocking(true), _if_blocking(true),
_if_connected(_cmutex), _if_connected(_cmutex),
@ -87,9 +92,10 @@ ESP8266Interface::ESP8266Interface()
#endif #endif
// ESP8266Interface implementation // 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), : _esp(tx, rx, debug, rts, cts),
_rst_pin(rst), _rst_pin(rst),
_pwr_pin(pwr),
_ap_sec(NSAPI_SECURITY_UNKNOWN), _ap_sec(NSAPI_SECURITY_UNKNOWN),
_if_blocking(true), _if_blocking(true),
_if_connected(_cmutex), _if_connected(_cmutex),
@ -134,6 +140,8 @@ ESP8266Interface::~ESP8266Interface()
// Power down the modem // Power down the modem
_rst_pin.rst_assert(); _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)) 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(); 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, int ESP8266Interface::connect(const char *ssid, const char *pass, nsapi_security_t security,
uint8_t channel) uint8_t channel)
{ {
@ -347,6 +382,8 @@ int ESP8266Interface::disconnect()
// Power down the modem // Power down the modem
_rst_pin.rst_assert(); _rst_pin.rst_assert();
// Power off the modem
_pwr_pin.power_off();
return ret; return ret;
} }
@ -425,6 +462,8 @@ bool ESP8266Interface::_get_firmware_ok()
nsapi_error_t ESP8266Interface::_init(void) nsapi_error_t ESP8266Interface::_init(void)
{ {
if (!_initialized) { if (!_initialized) {
_pwr_pin.power_off();
_pwr_pin.power_on();
if (_reset() != NSAPI_ERROR_OK) { if (_reset() != NSAPI_ERROR_OK) {
return NSAPI_ERROR_DEVICE_ERROR; return NSAPI_ERROR_DEVICE_ERROR;
} }

View File

@ -78,7 +78,7 @@ public:
* @param rx RX pin * @param rx RX pin
* @param debug Enable debugging * @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 * @brief ESP8266Interface default destructor
@ -392,6 +392,16 @@ private:
mbed::DigitalOut _rst_pin; mbed::DigitalOut _rst_pin;
} _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 // Credentials
static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */ static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */

View File

@ -21,6 +21,23 @@
"help": "RESET pin for the modem, defaults to Not Connected", "help": "RESET pin for the modem, defaults to Not Connected",
"value": null "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": { "debug": {
"help": "Enable debug logs. [true/false]", "help": "Enable debug logs. [true/false]",
"value": false "value": false