3131#include " platform/mbed_debug.h"
3232#include " rtos/ThisThread.h"
3333
34+ using namespace std ::chrono;
35+
3436#ifndef MBED_CONF_ESP8266_DEBUG
3537#define MBED_CONF_ESP8266_DEBUG false
3638#endif
@@ -197,7 +199,7 @@ void ESP8266Interface::PowerPin::power_on()
197199 if (_pwr_pin.is_connected ()) {
198200 _pwr_pin = MBED_CONF_ESP8266_POWER_ON_POLARITY;
199201 tr_debug (" power_on(): HW power-on." );
200- ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_ON_TIME_MS);
202+ ThisThread::sleep_for (milliseconds ( MBED_CONF_ESP8266_POWER_ON_TIME_MS) );
201203 }
202204}
203205
@@ -206,7 +208,7 @@ void ESP8266Interface::PowerPin::power_off()
206208 if (_pwr_pin.is_connected ()) {
207209 _pwr_pin = !MBED_CONF_ESP8266_POWER_ON_POLARITY;
208210 tr_debug (" power_off(): HW power-off." );
209- ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_OFF_TIME_MS);
211+ ThisThread::sleep_for (milliseconds ( MBED_CONF_ESP8266_POWER_OFF_TIME_MS) );
210212 }
211213}
212214
@@ -261,14 +263,14 @@ void ESP8266Interface::_connect_async()
261263 return ;
262264 }
263265 _connect_retval = _esp.connect (ap_ssid, ap_pass);
264- int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms ();
266+ auto timepassed = _conn_timer.elapsed_time ();
265267 if (_connect_retval == NSAPI_ERROR_OK
266268 || _connect_retval == NSAPI_ERROR_AUTH_FAILURE
267269 || _connect_retval == NSAPI_ERROR_NO_SSID
268- || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
270+ || ((_if_blocking == true ) && (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT ))) {
269271 _connect_event_id = 0 ;
270272 _conn_timer.stop ();
271- if (timeleft_ms <= 0 && _connect_retval != NSAPI_ERROR_OK) {
273+ if (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT && _connect_retval != NSAPI_ERROR_OK) {
272274 _connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
273275 }
274276 if (_connect_retval != NSAPI_ERROR_OK) {
@@ -280,7 +282,7 @@ void ESP8266Interface::_connect_async()
280282#endif
281283 } else {
282284 // Postpone to give other stuff time to run
283- _connect_event_id = _global_event_queue->call_in (ESP8266_INTERFACE_CONNECT_INTERVAL_MS ,
285+ _connect_event_id = _global_event_queue->call_in (ESP8266_INTERFACE_CONNECT_INTERVAL ,
284286 callback (this , &ESP8266Interface::_connect_async));
285287 if (!_connect_event_id) {
286288 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
@@ -442,11 +444,11 @@ void ESP8266Interface::_disconnect_async()
442444{
443445 _cmutex.lock ();
444446 _disconnect_retval = _esp.disconnect () ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
445- int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms ();
447+ auto timepassed = _conn_timer.elapsed_time ();
446448
447- if (_disconnect_retval == NSAPI_ERROR_OK || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
449+ if (_disconnect_retval == NSAPI_ERROR_OK || ((_if_blocking == true ) && (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT ))) {
448450
449- if (timeleft_ms <= 0 && _connect_retval != NSAPI_ERROR_OK) {
451+ if (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT && _connect_retval != NSAPI_ERROR_OK) {
450452 _disconnect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
451453 } else {
452454 if (_conn_stat != NSAPI_STATUS_DISCONNECTED) {
@@ -467,7 +469,7 @@ void ESP8266Interface::_disconnect_async()
467469 } else {
468470 // Postpone to give other stuff time to run
469471 _disconnect_event_id = _global_event_queue->call_in (
470- ESP8266_INTERFACE_CONNECT_INTERVAL_MS ,
472+ ESP8266_INTERFACE_CONNECT_INTERVAL ,
471473 callback (this , &ESP8266Interface::_disconnect_async));
472474 if (!_disconnect_event_id) {
473475 MBED_ERROR (
@@ -643,10 +645,10 @@ int8_t ESP8266Interface::get_rssi()
643645
644646int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count)
645647{
646- return scan (res, count, SCANMODE_ACTIVE, 0 , 0 );
648+ return scan (res, count, SCANMODE_ACTIVE);
647649}
648650
649- int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count, scan_mode mode, unsigned t_max, unsigned t_min)
651+ int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count, scan_mode mode, mbed::chrono::milliseconds_u32 t_max, mbed::chrono::milliseconds_u32 t_min)
650652{
651653 if (t_max > ESP8266_SCAN_TIME_MAX) {
652654 return NSAPI_ERROR_PARAMETER;
@@ -766,7 +768,15 @@ nsapi_error_t ESP8266Interface::_reset()
766768 _rst_pin.rst_assert ();
767769 // If you happen to use Pin7 CH_EN as reset pin, not needed otherwise
768770 // https://www.espressif.com/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf
769- ThisThread::sleep_for (2 ); // Documentation says 200 us; need 2 ticks to get minimum 1 ms.
771+ // First need to round up when converting to kernel ticks (eg 200us -> 1ms).
772+ auto delay = duration_cast<Kernel::Clock::duration_u32>(200us);
773+ if (delay < 200us) {
774+ delay++;
775+ }
776+ // Then need to round the clock-resolution duration up; if we were at the end of a tick
777+ // period, it might flip immediately.
778+ delay++;
779+ ThisThread::sleep_for (delay);
770780 _esp.flush ();
771781 _rst_pin.rst_deassert ();
772782 } else {
@@ -929,7 +939,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
929939 && socket->proto == NSAPI_TCP
930940 && core_util_atomic_cas_u8 (&_cbs[socket->id ].deferred , &expect_false, true )) {
931941 tr_debug (" socket_send(...): Postponing SIGIO from the device." );
932- if (!_global_event_queue->call_in (50 , callback (this , &ESP8266Interface::event_deferred))) {
942+ if (!_global_event_queue->call_in (50ms , callback (this , &ESP8266Interface::event_deferred))) {
933943 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
934944 " socket_send(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
935945 }
@@ -1086,7 +1096,7 @@ void ESP8266Interface::event()
10861096{
10871097 if (!_oob_event_id) {
10881098 // Throttles event creation by using arbitrary small delay
1089- _oob_event_id = _global_event_queue->call_in (50 , callback (this , &ESP8266Interface::proc_oob_evnt));
1099+ _oob_event_id = _global_event_queue->call_in (50ms , callback (this , &ESP8266Interface::proc_oob_evnt));
10901100 if (!_oob_event_id) {
10911101 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
10921102 " ESP8266Interface::event(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
0 commit comments