3030#include " platform/mbed_debug.h"
3131#include " rtos/ThisThread.h"
3232
33+ using namespace std ::chrono;
34+
3335#ifndef MBED_CONF_ESP8266_DEBUG
3436#define MBED_CONF_ESP8266_DEBUG false
3537#endif
@@ -195,7 +197,7 @@ void ESP8266Interface::PowerPin::power_on()
195197 if (_pwr_pin.is_connected ()) {
196198 _pwr_pin = MBED_CONF_ESP8266_POWER_ON_POLARITY;
197199 tr_debug (" power_on(): HW power-on." );
198- ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_ON_TIME_MS);
200+ ThisThread::sleep_for (milliseconds ( MBED_CONF_ESP8266_POWER_ON_TIME_MS) );
199201 }
200202}
201203
@@ -204,7 +206,7 @@ void ESP8266Interface::PowerPin::power_off()
204206 if (_pwr_pin.is_connected ()) {
205207 _pwr_pin = !MBED_CONF_ESP8266_POWER_ON_POLARITY;
206208 tr_debug (" power_off(): HW power-off." );
207- ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_OFF_TIME_MS);
209+ ThisThread::sleep_for (milliseconds ( MBED_CONF_ESP8266_POWER_OFF_TIME_MS) );
208210 }
209211}
210212
@@ -259,14 +261,14 @@ void ESP8266Interface::_connect_async()
259261 return ;
260262 }
261263 _connect_retval = _esp.connect (ap_ssid, ap_pass);
262- int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms ();
264+ auto timepassed = _conn_timer.read_duration ();
263265 if (_connect_retval == NSAPI_ERROR_OK
264266 || _connect_retval == NSAPI_ERROR_AUTH_FAILURE
265267 || _connect_retval == NSAPI_ERROR_NO_SSID
266- || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
268+ || ((_if_blocking == true ) && (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT ))) {
267269 _connect_event_id = 0 ;
268270 _conn_timer.stop ();
269- if (timeleft_ms <= 0 && _connect_retval != NSAPI_ERROR_OK) {
271+ if (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT && _connect_retval != NSAPI_ERROR_OK) {
270272 _connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
271273 }
272274 if (_connect_retval != NSAPI_ERROR_OK) {
@@ -278,7 +280,7 @@ void ESP8266Interface::_connect_async()
278280#endif
279281 } else {
280282 // Postpone to give other stuff time to run
281- _connect_event_id = _global_event_queue->call_in (ESP8266_INTERFACE_CONNECT_INTERVAL_MS ,
283+ _connect_event_id = _global_event_queue->call_in (ESP8266_INTERFACE_CONNECT_INTERVAL ,
282284 callback (this , &ESP8266Interface::_connect_async));
283285 if (!_connect_event_id) {
284286 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
@@ -410,11 +412,11 @@ void ESP8266Interface::_disconnect_async()
410412{
411413 _cmutex.lock ();
412414 _disconnect_retval = _esp.disconnect () ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
413- int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms ();
415+ auto timepassed = _conn_timer.read_duration ();
414416
415- if (_disconnect_retval == NSAPI_ERROR_OK || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
417+ if (_disconnect_retval == NSAPI_ERROR_OK || ((_if_blocking == true ) && (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT ))) {
416418
417- if (timeleft_ms <= 0 && _connect_retval != NSAPI_ERROR_OK) {
419+ if (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT && _connect_retval != NSAPI_ERROR_OK) {
418420 _disconnect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
419421 } else {
420422 if (_conn_stat != NSAPI_STATUS_DISCONNECTED) {
@@ -435,7 +437,7 @@ void ESP8266Interface::_disconnect_async()
435437 } else {
436438 // Postpone to give other stuff time to run
437439 _disconnect_event_id = _global_event_queue->call_in (
438- ESP8266_INTERFACE_CONNECT_INTERVAL_MS ,
440+ ESP8266_INTERFACE_CONNECT_INTERVAL ,
439441 callback (this , &ESP8266Interface::_disconnect_async));
440442 if (!_disconnect_event_id) {
441443 MBED_ERROR (
@@ -621,10 +623,10 @@ int8_t ESP8266Interface::get_rssi()
621623
622624int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count)
623625{
624- return scan (res, count, SCANMODE_ACTIVE, 0 , 0 );
626+ return scan (res, count, SCANMODE_ACTIVE);
625627}
626628
627- int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count, scan_mode mode, unsigned t_max, unsigned t_min)
629+ int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count, scan_mode mode, mbed::chrono::milliseconds_u32 t_max, mbed::chrono::milliseconds_u32 t_min)
628630{
629631 if (t_max > ESP8266_SCAN_TIME_MAX) {
630632 return NSAPI_ERROR_PARAMETER;
@@ -736,7 +738,15 @@ nsapi_error_t ESP8266Interface::_reset()
736738 _rst_pin.rst_assert ();
737739 // If you happen to use Pin7 CH_EN as reset pin, not needed otherwise
738740 // https://www.espressif.com/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf
739- ThisThread::sleep_for (2 ); // Documentation says 200 us; need 2 ticks to get minimum 1 ms.
741+ // First need to round up when converting to kernel ticks (eg 200us -> 1ms).
742+ auto delay = duration_cast<Kernel::Clock::duration_u32>(200us);
743+ if (delay < 200us) {
744+ delay++;
745+ }
746+ // Then need to round the clock-resolution duration up; if we were at the end of a tick
747+ // period, it might flip immediately.
748+ delay++;
749+ ThisThread::sleep_for (delay);
740750 _esp.flush ();
741751 _rst_pin.rst_deassert ();
742752 } else {
@@ -899,7 +909,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
899909 && socket->proto == NSAPI_TCP
900910 && core_util_atomic_cas_u8 (&_cbs[socket->id ].deferred , &expect_false, true )) {
901911 tr_debug (" socket_send(...): Postponing SIGIO from the device." );
902- if (!_global_event_queue->call_in (50 , callback (this , &ESP8266Interface::event_deferred))) {
912+ if (!_global_event_queue->call_in (50ms , callback (this , &ESP8266Interface::event_deferred))) {
903913 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
904914 " socket_send(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
905915 }
@@ -1056,7 +1066,7 @@ void ESP8266Interface::event()
10561066{
10571067 if (!_oob_event_id) {
10581068 // Throttles event creation by using arbitrary small delay
1059- _oob_event_id = _global_event_queue->call_in (50 , callback (this , &ESP8266Interface::proc_oob_evnt));
1069+ _oob_event_id = _global_event_queue->call_in (50ms , callback (this , &ESP8266Interface::proc_oob_evnt));
10601070 if (!_oob_event_id) {
10611071 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
10621072 " ESP8266Interface::event(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
0 commit comments