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:
Fri Dec 07 17:49:32 2018 -0800
Revision:
1:ae6b29794806
Parent:
0:900163d530ae
ifdef for header multiple inclusion

Who changed what in which revision?

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