diff --git a/TESTS/netsocket/udp/main.cpp b/TESTS/netsocket/udp/main.cpp index 664528d462..b3bfa899fc 100644 --- a/TESTS/netsocket/udp/main.cpp +++ b/TESTS/netsocket/udp/main.cpp @@ -118,9 +118,9 @@ void fill_tx_buffer_ascii(char *buff, size_t len) } } -int split2half_rmng_udp_test_time() +microseconds split2half_rmng_udp_test_time() { - return (udp_global::TESTS_TIMEOUT - tc_bucket.read()) / 2; + return (udp_global::TESTS_TIMEOUT - tc_bucket.elapsed_time()) / 2; } #if MBED_CONF_NSAPI_SOCKET_STATS_ENABLED @@ -133,7 +133,7 @@ int fetch_stats() // Test setup utest::v1::status_t greentea_setup(const size_t number_of_cases) { - GREENTEA_SETUP(udp_global::TESTS_TIMEOUT, "default_auto"); + GREENTEA_SETUP(seconds(udp_global::TESTS_TIMEOUT).count(), "default_auto"); _ifup(); tc_bucket.start(); return greentea_test_setup_handler(number_of_cases); diff --git a/TESTS/netsocket/udp/udp_tests.h b/TESTS/netsocket/udp/udp_tests.h index ed5bba5821..3ae90e1c19 100644 --- a/TESTS/netsocket/udp/udp_tests.h +++ b/TESTS/netsocket/udp/udp_tests.h @@ -21,6 +21,8 @@ #include "../test_params.h" #include "mbed_trace.h" +using namespace std::chrono; + #define TRACE_GROUP "GRNT" NetworkInterface *get_interface(); @@ -37,18 +39,18 @@ int fetch_stats(void); /** * Single testcase might take only half of the remaining execution time */ -int split2half_rmng_udp_test_time(); // [s] +microseconds split2half_rmng_udp_test_time(); namespace udp_global { #ifdef MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S -static const int TESTS_TIMEOUT = MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S; +static constexpr seconds TESTS_TIMEOUT(MBED_GREENTEA_TEST_UDPSOCKET_TIMEOUT_S); #else #define MESH 3 #define WISUN 0x2345 #if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == MESH && MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == WISUN -static const int TESTS_TIMEOUT = (25 * 60); +static constexpr seconds TESTS_TIMEOUT = 25min; #else -static const int TESTS_TIMEOUT = (20 * 60); +static constexpr seconds TESTS_TIMEOUT = 20min; #endif #endif diff --git a/TESTS/netsocket/udp/udpsocket_echotest.cpp b/TESTS/netsocket/udp/udpsocket_echotest.cpp index 575921135c..530ba97a02 100755 --- a/TESTS/netsocket/udp/udpsocket_echotest.cpp +++ b/TESTS/netsocket/udp/udpsocket_echotest.cpp @@ -23,13 +23,14 @@ #include "utest.h" #include "udp_tests.h" +using namespace std::chrono; using namespace utest::v1; namespace { static const int SIGNAL_SIGIO_RX = 0x1; static const int SIGNAL_SIGIO_TX = 0x2; -static const int SIGIO_TIMEOUT = 5000; //[ms] -static const int SOCKET_TIMEOUT = (10 * 1000); //[ms] +static constexpr seconds SIGIO_TIMEOUT = 5s; +static constexpr seconds SOCKET_TIMEOUT = 10s; static const int RETRIES = 2; static const double EXPECTED_LOSS_RATIO = 0.0; @@ -53,7 +54,7 @@ static bool pkt_received[PKTS] = {false, false, false, false, false, false, fals }; Timer tc_exec_time; -int time_allotted; +microseconds time_allotted; } static void _sigio_handler() @@ -75,7 +76,7 @@ void UDPSOCKET_ECHOTEST_impl(bool use_sendto) TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, sock.connect(udp_addr)); } - sock.set_timeout(SOCKET_TIMEOUT); + sock.set_timeout(milliseconds(SOCKET_TIMEOUT).count()); int recvd; int sent; int packets_sent = 0; @@ -169,7 +170,7 @@ void UDPSOCKET_ECHOTEST_CONNECT_SEND_RECV() void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto) { tc_exec_time.start(); - time_allotted = split2half_rmng_udp_test_time(); // [s] + time_allotted = split2half_rmng_udp_test_time(); SocketAddress udp_addr; NetworkInterface::get_default_instance()->gethostbyname(ECHO_SERVER_ADDR, &udp_addr); @@ -211,8 +212,12 @@ void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto) } else if (sent == pkt_s) { packets_sent++; } else if (sent == NSAPI_ERROR_WOULD_BLOCK) { - if (tc_exec_time.read() >= time_allotted || - signals.wait_all(SIGNAL_SIGIO_TX, SIGIO_TIMEOUT) == osFlagsErrorTimeout) { + if ( + (tc_exec_time.elapsed_time() >= time_allotted) + || signals.wait_all( + SIGNAL_SIGIO_TX, milliseconds(SIGIO_TIMEOUT).count() + ) == osFlagsErrorTimeout + ) { continue; } --retry_cnt; @@ -230,10 +235,13 @@ void UDPSOCKET_ECHOTEST_NONBLOCK_impl(bool use_sendto) } if (recvd == NSAPI_ERROR_WOULD_BLOCK) { - if (tc_exec_time.read() >= time_allotted) { + if (tc_exec_time.elapsed_time() >= time_allotted) { break; } - signals.wait_all(SIGNAL_SIGIO_RX, SIGIO_TIMEOUT); + signals.wait_all( + SIGNAL_SIGIO_RX, + milliseconds(SIGIO_TIMEOUT).count() + ); --retry_recv; continue; } else if (recvd < 0) { diff --git a/components/802.15.4_RF/atmel-rf-driver/source/NanostackRfPhyAtmel.cpp b/components/802.15.4_RF/atmel-rf-driver/source/NanostackRfPhyAtmel.cpp index ef3ce7f447..c0c95a39c8 100644 --- a/components/802.15.4_RF/atmel-rf-driver/source/NanostackRfPhyAtmel.cpp +++ b/components/802.15.4_RF/atmel-rf-driver/source/NanostackRfPhyAtmel.cpp @@ -35,6 +35,8 @@ #include "Timeout.h" #include "platform/mbed_error.h" +using namespace std::chrono; + #define TRACE_GROUP "AtRF" /*Worst case sensitivity*/ @@ -356,9 +358,9 @@ static rf_trx_part_e rf_radio_type_read(void) static void rf_if_ack_wait_timer_start(uint16_t slots) { #ifdef MBED_CONF_RTOS_PRESENT - rf->ack_timer.attach_us(rf_if_ack_timer_signal, slots * 50); + rf->ack_timer.attach(rf_if_ack_timer_signal, slots * 50us); #else - rf->ack_timer.attach_us(rf_ack_wait_timer_interrupt, slots * 50); + rf->ack_timer.attach(rf_ack_wait_timer_interrupt, slots * 50us); #endif } @@ -372,9 +374,9 @@ static void rf_if_ack_wait_timer_start(uint16_t slots) static void rf_if_calibration_timer_start(uint32_t slots) { #ifdef MBED_CONF_RTOS_PRESENT - rf->cal_timer.attach_us(rf_if_cal_timer_signal, slots * 50); + rf->cal_timer.attach(rf_if_cal_timer_signal, slots * 50us); #else - rf->cal_timer.attach_us(rf_calibration_timer_interrupt, slots * 50); + rf->cal_timer.attach(rf_calibration_timer_interrupt, slots * 50us); #endif } @@ -388,9 +390,9 @@ static void rf_if_calibration_timer_start(uint32_t slots) static void rf_if_cca_timer_start(uint32_t slots) { #ifdef MBED_CONF_RTOS_PRESENT - rf->cca_timer.attach_us(rf_if_cca_timer_signal, slots * 50); + rf->cca_timer.attach(rf_if_cca_timer_signal, slots * 50us); #else - rf->cca_timer.attach_us(rf_cca_timer_interrupt, slots * 50); + rf->cca_timer.attach(rf_cca_timer_interrupt, slots * 50us); #endif } @@ -519,14 +521,14 @@ static void rf_if_reset_radio(void) #endif rf->IRQ.rise(nullptr); rf->RST = 1; - ThisThread::sleep_for(2); + ThisThread::sleep_for(2ms); rf->RST = 0; - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); CS_RELEASE(); rf->SLP_TR = 0; - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); rf->RST = 1; - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); rf->IRQ.rise(&rf_if_interrupt_handler); } diff --git a/components/802.15.4_RF/stm-s2lp-rf-driver/source/NanostackRfPhys2lp.cpp b/components/802.15.4_RF/stm-s2lp-rf-driver/source/NanostackRfPhys2lp.cpp index 720a0535ec..e66317a37c 100644 --- a/components/802.15.4_RF/stm-s2lp-rf-driver/source/NanostackRfPhys2lp.cpp +++ b/components/802.15.4_RF/stm-s2lp-rf-driver/source/NanostackRfPhys2lp.cpp @@ -15,6 +15,7 @@ */ #include #if defined(MBED_CONF_NANOSTACK_CONFIGURATION) && DEVICE_SPI && DEVICE_INTERRUPTIN && defined(MBED_CONF_RTOS_PRESENT) + #include "platform/arm_hal_interrupt.h" #include "nanostack/platform/arm_hal_phy.h" #include "ns_types.h" @@ -31,6 +32,7 @@ #include "mbed_wait_api.h" #include "platform/mbed_error.h" +using namespace std::chrono; using namespace mbed; using namespace rtos; @@ -276,7 +278,7 @@ static void rf_calculate_symbol_rate(uint32_t baudrate, phy_modulation_e modulat static uint32_t rf_get_timestamp(void) { - return (uint32_t)rf->tx_timer.read_us(); + return (uint32_t)rf->tx_timer.elapsed_time().count(); } static void rf_update_tx_active_time(void) @@ -866,7 +868,7 @@ static void rf_cca_timer_stop(void) static void rf_cca_timer_start(uint32_t slots) { - rf->cca_timer.attach_us(rf_cca_timer_signal, slots); + rf->cca_timer.attach(rf_cca_timer_signal, microseconds(slots)); TEST_CSMA_STARTED } @@ -903,7 +905,7 @@ static void rf_backup_timer_stop(void) static void rf_backup_timer_start(uint32_t slots) { - rf->backup_timer.attach_us(rf_backup_timer_signal, slots); + rf->backup_timer.attach(rf_backup_timer_signal, microseconds(slots)); } static int8_t rf_start_cca(uint8_t *data_ptr, uint16_t data_length, uint8_t tx_handle, data_protocol_e data_protocol) @@ -1177,10 +1179,10 @@ static void rf_reset(void) { // Shutdown rf->SDN = 1; - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); // Wake up rf->SDN = 0; - ThisThread::sleep_for(10); + ThisThread::sleep_for(10ms); } static void rf_init(void) diff --git a/components/storage/blockdevice/COMPONENT_QSPIF/QSPIFBlockDevice.cpp b/components/storage/blockdevice/COMPONENT_QSPIF/QSPIFBlockDevice.cpp index 804a0fd6b3..33f716b777 100644 --- a/components/storage/blockdevice/COMPONENT_QSPIF/QSPIFBlockDevice.cpp +++ b/components/storage/blockdevice/COMPONENT_QSPIF/QSPIFBlockDevice.cpp @@ -28,6 +28,7 @@ #include "mbed_trace.h" #define TRACE_GROUP "QSPIF" +using namespace std::chrono; using namespace mbed; /* Default QSPIF Parameters */ @@ -1228,7 +1229,7 @@ bool QSPIFBlockDevice::_is_mem_ready() bool mem_ready = true; do { - rtos::ThisThread::sleep_for(1); + rtos::ThisThread::sleep_for(1ms); retries++; //Read Status Register 1 from device if (QSPI_STATUS_OK != _qspi_send_general_command(QSPIF_INST_RSR1, QSPI_NO_ADDRESS_COMMAND, diff --git a/features/FEATURE_BLE/ble/common/Duration.h b/features/FEATURE_BLE/ble/common/Duration.h index 5441f7a057..e6c4f6bafa 100644 --- a/features/FEATURE_BLE/ble/common/Duration.h +++ b/features/FEATURE_BLE/ble/common/Duration.h @@ -20,6 +20,7 @@ #include #include #include "platform/mbed_assert.h" +#include "platform/mbed_chrono.h" namespace ble { @@ -231,6 +232,48 @@ struct Duration { return Duration(Forever::VALUE); } +#if defined(DOXYGEN_ONLY) + /** + * Test if the forever value is being held + * @return True if the forever value is held False otherwise + */ + bool isForever() const; +#else + // Overload when Forever isn't defined + template + std::enable_if_t< + std::is_same::value, + bool + > + isForever() const + { + return false; + } + + // Overload when Forever is defined + template + std::enable_if_t< + !std::is_same::value, + bool + > + isForever() const + { + return duration == Forever::VALUE; + } +#endif + + /** + * Convert the duration into an std::chrono one. + * @return The duration in the std::chrono format. + */ + std::chrono::duration::type> + valueChrono() const + { + MBED_ASSERT(!isForever()); + + return std::chrono::duration::type>{duration}; + } + private: static Rep clamp(Rep in) { diff --git a/features/FEATURE_BLE/source/generic/GenericGap.tpp b/features/FEATURE_BLE/source/generic/GenericGap.tpp index 2cafaf4a2b..c5b49f88ea 100644 --- a/features/FEATURE_BLE/source/generic/GenericGap.tpp +++ b/features/FEATURE_BLE/source/generic/GenericGap.tpp @@ -27,6 +27,8 @@ #include "drivers/Timeout.h" +using namespace std::chrono; + namespace ble { namespace generic { @@ -1390,9 +1392,9 @@ void GenericGap(maxDuration).value() + maxDuration.valueChrono() ); } } @@ -2500,9 +2502,9 @@ ble_error_t GenericGapon_host_stack_inactivity(); diff --git a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp index 5895cb4fc2..d4120070fe 100755 --- a/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp +++ b/features/cellular/framework/targets/Altair/ALT1250/PPP/ALT1250_PPP.cpp @@ -22,6 +22,7 @@ #include "CellularLog.h" #include "rtos/ThisThread.h" +using namespace std::chrono; using namespace rtos; using namespace mbed; using namespace events; @@ -86,7 +87,7 @@ nsapi_error_t ALT1250_PPP::soft_power_on() tr_warn("Modem is not responding to AT commands, reset it"); if (_rst.is_connected()) { _rst = 0; - ThisThread::sleep_for(100); + ThisThread::sleep_for(100ms); _rst = 1; } if (_at.sync(2000)) { diff --git a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp index 43e979bf23..5ff3d78587 100644 --- a/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp +++ b/features/cellular/framework/targets/QUECTEL/BC95/QUECTEL_BC95_CellularStack.cpp @@ -26,12 +26,13 @@ #include "CellularLog.h" #define PACKET_SIZE_MAX 1358 -#define TXFULL_EVENT_TIMEOUT (1 * 1000) // ms +#define TXFULL_EVENT_TIMEOUT 1s #define AT_UPLINK_BUSY 159 #define AT_UART_BUFFER_ERROR 536 #define AT_BACK_OFF_TIMER 537 +using namespace std::chrono; using namespace mbed; using namespace mbed_cellular_util; @@ -230,7 +231,7 @@ retry_send: if (retry < 3) { retry++; tr_warn("Socket %d sendto EAGAIN", socket->id); - rtos::ThisThread::sleep_for(30); + rtos::ThisThread::sleep_for(30ms); _at.clear_error(); goto retry_send; } diff --git a/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.cpp b/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.cpp index 82f577fbfd..e8642926d8 100644 --- a/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.cpp +++ b/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.cpp @@ -26,7 +26,7 @@ using namespace mbed; using namespace mbed_cellular_util; -using namespace std::chrono_literals; +using namespace std::chrono; RM1000_AT_CellularStack::RM1000_AT_CellularStack(ATHandler &atHandler, int cid, nsapi_ip_stack_t stack_type, AT_CellularDevice &device) : AT_CellularStack(atHandler, cid, stack_type, device) @@ -266,7 +266,7 @@ nsapi_size_or_error_t RM1000_AT_CellularStack::socket_recvfrom_impl(CellularSock // read() should not fail success = false; } - } else if (timer.read_ms() < SOCKET_TIMEOUT) { + } else if (timer.elapsed_time() < SOCKET_TIMEOUT) { // Wait for URCs _at.process_oob(); } else { diff --git a/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.h b/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.h index 5f28c1ec0e..a8b6c4af89 100644 --- a/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.h +++ b/features/cellular/framework/targets/RiotMicro/AT/RM1000_AT_CellularStack.h @@ -17,11 +17,14 @@ #ifndef RM1000_AT_CELLULARSTACK_H_ #define RM1000_AT_CELLULARSTACK_H_ +#include + #include "AT_CellularStack.h" #include "CellularUtil.h" #include "mbed_wait_api.h" #include "drivers/Timer.h" +using namespace std::chrono; namespace mbed { @@ -54,7 +57,7 @@ protected: // AT_CellularStack * call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK * and the user has set a larger timeout or full blocking. */ - static const int SOCKET_TIMEOUT = 1000; + static constexpr seconds SOCKET_TIMEOUT = 1s; /** The maximum number of bytes in a packet that can be write/read from * the AT interface in one go. diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp index a3011f88aa..aceaf24201 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularNetwork.cpp @@ -88,7 +88,7 @@ nsapi_error_t UBLOX_AT_CellularNetwork::ubx_reboot() nsapi_error_t err = NSAPI_ERROR_OK; Timer t1; t1.start(); - while (!(t1.read() >= 30)) { + while (!(t1.elapsed_time() >= 30s)) { err = _at.at_cmd_discard("E0", ""); if (err == NSAPI_ERROR_OK) { break; diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp index 3b71537273..d8cec3f7da 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.cpp @@ -177,7 +177,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_sendto_impl(CellularSocket } _at.cmd_start_stop("+USOST", "=", "%d%s%d%d", socket->id, address.get_ip_address(), address.get_port(), size); _at.resp_start("@", true); - rtos::ThisThread::sleep_for(50); //wait for 50ms before sending data + rtos::ThisThread::sleep_for(50ms); _at.write_bytes((uint8_t *)data, size); @@ -201,7 +201,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_sendto_impl(CellularSocket } _at.cmd_start_stop("+USOWR", "=", "%d%d", socket->id, blk); _at.resp_start("@", true); - rtos::ThisThread::sleep_for(50); //wait for 50ms before sending data + rtos::ThisThread::sleep_for(50ms); _at.write_bytes((uint8_t *)buf, blk); @@ -289,7 +289,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke // read() should not fail success = false; } - } else if (timer.read_ms() < SOCKET_TIMEOUT) { + } else if (timer.elapsed_time() < SOCKET_TIMEOUT) { // Wait for URCs _at.process_oob(); } else { @@ -332,7 +332,7 @@ nsapi_size_or_error_t UBLOX_AT_CellularStack::socket_recvfrom_impl(CellularSocke } else { success = false; } - } else if (timer.read_ms() < SOCKET_TIMEOUT) { + } else if (timer.elapsed_time() < SOCKET_TIMEOUT) { // Wait for URCs _at.process_oob(); } else { diff --git a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.h b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.h index ad426a8ccd..d92734c981 100644 --- a/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.h +++ b/features/cellular/framework/targets/UBLOX/AT/UBLOX_AT_CellularStack.h @@ -17,11 +17,14 @@ #ifndef UBLOX_AT_CELLULARSTACK_H_ #define UBLOX_AT_CELLULARSTACK_H_ +#include + #include "AT_CellularStack.h" #include "CellularUtil.h" #include "rtos/ThisThread.h" #include "drivers/Timer.h" +using namespace std::chrono; namespace mbed { @@ -56,7 +59,7 @@ protected: * call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK * and the user has set a larger timeout or full blocking. */ - static const int SOCKET_TIMEOUT = 1000; + static constexpr seconds SOCKET_TIMEOUT = 1s; /** The maximum number of bytes in a packet that can be write/read from * the AT interface in one go. diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp index 7dc3fe1b77..c413b1b2bf 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.cpp @@ -185,7 +185,7 @@ nsapi_size_or_error_t UBLOX_N2XX_CellularStack::socket_recvfrom_impl(CellularSoc // read() should not fail success = false; } - } else if (timer.read_ms() < SOCKET_TIMEOUT) { + } else if (timer.elapsed_time() < SOCKET_TIMEOUT) { // Wait for URCs _at.process_oob(); } else { diff --git a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h index 8e9e1e5315..4eaa392dc5 100644 --- a/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h +++ b/features/cellular/framework/targets/UBLOX/N2XX/UBLOX_N2XX_CellularStack.h @@ -17,11 +17,15 @@ #ifndef UBLOX_N2XX_CELLULARSTACK_H_ #define UBLOX_N2XX_CELLULARSTACK_H_ +#include + #include "AT_CellularStack.h" #include "CellularUtil.h" #include "mbed_wait_api.h" #include "drivers/Timer.h" +using namespace std::chrono; + namespace mbed { class UBLOX_N2XX_CellularStack : public AT_CellularStack { @@ -45,7 +49,7 @@ protected: * call to the functions here when they return NSAPI_ERROR_WOULD_BLOCK * and the user has set a larger timeout or full blocking. */ - static const int SOCKET_TIMEOUT = 1000; + static constexpr seconds SOCKET_TIMEOUT = 1s; /** The maximum number of bytes in a packet that can be write/read from * the AT interface in one go. diff --git a/features/frameworks/utest/source/utest_shim.cpp b/features/frameworks/utest/source/utest_shim.cpp index 558e39a483..e223a753f9 100644 --- a/features/frameworks/utest/source/utest_shim.cpp +++ b/features/frameworks/utest/source/utest_shim.cpp @@ -21,6 +21,7 @@ #include "platform/SingletonPtr.h" #include "Timeout.h" using mbed::Timeout; +using namespace std::chrono; // only one callback is active at any given time static volatile utest_v1_harness_callback_t minimal_callback; @@ -46,13 +47,11 @@ static int32_t utest_us_ticker_init() static void *utest_us_ticker_post(const utest_v1_harness_callback_t callback, timestamp_t delay_ms) { UTEST_LOG_FUNCTION(); - timestamp_t delay_us = delay_ms *1000; if (delay_ms) { ticker_callback = callback; // fire the interrupt in 1000us * delay_ms - utest_timeout_object->attach_us(ticker_handler, delay_us); - + utest_timeout_object->attach(ticker_handler, milliseconds(delay_ms)); } else { minimal_callback = callback; diff --git a/features/lorawan/system/LoRaWANTimer.cpp b/features/lorawan/system/LoRaWANTimer.cpp index a91424b96d..d17b96b49e 100644 --- a/features/lorawan/system/LoRaWANTimer.cpp +++ b/features/lorawan/system/LoRaWANTimer.cpp @@ -20,6 +20,8 @@ SPDX-License-Identifier: BSD-3-Clause #include "LoRaWANTimer.h" +using namespace std::chrono; + LoRaWANTimeHandler::LoRaWANTimeHandler() : _queue(NULL) { @@ -53,7 +55,7 @@ void LoRaWANTimeHandler::init(timer_event_t &obj, mbed::Callback callbac void LoRaWANTimeHandler::start(timer_event_t &obj, const uint32_t timeout) { - obj.timer_id = _queue->call_in(timeout, obj.callback); + obj.timer_id = _queue->call_in(milliseconds(timeout), obj.callback); MBED_ASSERT(obj.timer_id != 0); } diff --git a/features/nanostack/mbed-mesh-api/source/NanostackEthernetInterface.cpp b/features/nanostack/mbed-mesh-api/source/NanostackEthernetInterface.cpp index 3ad3e3fa39..189671d00d 100644 --- a/features/nanostack/mbed-mesh-api/source/NanostackEthernetInterface.cpp +++ b/features/nanostack/mbed-mesh-api/source/NanostackEthernetInterface.cpp @@ -13,11 +13,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include "NanostackEthernetInterface.h" #include "mesh_system.h" #include "callback_handler.h" #include "enet_tasklet.h" +using namespace std::chrono_literals; + nsapi_error_t Nanostack::EthernetInterface::initialize() { nanostack_lock(); @@ -78,7 +81,7 @@ nsapi_error_t Nanostack::EthernetInterface::bringup(bool dhcp, const char *ip, } if (blocking) { - bool acquired = connect_semaphore.try_acquire_for(30000); + bool acquired = connect_semaphore.try_acquire_for(30s); if (!acquired) { return NSAPI_ERROR_DHCP_FAILURE; // sort of... @@ -112,9 +115,9 @@ nsapi_error_t Nanostack::EthernetInterface::bringdown() } if (_blocking) { - int32_t count = disconnect_semaphore.try_acquire_for(30000); + bool acquired = disconnect_semaphore.try_acquire_for(30s); - if (count <= 0) { + if (!acquired) { return NSAPI_ERROR_TIMEOUT; } } diff --git a/features/nanostack/mbed-mesh-api/source/NanostackPPPInterface.cpp b/features/nanostack/mbed-mesh-api/source/NanostackPPPInterface.cpp index 948ec58bd2..70893ba543 100644 --- a/features/nanostack/mbed-mesh-api/source/NanostackPPPInterface.cpp +++ b/features/nanostack/mbed-mesh-api/source/NanostackPPPInterface.cpp @@ -26,6 +26,8 @@ #include "enet_tasklet.h" #include "ip6string.h" +using namespace std::chrono_literals; + class PPPPhy : public NanostackPPPPhy { public: PPPPhy(NanostackMemoryManager &mem, PPP &m); @@ -86,8 +88,8 @@ nsapi_error_t Nanostack::PPPInterface::bringup(bool dhcp, const char *ip, if (blocking) { uint8_t retries = 10; while (_connect_status != NSAPI_STATUS_GLOBAL_UP) { - int32_t count = connect_semaphore.try_acquire_for(3000); - if (count <= 0 && retries-- == 0) { + bool acquired = connect_semaphore.try_acquire_for(3s); + if (!acquired && retries-- == 0) { return NSAPI_ERROR_DHCP_FAILURE; // sort of... } // Not up until global up @@ -153,9 +155,9 @@ nsapi_error_t Nanostack::PPPInterface::bringdown() } if (_blocking) { - int32_t count = disconnect_semaphore.try_acquire_for(30000); + bool acquired = disconnect_semaphore.try_acquire_for(30s); - if (count <= 0) { + if (!acquired) { return NSAPI_ERROR_TIMEOUT; } } diff --git a/features/netsocket/NetworkStack.cpp b/features/netsocket/NetworkStack.cpp index aad5e54799..d611fd74af 100644 --- a/features/netsocket/NetworkStack.cpp +++ b/features/netsocket/NetworkStack.cpp @@ -24,6 +24,8 @@ // Default NetworkStack operations +using namespace std::chrono; + nsapi_error_t NetworkStack::get_ip_address(SocketAddress *address) { return NSAPI_ERROR_UNSUPPORTED; @@ -197,7 +199,7 @@ nsapi_error_t NetworkStack::call_in(int delay, mbed::Callback func) } if (delay > 0) { - if (event_queue->call_in(delay, func) == 0) { + if (event_queue->call_in(milliseconds(delay), func) == 0) { goto NO_MEM; } } else { diff --git a/features/nfc/source/nfc/NFCController.cpp b/features/nfc/source/nfc/NFCController.cpp index 0e22ff4103..8101558ece 100644 --- a/features/nfc/source/nfc/NFCController.cpp +++ b/features/nfc/source/nfc/NFCController.cpp @@ -222,7 +222,9 @@ void nfc_scheduler_timer_start(nfc_scheduler_timer_t *timer) uint32_t nfc_scheduler_timer_get(nfc_scheduler_timer_t *timer) { Timer *mbed_timer = (Timer *)timer; - return (uint32_t)mbed_timer->read_ms(); + return (uint32_t)std::chrono::duration_cast( + mbed_timer->elapsed_time() + ).count(); } void nfc_scheduler_timer_stop(nfc_scheduler_timer_t *timer) diff --git a/platform/source/mbed_rtc_time.cpp b/platform/source/mbed_rtc_time.cpp index 2465ae2bbc..52a3a240b5 100644 --- a/platform/source/mbed_rtc_time.cpp +++ b/platform/source/mbed_rtc_time.cpp @@ -33,7 +33,7 @@ static void (*_rtc_write)(time_t t) = rtc_write; #include "drivers/LowPowerTimer.h" -#define US_PER_SEC 1000000 +using namespace std::chrono; static SingletonPtr _rtc_lp_timer; static uint64_t _rtc_lp_base; @@ -52,7 +52,7 @@ static int _rtc_lpticker_isenabled(void) static time_t _rtc_lpticker_read(void) { - return _rtc_lp_timer->read_high_resolution_us() / US_PER_SEC + _rtc_lp_base; + return duration_cast(_rtc_lp_timer->elapsed_time()).count() + _rtc_lp_base; } static void _rtc_lpticker_write(time_t t)