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
@@ -196,7 +198,7 @@ void ESP8266Interface::PowerPin::power_on()
196198 if (_pwr_pin.is_connected ()) {
197199 _pwr_pin = MBED_CONF_ESP8266_POWER_ON_POLARITY;
198200 tr_debug (" power_on(): HW power-on." );
199- ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_ON_TIME_MS);
201+ ThisThread::sleep_for (milliseconds ( MBED_CONF_ESP8266_POWER_ON_TIME_MS) );
200202 }
201203}
202204
@@ -205,7 +207,7 @@ void ESP8266Interface::PowerPin::power_off()
205207 if (_pwr_pin.is_connected ()) {
206208 _pwr_pin = !MBED_CONF_ESP8266_POWER_ON_POLARITY;
207209 tr_debug (" power_off(): HW power-off." );
208- ThisThread::sleep_for (MBED_CONF_ESP8266_POWER_OFF_TIME_MS);
210+ ThisThread::sleep_for (milliseconds ( MBED_CONF_ESP8266_POWER_OFF_TIME_MS) );
209211 }
210212}
211213
@@ -260,14 +262,14 @@ void ESP8266Interface::_connect_async()
260262 return ;
261263 }
262264 _connect_retval = _esp.connect (ap_ssid, ap_pass);
263- int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms ();
265+ auto timepassed = _conn_timer.elapsed_time ();
264266 if (_connect_retval == NSAPI_ERROR_OK
265267 || _connect_retval == NSAPI_ERROR_AUTH_FAILURE
266268 || _connect_retval == NSAPI_ERROR_NO_SSID
267- || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
269+ || ((_if_blocking == true ) && (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT ))) {
268270 _connect_event_id = 0 ;
269271 _conn_timer.stop ();
270- if (timeleft_ms <= 0 && _connect_retval != NSAPI_ERROR_OK) {
272+ if (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT && _connect_retval != NSAPI_ERROR_OK) {
271273 _connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
272274 }
273275 if (_connect_retval != NSAPI_ERROR_OK) {
@@ -279,7 +281,7 @@ void ESP8266Interface::_connect_async()
279281#endif
280282 } else {
281283 // Postpone to give other stuff time to run
282- _connect_event_id = _global_event_queue->call_in (ESP8266_INTERFACE_CONNECT_INTERVAL_MS ,
284+ _connect_event_id = _global_event_queue->call_in (ESP8266_INTERFACE_CONNECT_INTERVAL ,
283285 callback (this , &ESP8266Interface::_connect_async));
284286 if (!_connect_event_id) {
285287 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
@@ -411,11 +413,11 @@ void ESP8266Interface::_disconnect_async()
411413{
412414 _cmutex.lock ();
413415 _disconnect_retval = _esp.disconnect () ? NSAPI_ERROR_OK : NSAPI_ERROR_DEVICE_ERROR;
414- int timeleft_ms = ESP8266_INTERFACE_CONNECT_TIMEOUT_MS - _conn_timer.read_ms ();
416+ auto timepassed = _conn_timer.elapsed_time ();
415417
416- if (_disconnect_retval == NSAPI_ERROR_OK || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
418+ if (_disconnect_retval == NSAPI_ERROR_OK || ((_if_blocking == true ) && (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT ))) {
417419
418- if (timeleft_ms <= 0 && _connect_retval != NSAPI_ERROR_OK) {
420+ if (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT && _connect_retval != NSAPI_ERROR_OK) {
419421 _disconnect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
420422 } else {
421423 if (_conn_stat != NSAPI_STATUS_DISCONNECTED) {
@@ -436,7 +438,7 @@ void ESP8266Interface::_disconnect_async()
436438 } else {
437439 // Postpone to give other stuff time to run
438440 _disconnect_event_id = _global_event_queue->call_in (
439- ESP8266_INTERFACE_CONNECT_INTERVAL_MS ,
441+ ESP8266_INTERFACE_CONNECT_INTERVAL ,
440442 callback (this , &ESP8266Interface::_disconnect_async));
441443 if (!_disconnect_event_id) {
442444 MBED_ERROR (
@@ -612,10 +614,10 @@ int8_t ESP8266Interface::get_rssi()
612614
613615int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count)
614616{
615- return scan (res, count, SCANMODE_ACTIVE, 0 , 0 );
617+ return scan (res, count, SCANMODE_ACTIVE);
616618}
617619
618- int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count, scan_mode mode, unsigned t_max, unsigned t_min)
620+ int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count, scan_mode mode, mbed::chrono::milliseconds_u32 t_max, mbed::chrono::milliseconds_u32 t_min)
619621{
620622 if (t_max > ESP8266_SCAN_TIME_MAX) {
621623 return NSAPI_ERROR_PARAMETER;
@@ -735,7 +737,15 @@ nsapi_error_t ESP8266Interface::_reset()
735737 _rst_pin.rst_assert ();
736738 // If you happen to use Pin7 CH_EN as reset pin, not needed otherwise
737739 // https://www.espressif.com/sites/default/files/documentation/esp8266_hardware_design_guidelines_en.pdf
738- ThisThread::sleep_for (2 ); // Documentation says 200 us; need 2 ticks to get minimum 1 ms.
740+ // First need to round up when converting to kernel ticks (eg 200us -> 1ms).
741+ auto delay = duration_cast<Kernel::Clock::duration_u32>(200us);
742+ if (delay < 200us) {
743+ delay++;
744+ }
745+ // Then need to round the clock-resolution duration up; if we were at the end of a tick
746+ // period, it might flip immediately.
747+ delay++;
748+ ThisThread::sleep_for (delay);
739749 _esp.flush ();
740750 _rst_pin.rst_deassert ();
741751 } else {
@@ -898,7 +908,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
898908 && socket->proto == NSAPI_TCP
899909 && core_util_atomic_cas_u8 (&_cbs[socket->id ].deferred , &expect_false, true )) {
900910 tr_debug (" socket_send(...): Postponing SIGIO from the device." );
901- if (!_global_event_queue->call_in (50 , callback (this , &ESP8266Interface::event_deferred))) {
911+ if (!_global_event_queue->call_in (50ms , callback (this , &ESP8266Interface::event_deferred))) {
902912 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
903913 " socket_send(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
904914 }
@@ -1055,7 +1065,7 @@ void ESP8266Interface::event()
10551065{
10561066 if (!_oob_event_id) {
10571067 // Throttles event creation by using arbitrary small delay
1058- _oob_event_id = _global_event_queue->call_in (50 , callback (this , &ESP8266Interface::proc_oob_evnt));
1068+ _oob_event_id = _global_event_queue->call_in (50ms , callback (this , &ESP8266Interface::proc_oob_evnt));
10591069 if (!_oob_event_id) {
10601070 MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
10611071 " ESP8266Interface::event(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
0 commit comments