Skip to content

Commit b53dc66

Browse files
authored
Merge pull request #12433 from kjbracey-arm/chrono_esp
ESP8266: Convert to Chrono
2 parents b083389 + d114f54 commit b53dc66

File tree

4 files changed

+68
-49
lines changed

4 files changed

+68
-49
lines changed

components/wifi/esp8266-driver/ESP8266/ESP8266.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141

4242
using namespace mbed;
43+
using namespace std::chrono;
44+
using std::milli;
4345

4446
ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
4547
: _sdk_v(-1, -1, -1),
@@ -275,12 +277,12 @@ bool ESP8266::startup(int mode)
275277

276278
bool ESP8266::reset(void)
277279
{
278-
static const int ESP8266_BOOTTIME = 10000; // [ms]
280+
static const auto ESP8266_BOOTTIME = 10s;
279281
bool done = false;
280282

281283
_smutex.lock();
282284

283-
unsigned long int start_time = rtos::Kernel::get_ms_count();
285+
auto start_time = rtos::Kernel::Clock::now();
284286
_reset_done = false;
285287
set_timeout(ESP8266_RECV_TIMEOUT);
286288
for (int i = 0; i < 2; i++) {
@@ -291,10 +293,10 @@ bool ESP8266::reset(void)
291293

292294
while (!_reset_done) {
293295
_process_oob(ESP8266_RECV_TIMEOUT, true); // UART mutex claimed -> need to check for OOBs ourselves
294-
if (_reset_done || (rtos::Kernel::get_ms_count() - start_time >= ESP8266_BOOTTIME)) {
296+
if (_reset_done || rtos::Kernel::Clock::now() - start_time >= ESP8266_BOOTTIME) {
295297
break;
296298
}
297-
rtos::ThisThread::sleep_for(100);
299+
rtos::ThisThread::sleep_for(100ms);
298300
}
299301

300302
done = _reset_done;
@@ -520,12 +522,12 @@ int8_t ESP8266::rssi()
520522
return rssi;
521523
}
522524

523-
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min)
525+
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, duration<unsigned, milli> t_max, duration<unsigned, milli> t_min)
524526
{
525527
_smutex.lock();
526528

527529
// Default timeout plus time spend scanning each channel
528-
set_timeout(ESP8266_MISC_TIMEOUT + 13 * (t_max ? t_max : ESP8266_SCAN_TIME_MAX_DEFAULT));
530+
set_timeout(ESP8266_MISC_TIMEOUT + 13 * (t_max != t_max.zero() ? t_max : duration<unsigned, milli>(ESP8266_SCAN_TIME_MAX_DEFAULT)));
529531

530532
_scan_r.res = res;
531533
_scan_r.limit = limit;
@@ -534,7 +536,7 @@ int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned
534536
bool ret_parse_send = true;
535537

536538
if (FW_AT_LEAST_VERSION(_at_v.major, _at_v.minor, _at_v.patch, 0, ESP8266_AT_VERSION_WIFI_SCAN_CHANGE)) {
537-
ret_parse_send = _parser.send("AT+CWLAP=,,,%u,%u,%u", (mode == SCANMODE_ACTIVE ? 0 : 1), t_min, t_max);
539+
ret_parse_send = _parser.send("AT+CWLAP=,,,%u,%u,%u", (mode == SCANMODE_ACTIVE ? 0 : 1), t_min.count(), t_max.count());
538540
} else {
539541
ret_parse_send = _parser.send("AT+CWLAP");
540542
}
@@ -870,7 +872,7 @@ void ESP8266::_oob_packet_hdlr()
870872
_packets_end = &packet->next;
871873
}
872874

873-
void ESP8266::_process_oob(uint32_t timeout, bool all)
875+
void ESP8266::_process_oob(duration<uint32_t, milli> timeout, bool all)
874876
{
875877
set_timeout(timeout);
876878
// Poll for inbound packets
@@ -879,14 +881,14 @@ void ESP8266::_process_oob(uint32_t timeout, bool all)
879881
set_timeout();
880882
}
881883

882-
void ESP8266::bg_process_oob(uint32_t timeout, bool all)
884+
void ESP8266::bg_process_oob(duration<uint32_t, milli> timeout, bool all)
883885
{
884886
_smutex.lock();
885887
_process_oob(timeout, all);
886888
_smutex.unlock();
887889
}
888890

889-
int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t timeout)
891+
int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, duration<uint32_t, milli> timeout)
890892
{
891893
int32_t ret = NSAPI_ERROR_WOULD_BLOCK;
892894

@@ -949,7 +951,7 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
949951
return ret;
950952
}
951953

952-
int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
954+
int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, duration<uint32_t, milli> timeout)
953955
{
954956
if (_tcp_passive) {
955957
return _recv_tcp_passive(id, data, amount, timeout);
@@ -1007,7 +1009,7 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
10071009
return NSAPI_ERROR_WOULD_BLOCK;
10081010
}
10091011

1010-
int32_t ESP8266::recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, uint32_t timeout)
1012+
int32_t ESP8266::recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, duration<uint32_t, milli> timeout)
10111013
{
10121014
_smutex.lock();
10131015
set_timeout(timeout);
@@ -1125,9 +1127,9 @@ bool ESP8266::close(int id)
11251127
return false;
11261128
}
11271129

1128-
void ESP8266::set_timeout(uint32_t timeout_ms)
1130+
void ESP8266::set_timeout(duration<uint32_t, milli> timeout)
11291131
{
1130-
_parser.set_timeout(timeout_ms);
1132+
_parser.set_timeout(timeout.count());
11311133
}
11321134

11331135
bool ESP8266::readable()
@@ -1198,7 +1200,7 @@ bool ESP8266::get_sntp_time(std::tm *t)
11981200
memset(buf, 0, 25);
11991201

12001202
bool done = _parser.send("AT+CIPSNTPTIME?")
1201-
&& _parser.scanf("+CIPSNTPTIME:%24c", &buf)
1203+
&& _parser.scanf("+CIPSNTPTIME:%24c", buf)
12021204
&& _parser.recv("OK\n");
12031205
_smutex.unlock();
12041206

components/wifi/esp8266-driver/ESP8266/ESP8266.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,35 @@
2828
#include "PinNames.h"
2929
#include "platform/ATCmdParser.h"
3030
#include "platform/Callback.h"
31+
#include "platform/mbed_chrono.h"
3132
#include "platform/mbed_error.h"
3233
#include "rtos/Mutex.h"
3334
#include "rtos/ThisThread.h"
3435
#include "features/netsocket/SocketAddress.h"
3536

3637
// Various timeouts for different ESP8266 operations
38+
// (some of these can't use literal form as they're needed for defaults in this header, where
39+
// we shouldn't add a using directive for them. Defines only used in the C++ file can use literals).
3740
#ifndef ESP8266_CONNECT_TIMEOUT
38-
#define ESP8266_CONNECT_TIMEOUT 15000
41+
#define ESP8266_CONNECT_TIMEOUT 15s
3942
#endif
4043
#ifndef ESP8266_SEND_TIMEOUT
41-
#define ESP8266_SEND_TIMEOUT 2000
44+
#define ESP8266_SEND_TIMEOUT 2s
4245
#endif
4346
#ifndef ESP8266_RECV_TIMEOUT
44-
#define ESP8266_RECV_TIMEOUT 2000
47+
#define ESP8266_RECV_TIMEOUT std::chrono::seconds(2)
4548
#endif
4649
#ifndef ESP8266_MISC_TIMEOUT
47-
#define ESP8266_MISC_TIMEOUT 2000
50+
#define ESP8266_MISC_TIMEOUT std::chrono::seconds(2)
4851
#endif
4952
#ifndef ESP8266_DNS_TIMEOUT
50-
#define ESP8266_DNS_TIMEOUT 15000
53+
#define ESP8266_DNS_TIMEOUT 15s
5154
#endif
5255

53-
#define ESP8266_SCAN_TIME_MIN 0 // [ms]
54-
#define ESP8266_SCAN_TIME_MAX 1500 // [ms]
55-
#define ESP8266_SCAN_TIME_MIN_DEFAULT 120 // [ms]
56-
#define ESP8266_SCAN_TIME_MAX_DEFAULT 360 // [ms]
56+
#define ESP8266_SCAN_TIME_MIN 0ms
57+
#define ESP8266_SCAN_TIME_MAX 1500ms
58+
#define ESP8266_SCAN_TIME_MIN_DEFAULT 120ms
59+
#define ESP8266_SCAN_TIME_MAX_DEFAULT 360ms
5760

5861
// Firmware version
5962
#define ESP8266_SDK_VERSION 2000000
@@ -248,7 +251,9 @@ class ESP8266 {
248251
* @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
249252
* see @a nsapi_error
250253
*/
251-
int scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min);
254+
int scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode,
255+
std::chrono::duration<unsigned, std::milli> t_max,
256+
std::chrono::duration<unsigned, std::milli> t_min);
252257

253258
/**Perform a dns query
254259
*
@@ -303,7 +308,7 @@ class ESP8266 {
303308
* @param amount number of bytes to be received
304309
* @return the number of bytes received
305310
*/
306-
int32_t recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, uint32_t timeout = ESP8266_RECV_TIMEOUT);
311+
int32_t recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, mbed::chrono::milliseconds_u32 timeout = ESP8266_RECV_TIMEOUT);
307312

308313
/**
309314
* Receives stream data from an open TCP socket
@@ -313,7 +318,7 @@ class ESP8266 {
313318
* @param amount number of bytes to be received
314319
* @return the number of bytes received
315320
*/
316-
int32_t recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout = ESP8266_RECV_TIMEOUT);
321+
int32_t recv_tcp(int id, void *data, uint32_t amount, mbed::chrono::milliseconds_u32 timeout = ESP8266_RECV_TIMEOUT);
317322

318323
/**
319324
* Closes a socket
@@ -328,7 +333,7 @@ class ESP8266 {
328333
*
329334
* @param timeout_ms timeout of the connection
330335
*/
331-
void set_timeout(uint32_t timeout_ms = ESP8266_MISC_TIMEOUT);
336+
void set_timeout(mbed::chrono::milliseconds_u32 timeout = ESP8266_MISC_TIMEOUT);
332337

333338
/**
334339
* Checks if data is available
@@ -465,7 +470,7 @@ class ESP8266 {
465470
* @param timeout AT parser receive timeout
466471
* @param if TRUE, process all OOBs instead of only one
467472
*/
468-
void bg_process_oob(uint32_t timeout, bool all);
473+
void bg_process_oob(std::chrono::duration<uint32_t, std::milli> timeout, bool all);
469474

470475
/**
471476
* Flush the serial port input buffers.
@@ -496,7 +501,7 @@ class ESP8266 {
496501

497502
// FW version specific settings and functionalities
498503
bool _tcp_passive;
499-
int32_t _recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t timeout);
504+
int32_t _recv_tcp_passive(int id, void *data, uint32_t amount, std::chrono::duration<uint32_t, std::milli> timeout);
500505
mbed::Callback<void()> _callback;
501506

502507
// UART settings
@@ -529,7 +534,7 @@ class ESP8266 {
529534
size_t _heap_usage; // (Socket data buffer usage)
530535

531536
// OOB processing
532-
void _process_oob(uint32_t timeout, bool all);
537+
void _process_oob(std::chrono::duration<uint32_t, std::milli> timeout, bool all);
533538

534539
// OOB message handlers
535540
void _oob_packet_hdlr();

components/wifi/esp8266-driver/ESP8266Interface.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
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

644646
int 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");

components/wifi/esp8266-driver/ESP8266Interface.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@
3131
#include "features/netsocket/WiFiAccessPoint.h"
3232
#include "features/netsocket/WiFiInterface.h"
3333
#include "platform/Callback.h"
34+
#include "platform/mbed_chrono.h"
3435
#if MBED_CONF_RTOS_PRESENT
3536
#include "rtos/ConditionVariable.h"
3637
#endif
3738
#include "rtos/Mutex.h"
3839

3940
#define ESP8266_SOCKET_COUNT 5
4041

41-
#define ESP8266_INTERFACE_CONNECT_INTERVAL_MS (5000)
42-
#define ESP8266_INTERFACE_CONNECT_TIMEOUT_MS (2 * ESP8266_CONNECT_TIMEOUT + ESP8266_INTERFACE_CONNECT_INTERVAL_MS)
42+
#define ESP8266_INTERFACE_CONNECT_INTERVAL 5s
43+
#define ESP8266_INTERFACE_CONNECT_TIMEOUT (2 * ESP8266_CONNECT_TIMEOUT + ESP8266_INTERFACE_CONNECT_INTERVAL)
4344

4445
#ifdef TARGET_FF_ARDUINO
4546
#ifndef MBED_CONF_ESP8266_TX
@@ -225,7 +226,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
225226
* see @a nsapi_error
226227
*/
227228
virtual int scan(WiFiAccessPoint *res, unsigned count, scan_mode mode = SCANMODE_PASSIVE,
228-
unsigned t_max = 0, unsigned t_min = 0);
229+
mbed::chrono::milliseconds_u32 t_max = mbed::chrono::milliseconds_u32(0),
230+
mbed::chrono::milliseconds_u32 t_min = mbed::chrono::milliseconds_u32(0));
229231

230232
/** Translates a hostname to an IP address with specific version
231233
*

0 commit comments

Comments
 (0)