Timeout driver for event at absolute time

Dependents:   demo_TimeoutAbs i2c_lora_slave

Timeout driver for generating event at absolute time, i.e. alarm at 12:00PM instead of alarm in 5 minutes.

This is the same concept as TIMER_ABSTIME in clock_nanosleep().

The purpose is to remove dependency on when attach() is called, so the event occurs at correct time regardless of any latency in calling attach().

This driver doesnt add any functionality over mbed-os library; it only skips the call to ticker_read_us() in Ticker::setup(), which just changes a relative time to absolute time.

example use here.

Committer:
Wayne Roberts
Date:
Mon Nov 26 14:22:55 2018 -0800
Revision:
0:900163d530ae
Child:
1:ae6b29794806
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wayne Roberts 0:900163d530ae 1 #include "drivers/TimerEvent.h"
Wayne Roberts 0:900163d530ae 2 #include "platform/Callback.h"
Wayne Roberts 0:900163d530ae 3 #include "platform/mbed_toolchain.h"
Wayne Roberts 0:900163d530ae 4 #include "platform/NonCopyable.h"
Wayne Roberts 0:900163d530ae 5 #include "platform/mbed_power_mgmt.h"
Wayne Roberts 0:900163d530ae 6 #include "hal/lp_ticker_api.h"
Wayne Roberts 0:900163d530ae 7 #include "platform/mbed_critical.h"
Wayne Roberts 0:900163d530ae 8
Wayne Roberts 0:900163d530ae 9 namespace mbed {
Wayne Roberts 0:900163d530ae 10
Wayne Roberts 0:900163d530ae 11 class TimeoutAbs : public TimerEvent, private NonCopyable<TimeoutAbs> {
Wayne Roberts 0:900163d530ae 12 public:
Wayne Roberts 0:900163d530ae 13 TimeoutAbs() : TimerEvent(), _function(0), _lock_deepsleep(true)
Wayne Roberts 0:900163d530ae 14 {
Wayne Roberts 0:900163d530ae 15 }
Wayne Roberts 0:900163d530ae 16
Wayne Roberts 0:900163d530ae 17 TimeoutAbs(const ticker_data_t* data) : TimerEvent(data), _function(0), _lock_deepsleep(true)
Wayne Roberts 0:900163d530ae 18 {
Wayne Roberts 0:900163d530ae 19 #if DEVICE_LPTICKER
Wayne Roberts 0:900163d530ae 20 _lock_deepsleep = (data != get_lp_ticker_data());
Wayne Roberts 0:900163d530ae 21 #endif
Wayne Roberts 0:900163d530ae 22 }
Wayne Roberts 0:900163d530ae 23
Wayne Roberts 0:900163d530ae 24 virtual ~TimeoutAbs()
Wayne Roberts 0:900163d530ae 25 {
Wayne Roberts 0:900163d530ae 26 detach();
Wayne Roberts 0:900163d530ae 27 }
Wayne Roberts 0:900163d530ae 28
Wayne Roberts 0:900163d530ae 29 /** Detach the function
Wayne Roberts 0:900163d530ae 30 */
Wayne Roberts 0:900163d530ae 31 void detach();
Wayne Roberts 0:900163d530ae 32
Wayne Roberts 0:900163d530ae 33 void attach_us(Callback<void()> func, us_timestamp_t t)
Wayne Roberts 0:900163d530ae 34 {
Wayne Roberts 0:900163d530ae 35 core_util_critical_section_enter();
Wayne Roberts 0:900163d530ae 36 // lock only for the initial callback setup and this is not low power ticker
Wayne Roberts 0:900163d530ae 37 if (!_function && _lock_deepsleep) {
Wayne Roberts 0:900163d530ae 38 sleep_manager_lock_deep_sleep();
Wayne Roberts 0:900163d530ae 39 }
Wayne Roberts 0:900163d530ae 40 _function = func;
Wayne Roberts 0:900163d530ae 41 abs_setup(t);
Wayne Roberts 0:900163d530ae 42 core_util_critical_section_exit();
Wayne Roberts 0:900163d530ae 43 }
Wayne Roberts 0:900163d530ae 44
Wayne Roberts 0:900163d530ae 45 us_timestamp_t read_us(void)
Wayne Roberts 0:900163d530ae 46 {
Wayne Roberts 0:900163d530ae 47 return ticker_read_us(_ticker_data);
Wayne Roberts 0:900163d530ae 48 }
Wayne Roberts 0:900163d530ae 49
Wayne Roberts 0:900163d530ae 50 protected:
Wayne Roberts 0:900163d530ae 51 void abs_setup(us_timestamp_t);
Wayne Roberts 0:900163d530ae 52 virtual void handler();
Wayne Roberts 0:900163d530ae 53 Callback<void()> _function; /**< Callback. */
Wayne Roberts 0:900163d530ae 54 bool _lock_deepsleep; /**< Flag which indicates if deep sleep should be disabled. */
Wayne Roberts 0:900163d530ae 55 };
Wayne Roberts 0:900163d530ae 56
Wayne Roberts 0:900163d530ae 57 } // namespace mbed