From 87b151c8a02b58e4befb9d405991d30c267d05c7 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Thu, 28 Sep 2017 16:36:15 -0500 Subject: [PATCH] Protect Ticker attach with a critical section Add a critical section to attach_us so setting _function and locking deep sleep are done atomically. --- drivers/Ticker.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/drivers/Ticker.h b/drivers/Ticker.h index cc5e5dd9e0..c8f1f9deb3 100644 --- a/drivers/Ticker.h +++ b/drivers/Ticker.h @@ -22,6 +22,7 @@ #include "platform/NonCopyable.h" #include "platform/mbed_sleep.h" #include "hal/lp_ticker_api.h" +#include "platform/mbed_critical.h" namespace mbed { /** \addtogroup drivers */ @@ -113,12 +114,14 @@ public: * */ void attach_us(Callback func, us_timestamp_t t) { + core_util_critical_section_enter(); // lock only for the initial callback setup and this is not low power ticker if(!_function && _lock_deepsleep) { sleep_manager_lock_deep_sleep(); } _function = func; setup(t); + core_util_critical_section_exit(); } /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds