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