4646#define MBED_CONF_ESP8266_RST NC
4747#endif
4848
49+ #ifndef MBED_CONF_ESP8266_PWR
50+ #define MBED_CONF_ESP8266_PWR NC
51+ #endif
52+
4953#define TRACE_GROUP " ESPI" // ESP8266 Interface
5054
5155using namespace mbed ;
@@ -55,6 +59,7 @@ using namespace rtos;
5559ESP8266Interface::ESP8266Interface ()
5660 : _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS),
5761 _rst_pin(MBED_CONF_ESP8266_RST), // Notice that Pin7 CH_EN cannot be left floating if used as reset
62+ _pwr_pin(MBED_CONF_ESP8266_PWR),
5863 _ap_sec(NSAPI_SECURITY_UNKNOWN),
5964 _if_blocking(true ),
6065 _if_connected(_cmutex),
@@ -87,9 +92,10 @@ ESP8266Interface::ESP8266Interface()
8792#endif
8893
8994// ESP8266Interface implementation
90- ESP8266Interface::ESP8266Interface (PinName tx, PinName rx, bool debug, PinName rts, PinName cts, PinName rst)
95+ ESP8266Interface::ESP8266Interface (PinName tx, PinName rx, bool debug, PinName rts, PinName cts, PinName rst, PinName pwr )
9196 : _esp(tx, rx, debug, rts, cts),
9297 _rst_pin(rst),
98+ _pwr_pin(pwr),
9399 _ap_sec(NSAPI_SECURITY_UNKNOWN),
94100 _if_blocking(true ),
95101 _if_connected(_cmutex),
@@ -134,6 +140,8 @@ ESP8266Interface::~ESP8266Interface()
134140
135141 // Power down the modem
136142 _rst_pin.rst_assert ();
143+ // Power off the modem
144+ _pwr_pin.power_off ();
137145}
138146
139147ESP8266Interface::ResetPin::ResetPin (PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1 ))
@@ -162,6 +170,33 @@ bool ESP8266Interface::ResetPin::is_connected()
162170 return _rst_pin.is_connected ();
163171}
164172
173+ ESP8266Interface::PowerPin::PowerPin (PinName pwr_pin) : _pwr_pin(mbed::DigitalOut(pwr_pin, !MBED_CONF_ESP8266_POWER_ON_POLARITY))
174+ {
175+ }
176+
177+ void ESP8266Interface::PowerPin::power_on ()
178+ {
179+ if (_pwr_pin.is_connected ()) {
180+ _pwr_pin = MBED_CONF_ESP8266_POWER_ON_POLARITY;
181+ tr_debug (" HW power-on" );
182+ ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_ON_TIME_MS);
183+ }
184+ }
185+
186+ void ESP8266Interface::PowerPin::power_off ()
187+ {
188+ if (_pwr_pin.is_connected ()) {
189+ _pwr_pin = !MBED_CONF_ESP8266_POWER_ON_POLARITY;
190+ tr_debug (" HW power-off" );
191+ ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_OFF_TIME_MS);
192+ }
193+ }
194+
195+ bool ESP8266Interface::PowerPin::is_connected ()
196+ {
197+ return _pwr_pin.is_connected ();
198+ }
199+
165200int ESP8266Interface::connect (const char *ssid, const char *pass, nsapi_security_t security,
166201 uint8_t channel)
167202{
@@ -347,6 +382,8 @@ int ESP8266Interface::disconnect()
347382
348383 // Power down the modem
349384 _rst_pin.rst_assert ();
385+ // Power off the modem
386+ _pwr_pin.power_off ();
350387
351388 return ret;
352389}
@@ -425,6 +462,8 @@ bool ESP8266Interface::_get_firmware_ok()
425462nsapi_error_t ESP8266Interface::_init (void )
426463{
427464 if (!_initialized) {
465+ _pwr_pin.power_off ();
466+ _pwr_pin.power_on ();
428467 if (_reset () != NSAPI_ERROR_OK) {
429468 return NSAPI_ERROR_DEVICE_ERROR;
430469 }
0 commit comments