Skip to content

Commit 98c0fd0

Browse files
authored
Merge pull request #11343 from OpenNuvoton/nuvoton_esp8266_power_pin
ESP8266: Support power pin in custom wiring
2 parents 4531229 + 650e2e5 commit 98c0fd0

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
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

5155
using namespace mbed;
@@ -55,6 +59,7 @@ using namespace rtos;
5559
ESP8266Interface::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

139147
ESP8266Interface::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+
165200
int 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()
425462
nsapi_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
}

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
7878
* @param rx RX pin
7979
* @param debug Enable debugging
8080
*/
81-
ESP8266Interface(PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC, PinName rst = NC);
81+
ESP8266Interface(PinName tx, PinName rx, bool debug = false, PinName rts = NC, PinName cts = NC, PinName rst = NC, PinName pwr = NC);
8282

8383
/**
8484
* @brief ESP8266Interface default destructor
@@ -392,6 +392,16 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
392392
mbed::DigitalOut _rst_pin;
393393
} _rst_pin;
394394

395+
// HW power pin
396+
class PowerPin {
397+
public:
398+
PowerPin(PinName pwr_pin);
399+
void power_on();
400+
void power_off();
401+
bool is_connected();
402+
private:
403+
mbed::DigitalOut _pwr_pin;
404+
} _pwr_pin;
395405

396406
// Credentials
397407
static const int ESP8266_SSID_MAX_LENGTH = 32; /* 32 is what 802.11 defines as longest possible name */

components/wifi/esp8266-driver/mbed_lib.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@
2525
"help": "RESET pin for the modem, defaults to Not Connected",
2626
"value": null
2727
},
28+
"pwr": {
29+
"help": "POWER pin for the modem, defaults to Not Connected",
30+
"value": null
31+
},
32+
"power-on-polarity": {
33+
"help": "Polarity of power-on for the modem. 0 means 0/1 for power on/off; 1 means 1/0 for power on/off.",
34+
"options": [0, 1],
35+
"value": 0
36+
},
37+
"power-on-time-ms": {
38+
"help": "Delay after powering on the modem in ms",
39+
"value": 3
40+
},
41+
"power-off-time-ms": {
42+
"help": "Delay after powering off the modem in ms",
43+
"value": 3
44+
},
2845
"debug": {
2946
"help": "Enable debug logs. [true/false]",
3047
"value": false

0 commit comments

Comments
 (0)