Skip to content

Commit 82c7b99

Browse files
committed
ESP8266: Convert to Chrono
1 parent 7383860 commit 82c7b99

File tree

4 files changed

+66
-48
lines changed

4 files changed

+66
-48
lines changed

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

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#define ESP8266_ALL_SOCKET_IDS -1
3737

3838
using namespace mbed;
39+
using namespace std::chrono;
3940

4041
ESP8266::ESP8266(PinName tx, PinName rx, bool debug, PinName rts, PinName cts)
4142
: _sdk_v(-1, -1, -1),
@@ -263,12 +264,12 @@ bool ESP8266::startup(int mode)
263264

264265
bool ESP8266::reset(void)
265266
{
266-
static const int ESP8266_BOOTTIME = 10000; // [ms]
267+
static const auto ESP8266_BOOTTIME = 10s;
267268
bool done = false;
268269

269270
_smutex.lock();
270271

271-
unsigned long int start_time = rtos::Kernel::get_ms_count();
272+
auto start_time = rtos::Kernel::Clock::now();
272273
_reset_done = false;
273274
set_timeout(ESP8266_RECV_TIMEOUT);
274275
for (int i = 0; i < 2; i++) {
@@ -279,10 +280,10 @@ bool ESP8266::reset(void)
279280

280281
while (!_reset_done) {
281282
_process_oob(ESP8266_RECV_TIMEOUT, true); // UART mutex claimed -> need to check for OOBs ourselves
282-
if (_reset_done || (rtos::Kernel::get_ms_count() - start_time >= ESP8266_BOOTTIME)) {
283+
if (_reset_done || rtos::Kernel::Clock::now() - start_time >= ESP8266_BOOTTIME) {
283284
break;
284285
}
285-
rtos::ThisThread::sleep_for(100);
286+
rtos::ThisThread::sleep_for(100ms);
286287
}
287288

288289
done = _reset_done;
@@ -482,12 +483,12 @@ int8_t ESP8266::rssi()
482483
return rssi;
483484
}
484485

485-
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min)
486+
int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, std::chrono::duration<unsigned, std::milli> t_max, std::chrono::duration<unsigned, std::milli> t_min)
486487
{
487488
_smutex.lock();
488489

489490
// Default timeout plus time spend scanning each channel
490-
set_timeout(ESP8266_MISC_TIMEOUT + 13 * (t_max ? t_max : ESP8266_SCAN_TIME_MAX_DEFAULT));
491+
set_timeout(ESP8266_MISC_TIMEOUT + 13 * (t_max != t_max.zero() ? t_max : std::chrono::duration<unsigned, std::milli>(ESP8266_SCAN_TIME_MAX_DEFAULT)));
491492

492493
_scan_r.res = res;
493494
_scan_r.limit = limit;
@@ -496,7 +497,7 @@ int ESP8266::scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned
496497
bool ret_parse_send = true;
497498

498499
if (FW_AT_LEAST_VERSION(_at_v.major, _at_v.minor, _at_v.patch, 0, ESP8266_AT_VERSION_WIFI_SCAN_CHANGE)) {
499-
ret_parse_send = _parser.send("AT+CWLAP=,,,%u,%u,%u", (mode == SCANMODE_ACTIVE ? 0 : 1), t_min, t_max);
500+
ret_parse_send = _parser.send("AT+CWLAP=,,,%u,%u,%u", (mode == SCANMODE_ACTIVE ? 0 : 1), t_min.count(), t_max.count());
500501
} else {
501502
ret_parse_send = _parser.send("AT+CWLAP");
502503
}
@@ -832,7 +833,7 @@ void ESP8266::_oob_packet_hdlr()
832833
_packets_end = &packet->next;
833834
}
834835

835-
void ESP8266::_process_oob(uint32_t timeout, bool all)
836+
void ESP8266::_process_oob(std::chrono::duration<uint32_t, std::milli> timeout, bool all)
836837
{
837838
set_timeout(timeout);
838839
// Poll for inbound packets
@@ -841,14 +842,14 @@ void ESP8266::_process_oob(uint32_t timeout, bool all)
841842
set_timeout();
842843
}
843844

844-
void ESP8266::bg_process_oob(uint32_t timeout, bool all)
845+
void ESP8266::bg_process_oob(std::chrono::duration<uint32_t, std::milli> timeout, bool all)
845846
{
846847
_smutex.lock();
847848
_process_oob(timeout, all);
848849
_smutex.unlock();
849850
}
850851

851-
int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t timeout)
852+
int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, std::chrono::duration<uint32_t, std::milli> timeout)
852853
{
853854
int32_t ret = NSAPI_ERROR_WOULD_BLOCK;
854855

@@ -911,7 +912,7 @@ int32_t ESP8266::_recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t
911912
return ret;
912913
}
913914

914-
int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
915+
int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, std::chrono::duration<uint32_t, std::milli> timeout)
915916
{
916917
if (_tcp_passive) {
917918
return _recv_tcp_passive(id, data, amount, timeout);
@@ -969,7 +970,7 @@ int32_t ESP8266::recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout)
969970
return NSAPI_ERROR_WOULD_BLOCK;
970971
}
971972

972-
int32_t ESP8266::recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, uint32_t timeout)
973+
int32_t ESP8266::recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, std::chrono::duration<uint32_t, std::milli> timeout)
973974
{
974975
_smutex.lock();
975976
set_timeout(timeout);
@@ -1087,9 +1088,9 @@ bool ESP8266::close(int id)
10871088
return false;
10881089
}
10891090

1090-
void ESP8266::set_timeout(uint32_t timeout_ms)
1091+
void ESP8266::set_timeout(duration<uint32_t, std::milli> timeout)
10911092
{
1092-
_parser.set_timeout(timeout_ms);
1093+
_parser.set_timeout(timeout);
10931094
}
10941095

10951096
bool ESP8266::readable()

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

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,35 @@
2626
#include "PinNames.h"
2727
#include "platform/ATCmdParser.h"
2828
#include "platform/Callback.h"
29+
#include "platform/mbed_chrono.h"
2930
#include "platform/mbed_error.h"
3031
#include "rtos/Mutex.h"
3132
#include "rtos/ThisThread.h"
3233
#include "features/netsocket/SocketAddress.h"
3334

3435
// Various timeouts for different ESP8266 operations
36+
// (some of these can't use literal form as they're needed for defaults in this header, where
37+
// we shouldn't add a using directive for them. Defines only used in the C++ file can use literals).
3538
#ifndef ESP8266_CONNECT_TIMEOUT
36-
#define ESP8266_CONNECT_TIMEOUT 15000
39+
#define ESP8266_CONNECT_TIMEOUT 15s
3740
#endif
3841
#ifndef ESP8266_SEND_TIMEOUT
39-
#define ESP8266_SEND_TIMEOUT 2000
42+
#define ESP8266_SEND_TIMEOUT 2s
4043
#endif
4144
#ifndef ESP8266_RECV_TIMEOUT
42-
#define ESP8266_RECV_TIMEOUT 2000
45+
#define ESP8266_RECV_TIMEOUT std::chrono::seconds(2)
4346
#endif
4447
#ifndef ESP8266_MISC_TIMEOUT
45-
#define ESP8266_MISC_TIMEOUT 2000
48+
#define ESP8266_MISC_TIMEOUT std::chrono::seconds(2)
4649
#endif
4750
#ifndef ESP8266_DNS_TIMEOUT
48-
#define ESP8266_DNS_TIMEOUT 15000
51+
#define ESP8266_DNS_TIMEOUT 15s
4952
#endif
5053

51-
#define ESP8266_SCAN_TIME_MIN 0 // [ms]
52-
#define ESP8266_SCAN_TIME_MAX 1500 // [ms]
53-
#define ESP8266_SCAN_TIME_MIN_DEFAULT 120 // [ms]
54-
#define ESP8266_SCAN_TIME_MAX_DEFAULT 360 // [ms]
54+
#define ESP8266_SCAN_TIME_MIN 0ms
55+
#define ESP8266_SCAN_TIME_MAX 1500ms
56+
#define ESP8266_SCAN_TIME_MIN_DEFAULT 120ms
57+
#define ESP8266_SCAN_TIME_MAX_DEFAULT 360ms
5558

5659
// Firmware version
5760
#define ESP8266_SDK_VERSION 2000000
@@ -235,7 +238,9 @@ class ESP8266 {
235238
* @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
236239
* see @a nsapi_error
237240
*/
238-
int scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode, unsigned t_max, unsigned t_min);
241+
int scan(WiFiAccessPoint *res, unsigned limit, scan_mode mode,
242+
std::chrono::duration<unsigned, std::milli> t_max,
243+
std::chrono::duration<unsigned, std::milli> t_min);
239244

240245
/**Perform a dns query
241246
*
@@ -290,7 +295,7 @@ class ESP8266 {
290295
* @param amount number of bytes to be received
291296
* @return the number of bytes received
292297
*/
293-
int32_t recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, uint32_t timeout = ESP8266_RECV_TIMEOUT);
298+
int32_t recv_udp(struct esp8266_socket *socket, void *data, uint32_t amount, mbed::chrono::milliseconds_u32 timeout = ESP8266_RECV_TIMEOUT);
294299

295300
/**
296301
* Receives stream data from an open TCP socket
@@ -300,7 +305,7 @@ class ESP8266 {
300305
* @param amount number of bytes to be received
301306
* @return the number of bytes received
302307
*/
303-
int32_t recv_tcp(int id, void *data, uint32_t amount, uint32_t timeout = ESP8266_RECV_TIMEOUT);
308+
int32_t recv_tcp(int id, void *data, uint32_t amount, mbed::chrono::milliseconds_u32 timeout = ESP8266_RECV_TIMEOUT);
304309

305310
/**
306311
* Closes a socket
@@ -315,7 +320,7 @@ class ESP8266 {
315320
*
316321
* @param timeout_ms timeout of the connection
317322
*/
318-
void set_timeout(uint32_t timeout_ms = ESP8266_MISC_TIMEOUT);
323+
void set_timeout(mbed::chrono::milliseconds_u32 timeout = ESP8266_MISC_TIMEOUT);
319324

320325
/**
321326
* Checks if data is available
@@ -411,7 +416,7 @@ class ESP8266 {
411416
* @param timeout AT parser receive timeout
412417
* @param if TRUE, process all OOBs instead of only one
413418
*/
414-
void bg_process_oob(uint32_t timeout, bool all);
419+
void bg_process_oob(std::chrono::duration<uint32_t, std::milli> timeout, bool all);
415420

416421
/**
417422
* Flush the serial port input buffers.
@@ -442,7 +447,7 @@ class ESP8266 {
442447

443448
// FW version specific settings and functionalities
444449
bool _tcp_passive;
445-
int32_t _recv_tcp_passive(int id, void *data, uint32_t amount, uint32_t timeout);
450+
int32_t _recv_tcp_passive(int id, void *data, uint32_t amount, std::chrono::duration<uint32_t, std::milli> timeout);
446451
mbed::Callback<void()> _callback;
447452

448453
// UART settings
@@ -475,7 +480,7 @@ class ESP8266 {
475480
size_t _heap_usage; // (Socket data buffer usage)
476481

477482
// OOB processing
478-
void _process_oob(uint32_t timeout, bool all);
483+
void _process_oob(std::chrono::duration<uint32_t, std::milli> timeout, bool all);
479484

480485
// OOB message handlers
481486
void _oob_packet_hdlr();

components/wifi/esp8266-driver/ESP8266Interface.cpp

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

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

components/wifi/esp8266-driver/ESP8266Interface.h

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

3839
#define ESP8266_SOCKET_COUNT 5
3940

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

4344
#ifdef TARGET_FF_ARDUINO
4445
#ifndef MBED_CONF_ESP8266_TX
@@ -212,7 +213,8 @@ class ESP8266Interface : public NetworkStack, public WiFiInterface {
212213
* see @a nsapi_error
213214
*/
214215
virtual int scan(WiFiAccessPoint *res, unsigned count, scan_mode mode = SCANMODE_PASSIVE,
215-
unsigned t_max = 0, unsigned t_min = 0);
216+
mbed::chrono::milliseconds_u32 t_max = mbed::chrono::milliseconds_u32(0),
217+
mbed::chrono::milliseconds_u32 t_min = mbed::chrono::milliseconds_u32(0));
216218

217219
/** Translates a hostname to an IP address with specific version
218220
*

0 commit comments

Comments
 (0)