Skip to content

Commit

Permalink
UART in deep sleep mode when it is disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominika Maziec committed Sep 18, 2019
1 parent f51bbe0 commit 630eb95
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
5 changes: 5 additions & 0 deletions components/wifi/esp8266-driver/ESP8266/ESP8266.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,4 +1275,9 @@ bool ESP8266::set_country_code_policy(bool track_ap, const char *country_code, i
return done;
}

int ESP8266::uart_enable_input(bool enabled)
{
return _serial.enable_input(enabled);
}

#endif
2 changes: 2 additions & 0 deletions components/wifi/esp8266-driver/ESP8266/ESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ class ESP8266 {
static const int8_t WIFIMODE_STATION_SOFTAP = 3;
static const int8_t SOCKET_COUNT = 5;

int uart_enable_input(bool lock);

private:
// FW version
struct fw_sdk_version _sdk_v;
Expand Down
55 changes: 48 additions & 7 deletions components/wifi/esp8266-driver/ESP8266Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ ESP8266Interface::ESP8266Interface()
_sock_i[i].open = false;
_sock_i[i].sport = 0;
}
_esp.uart_enable_input(false);
}
#endif

Expand Down Expand Up @@ -125,6 +126,7 @@ ESP8266Interface::ESP8266Interface(PinName tx, PinName rx, bool debug, PinName r
_sock_i[i].open = false;
_sock_i[i].sport = 0;
}
_esp.uart_enable_input(false);
}

ESP8266Interface::~ESP8266Interface()
Expand Down Expand Up @@ -224,13 +226,15 @@ void ESP8266Interface::_connect_async()
nsapi_error_t status = _init();
if (status != NSAPI_ERROR_OK) {
_connect_retval = status;
_esp.uart_enable_input(false);
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
//_conn_stat_cb will be called from refresh_conn_state_cb
return;
}

if (!_esp.dhcp(true, 1)) {
_connect_retval = NSAPI_ERROR_DHCP_FAILURE;
_esp.uart_enable_input(false);
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
//_conn_stat_cb will be called from refresh_conn_state_cb
return;
Expand All @@ -253,6 +257,7 @@ void ESP8266Interface::_connect_async()
_connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
}
if (_connect_retval != NSAPI_ERROR_OK) {
_esp.uart_enable_input(false);
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
}
_if_connected.notify_all();
Expand Down Expand Up @@ -304,6 +309,7 @@ int ESP8266Interface::connect()
_cmutex.lock();
}
_software_conn_stat = IFACE_STATUS_CONNECTING;
_esp.uart_enable_input(true);
_connect_retval = NSAPI_ERROR_NO_CONNECTION;
MBED_ASSERT(!_connect_event_id);
_conn_timer.stop();
Expand Down Expand Up @@ -404,6 +410,7 @@ void ESP8266Interface::_disconnect_async()
}

_power_off();
_software_conn_stat = IFACE_STATUS_DISCONNECTED;
_if_connected.notify_all();

} else {
Expand All @@ -418,8 +425,8 @@ void ESP8266Interface::_disconnect_async()
}
}
_cmutex.unlock();
_software_conn_stat = IFACE_STATUS_DISCONNECTED;

_esp.uart_enable_input(false);
if (_disconnect_event_id == 0) {
if (_conn_stat_cb) {
_conn_stat_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _conn_stat);
Expand Down Expand Up @@ -481,17 +488,31 @@ int ESP8266Interface::disconnect()

const char *ESP8266Interface::get_ip_address()
{
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED ) {
_esp.uart_enable_input(true);
}

const char *ip_buff = _esp.ip_addr();
if (!ip_buff || strcmp(ip_buff, "0.0.0.0") == 0) {
return NULL;
ip_buff = NULL;
}
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
_esp.uart_enable_input(false);
}

return ip_buff;
}

const char *ESP8266Interface::get_mac_address()
{
return _esp.mac_addr();
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED ) {
_esp.uart_enable_input(true);
}
const char *ret = _esp.mac_addr();

if (_software_conn_stat == IFACE_STATUS_DISCONNECTED ) {
_esp.uart_enable_input(false);
}
return ret;
}

const char *ESP8266Interface::get_gateway()
Expand All @@ -506,7 +527,17 @@ const char *ESP8266Interface::get_netmask()

int8_t ESP8266Interface::get_rssi()
{
return _esp.rssi();
if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
_esp.uart_enable_input(true);
}

int8_t ret = _esp.rssi();

if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
_esp.uart_enable_input(false);
}

return ret;
}

int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count)
Expand All @@ -523,13 +554,22 @@ int ESP8266Interface::scan(WiFiAccessPoint *res, unsigned count, scan_mode mode,
return NSAPI_ERROR_PARAMETER;
}

if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
_esp.uart_enable_input(true);
}

nsapi_error_t status = _init();
if (status != NSAPI_ERROR_OK) {
return status;
}

return _esp.scan(res, count, (mode == SCANMODE_ACTIVE ? ESP8266::SCANMODE_ACTIVE : ESP8266::SCANMODE_PASSIVE),
t_max, t_min);
int ret = _esp.scan(res, count, (mode == SCANMODE_ACTIVE ? ESP8266::SCANMODE_ACTIVE : ESP8266::SCANMODE_PASSIVE),
t_max, t_min);

if (_software_conn_stat == IFACE_STATUS_DISCONNECTED) {
_esp.uart_enable_input(false);
}
return ret;
}

bool ESP8266Interface::_get_firmware_ok()
Expand All @@ -555,6 +595,7 @@ nsapi_error_t ESP8266Interface::_init(void)
if (!_initialized) {
_pwr_pin.power_off();
_pwr_pin.power_on();

if (_reset() != NSAPI_ERROR_OK) {
return NSAPI_ERROR_DEVICE_ERROR;
}
Expand Down

0 comments on commit 630eb95

Please sign in to comment.