Skip to content

Commit 372a3f1

Browse files
authored
Merge pull request #11514 from dmaziec1/UART_deep_sleep_enable
ESP8266 unlocks deep sleep when disconnected
2 parents e39ba11 + 1aa3b5d commit 372a3f1

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,4 +1275,9 @@ bool ESP8266::set_country_code_policy(bool track_ap, const char *country_code, i
12751275
return done;
12761276
}
12771277

1278+
int ESP8266::uart_enable_input(bool enabled)
1279+
{
1280+
return _serial.enable_input(enabled);
1281+
}
1282+
12781283
#endif

components/wifi/esp8266-driver/ESP8266/ESP8266.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,14 @@ class ESP8266 {
405405
static const int8_t WIFIMODE_STATION_SOFTAP = 3;
406406
static const int8_t SOCKET_COUNT = 5;
407407

408+
/**
409+
* Enables or disables uart input and deep sleep
410+
*
411+
* @param lock if TRUE, uart input is enabled and deep sleep is locked
412+
* if FALSE, uart input is disabled and deep sleep is unlocked
413+
*/
414+
int uart_enable_input(bool lock);
415+
408416
private:
409417
// FW version
410418
struct fw_sdk_version _sdk_v;

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ ESP8266Interface::ESP8266Interface()
9191
_sock_i[i].open = false;
9292
_sock_i[i].sport = 0;
9393
}
94+
_esp.uart_enable_input(false);
9495
}
9596
#endif
9697

@@ -127,6 +128,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
127128
_sock_i[i].open = false;
128129
_sock_i[i].sport = 0;
129130
}
131+
_esp.uart_enable_input(false);
130132
}
131133

132134
ESP8266Interface::~ESP8266Interface()
@@ -226,13 +228,15 @@ void ESP8266Interface::_connect_async()
226228
nsapi_error_t status = _init();
227229
if (status != NSAPI_ERROR_OK) {
228230
_connect_retval = status;
231+
_esp.uart_enable_input(false);
229232
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
230233
//_conn_stat_cb will be called from refresh_conn_state_cb
231234
return;
232235
}
233236

234237
if (!_esp.dhcp(true, 1)) {
235238
_connect_retval = NSAPI_ERROR_DHCP_FAILURE;
239+
_esp.uart_enable_input(false);
236240
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
237241
//_conn_stat_cb will be called from refresh_conn_state_cb
238242
return;
@@ -255,6 +259,7 @@ void ESP8266Interface::_connect_async()
255259
_connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
256260
}
257261
if (_connect_retval != NSAPI_ERROR_OK) {
262+
_esp.uart_enable_input(false);
258263
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
259264
}
260265
_if_connected.notify_all();
@@ -306,6 +311,7 @@ int ESP8266Interface::connect()
306311
_cmutex.lock();
307312
}
308313
_software_conn_stat = IFACE_STATUS_CONNECTING;
314+
_esp.uart_enable_input(true);
309315
_connect_retval = NSAPI_ERROR_NO_CONNECTION;
310316
MBED_ASSERT(!_connect_event_id);
311317
_conn_timer.stop();
@@ -406,6 +412,7 @@ void ESP8266Interface::_disconnect_async()
406412
}
407413

408414
_power_off();
415+
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
409416
_if_connected.notify_all();
410417

411418
} else {
@@ -420,8 +427,8 @@ void ESP8266Interface::_disconnect_async()
420427
}
421428
}
422429
_cmutex.unlock();
423-
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
424430

431+
_esp.uart_enable_input(false);
425432
if (_disconnect_event_id == 0) {
426433
if (_conn_stat_cb) {
427434
_conn_stat_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat);
@@ -483,17 +490,31 @@ int ESP8266Interface::disconnect()
483490

484491
const char *ESP8266Interface::get_ip_address()
485492
{
493+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
494+
_esp.uart_enable_input(true);
495+
}
496+
486497
const char *ip_buff = _esp.ip_addr();
487498
if (!ip_buff || strcmp(ip_buff, "0.0.0.0") == 0) {
488-
return NULL;
499+
ip_buff = NULL;
500+
}
501+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
502+
_esp.uart_enable_input(false);
489503
}
490-
491504
return ip_buff;
492505
}
493506

494507
const char *ESP8266Interface::get_mac_address()
495508
{
496-
return _esp.mac_addr();
509+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
510+
_esp.uart_enable_input(true);
511+
}
512+
const char *ret = _esp.mac_addr();
513+
514+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
515+
_esp.uart_enable_input(false);
516+
}
517+
return ret;
497518
}
498519

499520
const char *ESP8266Interface::get_gateway()
@@ -514,7 +535,17 @@ char *ESP8266Interface::get_interface_name(char *interface_name)
514535

515536
int8_t ESP8266Interface::get_rssi()
516537
{
517-
return _esp.rssi();
538+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
539+
_esp.uart_enable_input(true);
540+
}
541+
542+
int8_t ret = _esp.rssi();
543+
544+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
545+
_esp.uart_enable_input(false);
546+
}
547+
548+
return ret;
518549
}
519550

520551
int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
@@ -531,13 +562,25 @@ int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count, scan_mode mode,
531562
return NSAPI_ERROR_PARAMETER;
532563
}
533564

565+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
566+
_esp.uart_enable_input(true);
567+
}
568+
534569
nsapi_error_t status = _init();
535570
if (status != NSAPI_ERROR_OK) {
536571
return status;
572+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
573+
_esp.uart_enable_input(false);
574+
}
537575
}
538576

539-
return _esp.scan(res, count, (mode == SCANMODE_ACTIVE ? ESP8266::SCANMODE_ACTIVE : ESP8266::SCANMODE_PASSIVE),
540-
t_max, t_min);
577+
int ret = _esp.scan(res, count, (mode == SCANMODE_ACTIVE ? ESP8266::SCANMODE_ACTIVE : ESP8266::SCANMODE_PASSIVE),
578+
t_max, t_min);
579+
580+
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
581+
_esp.uart_enable_input(false);
582+
}
583+
return ret;
541584
}
542585

543586
bool ESP8266Interface::_get_firmware_ok()
@@ -563,6 +606,7 @@ nsapi_error_t ESP8266Interface::_init(void)
563606
if (!_initialized) {
564607
_pwr_pin.power_off();
565608
_pwr_pin.power_on();
609+
566610
if (_reset() != NSAPI_ERROR_OK) {
567611
return NSAPI_ERROR_DEVICE_ERROR;
568612
}

0 commit comments

Comments
 (0)