31
31
#include " platform/mbed_debug.h"
32
32
#include " rtos/ThisThread.h"
33
33
34
+ using namespace std ::chrono;
35
+
34
36
#ifndef MBED_CONF_ESP8266_DEBUG
35
37
#define MBED_CONF_ESP8266_DEBUG false
36
38
#endif
@@ -196,7 +198,7 @@ void ESP8266Interface::PowerPin::power_on()
196
198
if (_pwr_pin.is_connected ()) {
197
199
_pwr_pin = MBED_CONF_ESP8266_POWER_ON_POLARITY;
198
200
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) );
200
202
}
201
203
}
202
204
@@ -205,7 +207,7 @@ void ESP8266Interface::PowerPin::power_off()
205
207
if (_pwr_pin.is_connected ()) {
206
208
_pwr_pin = !MBED_CONF_ESP8266_POWER_ON_POLARITY;
207
209
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) );
209
211
}
210
212
}
211
213
@@ -260,14 +262,14 @@ void ESP8266Interface::_connect_async()
260
262
return ;
261
263
}
262
264
_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.read_duration ();
264
266
if (_connect_retval == NSAPI_ERROR_OK
265
267
|| _connect_retval == NSAPI_ERROR_AUTH_FAILURE
266
268
|| _connect_retval == NSAPI_ERROR_NO_SSID
267
- || ((_if_blocking == true ) && (timeleft_ms <= 0 ))) {
269
+ || ((_if_blocking == true ) && (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT ))) {
268
270
_connect_event_id = 0 ;
269
271
_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) {
271
273
_connect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
272
274
}
273
275
if (_connect_retval != NSAPI_ERROR_OK) {
@@ -279,7 +281,7 @@ void ESP8266Interface::_connect_async()
279
281
#endif
280
282
} else {
281
283
// 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 ,
283
285
callback (this , &ESP8266Interface::_connect_async));
284
286
if (!_connect_event_id) {
285
287
MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
@@ -411,11 +413,11 @@ void ESP8266Interface::_disconnect_async()
411
413
{
412
414
_cmutex.lock ();
413
415
_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.read_duration ();
415
417
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 ))) {
417
419
418
- if (timeleft_ms <= 0 && _connect_retval != NSAPI_ERROR_OK) {
420
+ if (timepassed >= ESP8266_INTERFACE_CONNECT_TIMEOUT && _connect_retval != NSAPI_ERROR_OK) {
419
421
_disconnect_retval = NSAPI_ERROR_CONNECTION_TIMEOUT;
420
422
} else {
421
423
if (_conn_stat != NSAPI_STATUS_DISCONNECTED) {
@@ -436,7 +438,7 @@ void ESP8266Interface::_disconnect_async()
436
438
} else {
437
439
// Postpone to give other stuff time to run
438
440
_disconnect_event_id = _global_event_queue->call_in (
439
- ESP8266_INTERFACE_CONNECT_INTERVAL_MS ,
441
+ ESP8266_INTERFACE_CONNECT_INTERVAL ,
440
442
callback (this , &ESP8266Interface::_disconnect_async));
441
443
if (!_disconnect_event_id) {
442
444
MBED_ERROR (
@@ -612,10 +614,10 @@ int8_t ESP8266Interface::get_rssi()
612
614
613
615
int ESP8266Interface::scan (WiFiAccessPoint *res, unsigned count)
614
616
{
615
- return scan (res, count, SCANMODE_ACTIVE, 0 , 0 );
617
+ return scan (res, count, SCANMODE_ACTIVE);
616
618
}
617
619
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)
619
621
{
620
622
if (t_max > ESP8266_SCAN_TIME_MAX) {
621
623
return NSAPI_ERROR_PARAMETER;
@@ -735,7 +737,15 @@ nsapi_error_t ESP8266Interface::_reset()
735
737
_rst_pin.rst_assert ();
736
738
// If you happen to use Pin7 CH_EN as reset pin, not needed otherwise
737
739
// 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);
739
749
_esp.flush ();
740
750
_rst_pin.rst_deassert ();
741
751
} else {
@@ -898,7 +908,7 @@ int ESP8266Interface::socket_send(void *handle, const void *data, unsigned size)
898
908
&& socket->proto == NSAPI_TCP
899
909
&& core_util_atomic_cas_u8 (&_cbs[socket->id ].deferred , &expect_false, true )) {
900
910
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))) {
902
912
MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
903
913
" socket_send(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
904
914
}
@@ -1055,7 +1065,7 @@ void ESP8266Interface::event()
1055
1065
{
1056
1066
if (!_oob_event_id) {
1057
1067
// 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));
1059
1069
if (!_oob_event_id) {
1060
1070
MBED_ERROR (MBED_MAKE_ERROR (MBED_MODULE_DRIVER, MBED_ERROR_CODE_ENOMEM), \
1061
1071
" ESP8266Interface::event(): unable to add event to queue. Increase \" events.shared-eventsize\"\n " );
0 commit comments