From e5aa84308f9e57a0bcfa0f5440f9ba25b4a37739 Mon Sep 17 00:00:00 2001 From: Steve Cartmell Date: Thu, 22 Nov 2018 17:34:40 +0000 Subject: [PATCH] refactor(pan-cordio): Use Timer class instead of raw ticker for timing --- .../targets/TARGET_CORDIO/CordioBLE.h | 2 ++ .../targets/TARGET_CORDIO/source/CordioBLE.cpp | 17 ++++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/features/FEATURE_BLE/targets/TARGET_CORDIO/CordioBLE.h b/features/FEATURE_BLE/targets/TARGET_CORDIO/CordioBLE.h index fe3cc0e348..8a4428bef8 100644 --- a/features/FEATURE_BLE/targets/TARGET_CORDIO/CordioBLE.h +++ b/features/FEATURE_BLE/targets/TARGET_CORDIO/CordioBLE.h @@ -31,6 +31,7 @@ #include "ble/generic/GenericGap.h" #include "ble/generic/GenericSecurityManager.h" #include "SimpleEventQueue.h" +#include "Timer.h" namespace ble { namespace vendor { @@ -153,6 +154,7 @@ private: ::BLE::InstanceID_t instanceID; mutable SimpleEventQueue _event_queue; + mbed::Timer _timer; class SigningEventMonitorProxy : public pal::SigningEventMonitor { public: diff --git a/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioBLE.cpp b/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioBLE.cpp index c13b0228f9..e314b5c328 100644 --- a/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioBLE.cpp +++ b/features/FEATURE_BLE/targets/TARGET_CORDIO/source/CordioBLE.cpp @@ -121,6 +121,8 @@ ble_error_t BLE::init( { switch (initialization_status) { case NOT_INITIALIZED: + _timer.reset(); + _timer.start(); _event_queue.initialize(this, instanceID); _init_callback = initCallback; start_stack_reset(); @@ -400,15 +402,16 @@ void BLE::callDispatcher() _event_queue.process(); // follow by stack events - static uint32_t lastTimeUs = us_ticker_read(); - uint32_t currTimeUs, deltaTimeMs; + static us_timestamp_t last_time_us = _timer.read_high_resolution_us(); // Update the current cordio time - currTimeUs = us_ticker_read(); - deltaTimeMs = (currTimeUs - lastTimeUs) / 1000; - if (deltaTimeMs > 0) { - WsfTimerUpdate(deltaTimeMs / WSF_MS_PER_TICK); - lastTimeUs += deltaTimeMs * 1000; + us_timestamp_t curr_time_us = _timer.read_high_resolution_us(); + uint64_t delta_time_ms = ((curr_time_us - last_time_us) / 1000); + + if (delta_time_ms > 0) { + WsfTimerUpdate(delta_time_ms / WSF_MS_PER_TICK); + + last_time_us += (delta_time_ms * 1000); } wsfOsDispatcher();