46
46
#define MBED_CONF_ESP8266_RST NC
47
47
#endif
48
48
49
+ #ifndef MBED_CONF_ESP8266_PWR
50
+ #define MBED_CONF_ESP8266_PWR NC
51
+ #endif
52
+
49
53
#define TRACE_GROUP " ESPI" // ESP8266 Interface
50
54
51
55
using namespace mbed ;
@@ -55,6 +59,7 @@ using namespace rtos;
55
59
ESP8266Interface::ESP8266Interface ()
56
60
: _esp(MBED_CONF_ESP8266_TX, MBED_CONF_ESP8266_RX, MBED_CONF_ESP8266_DEBUG, MBED_CONF_ESP8266_RTS, MBED_CONF_ESP8266_CTS),
57
61
_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),
58
63
_ap_sec(NSAPI_SECURITY_UNKNOWN),
59
64
_if_blocking(true ),
60
65
_if_connected(_cmutex),
@@ -87,9 +92,10 @@ ESP8266Interface::ESP8266Interface()
87
92
#endif
88
93
89
94
// 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 )
91
96
: _esp(tx, rx, debug, rts, cts),
92
97
_rst_pin(rst),
98
+ _pwr_pin(pwr),
93
99
_ap_sec(NSAPI_SECURITY_UNKNOWN),
94
100
_if_blocking(true ),
95
101
_if_connected(_cmutex),
@@ -134,6 +140,8 @@ ESP8266Interface::~ESP8266Interface()
134
140
135
141
// Power down the modem
136
142
_rst_pin.rst_assert ();
143
+ // Power off the modem
144
+ _pwr_pin.power_off ();
137
145
}
138
146
139
147
ESP8266Interface::ResetPin::ResetPin (PinName rst_pin) : _rst_pin(mbed::DigitalOut(rst_pin, 1 ))
@@ -162,6 +170,33 @@ bool ESP8266Interface::ResetPin::is_connected()
162
170
return _rst_pin.is_connected ();
163
171
}
164
172
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
+
165
200
int ESP8266Interface::connect (const char *ssid, const char *pass, nsapi_security_t security,
166
201
uint8_t channel)
167
202
{
@@ -347,6 +382,8 @@ int ESP8266Interface::disconnect()
347
382
348
383
// Power down the modem
349
384
_rst_pin.rst_assert ();
385
+ // Power off the modem
386
+ _pwr_pin.power_off ();
350
387
351
388
return ret;
352
389
}
@@ -425,6 +462,8 @@ bool ESP8266Interface::_get_firmware_ok()
425
462
nsapi_error_t ESP8266Interface::_init (void )
426
463
{
427
464
if (!_initialized) {
465
+ _pwr_pin.power_off ();
466
+ _pwr_pin.power_on ();
428
467
if (_reset () != NSAPI_ERROR_OK) {
429
468
return NSAPI_ERROR_DEVICE_ERROR;
430
469
}
0 commit comments