Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/drivers/Ticker.h@3:6fe17b8a6d62, 2021-03-09 (annotated)
- Committer:
- boro
- Date:
- Tue Mar 09 13:10:40 2021 +0000
- Revision:
- 3:6fe17b8a6d62
- Parent:
- 0:4beb2ea291ec
SDBlockDevice added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boro | 0:4beb2ea291ec | 1 | /* mbed Microcontroller Library |
boro | 0:4beb2ea291ec | 2 | * Copyright (c) 2006-2013 ARM Limited |
boro | 0:4beb2ea291ec | 3 | * |
boro | 0:4beb2ea291ec | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
boro | 0:4beb2ea291ec | 5 | * you may not use this file except in compliance with the License. |
boro | 0:4beb2ea291ec | 6 | * You may obtain a copy of the License at |
boro | 0:4beb2ea291ec | 7 | * |
boro | 0:4beb2ea291ec | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
boro | 0:4beb2ea291ec | 9 | * |
boro | 0:4beb2ea291ec | 10 | * Unless required by applicable law or agreed to in writing, software |
boro | 0:4beb2ea291ec | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
boro | 0:4beb2ea291ec | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
boro | 0:4beb2ea291ec | 13 | * See the License for the specific language governing permissions and |
boro | 0:4beb2ea291ec | 14 | * limitations under the License. |
boro | 0:4beb2ea291ec | 15 | */ |
boro | 0:4beb2ea291ec | 16 | #ifndef MBED_TICKER_H |
boro | 0:4beb2ea291ec | 17 | #define MBED_TICKER_H |
boro | 0:4beb2ea291ec | 18 | |
boro | 0:4beb2ea291ec | 19 | #include "drivers/TimerEvent.h" |
boro | 0:4beb2ea291ec | 20 | #include "platform/Callback.h" |
boro | 0:4beb2ea291ec | 21 | #include "platform/mbed_toolchain.h" |
boro | 0:4beb2ea291ec | 22 | #include "platform/NonCopyable.h" |
boro | 0:4beb2ea291ec | 23 | #include "platform/mbed_sleep.h" |
boro | 0:4beb2ea291ec | 24 | #include "hal/lp_ticker_api.h" |
boro | 0:4beb2ea291ec | 25 | #include "platform/mbed_critical.h" |
boro | 0:4beb2ea291ec | 26 | |
boro | 0:4beb2ea291ec | 27 | namespace mbed { |
boro | 0:4beb2ea291ec | 28 | /** \addtogroup drivers */ |
boro | 0:4beb2ea291ec | 29 | |
boro | 0:4beb2ea291ec | 30 | /** A Ticker is used to call a function at a recurring interval |
boro | 0:4beb2ea291ec | 31 | * |
boro | 0:4beb2ea291ec | 32 | * You can use as many separate Ticker objects as you require. |
boro | 0:4beb2ea291ec | 33 | * |
boro | 0:4beb2ea291ec | 34 | * @note Synchronization level: Interrupt safe |
boro | 0:4beb2ea291ec | 35 | * |
boro | 0:4beb2ea291ec | 36 | * Example: |
boro | 0:4beb2ea291ec | 37 | * @code |
boro | 0:4beb2ea291ec | 38 | * // Toggle the blinking led after 5 seconds |
boro | 0:4beb2ea291ec | 39 | * |
boro | 0:4beb2ea291ec | 40 | * #include "mbed.h" |
boro | 0:4beb2ea291ec | 41 | * |
boro | 0:4beb2ea291ec | 42 | * Ticker timer; |
boro | 0:4beb2ea291ec | 43 | * DigitalOut led1(LED1); |
boro | 0:4beb2ea291ec | 44 | * DigitalOut led2(LED2); |
boro | 0:4beb2ea291ec | 45 | * |
boro | 0:4beb2ea291ec | 46 | * int flip = 0; |
boro | 0:4beb2ea291ec | 47 | * |
boro | 0:4beb2ea291ec | 48 | * void attime() { |
boro | 0:4beb2ea291ec | 49 | * flip = !flip; |
boro | 0:4beb2ea291ec | 50 | * } |
boro | 0:4beb2ea291ec | 51 | * |
boro | 0:4beb2ea291ec | 52 | * int main() { |
boro | 0:4beb2ea291ec | 53 | * timer.attach(&attime, 5); |
boro | 0:4beb2ea291ec | 54 | * while(1) { |
boro | 0:4beb2ea291ec | 55 | * if(flip == 0) { |
boro | 0:4beb2ea291ec | 56 | * led1 = !led1; |
boro | 0:4beb2ea291ec | 57 | * } else { |
boro | 0:4beb2ea291ec | 58 | * led2 = !led2; |
boro | 0:4beb2ea291ec | 59 | * } |
boro | 0:4beb2ea291ec | 60 | * wait(0.2); |
boro | 0:4beb2ea291ec | 61 | * } |
boro | 0:4beb2ea291ec | 62 | * } |
boro | 0:4beb2ea291ec | 63 | * @endcode |
boro | 0:4beb2ea291ec | 64 | * @ingroup drivers |
boro | 0:4beb2ea291ec | 65 | */ |
boro | 0:4beb2ea291ec | 66 | class Ticker : public TimerEvent, private NonCopyable<Ticker> { |
boro | 0:4beb2ea291ec | 67 | |
boro | 0:4beb2ea291ec | 68 | public: |
boro | 0:4beb2ea291ec | 69 | Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) { |
boro | 0:4beb2ea291ec | 70 | } |
boro | 0:4beb2ea291ec | 71 | |
boro | 0:4beb2ea291ec | 72 | // When low power ticker is in use, then do not disable deep-sleep. |
boro | 0:4beb2ea291ec | 73 | Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) { |
boro | 0:4beb2ea291ec | 74 | data->interface->init(); |
boro | 0:4beb2ea291ec | 75 | #if DEVICE_LOWPOWERTIMER |
boro | 0:4beb2ea291ec | 76 | _lock_deepsleep = (data != get_lp_ticker_data()); |
boro | 0:4beb2ea291ec | 77 | #endif |
boro | 0:4beb2ea291ec | 78 | } |
boro | 0:4beb2ea291ec | 79 | |
boro | 0:4beb2ea291ec | 80 | /** Attach a function to be called by the Ticker, specifying the interval in seconds |
boro | 0:4beb2ea291ec | 81 | * |
boro | 0:4beb2ea291ec | 82 | * @param func pointer to the function to be called |
boro | 0:4beb2ea291ec | 83 | * @param t the time between calls in seconds |
boro | 0:4beb2ea291ec | 84 | */ |
boro | 0:4beb2ea291ec | 85 | void attach(Callback<void()> func, float t) { |
boro | 0:4beb2ea291ec | 86 | attach_us(func, t * 1000000.0f); |
boro | 0:4beb2ea291ec | 87 | } |
boro | 0:4beb2ea291ec | 88 | |
boro | 0:4beb2ea291ec | 89 | /** Attach a member function to be called by the Ticker, specifying the interval in seconds |
boro | 0:4beb2ea291ec | 90 | * |
boro | 0:4beb2ea291ec | 91 | * @param obj pointer to the object to call the member function on |
boro | 0:4beb2ea291ec | 92 | * @param method pointer to the member function to be called |
boro | 0:4beb2ea291ec | 93 | * @param t the time between calls in seconds |
boro | 0:4beb2ea291ec | 94 | * @deprecated |
boro | 0:4beb2ea291ec | 95 | * The attach function does not support cv-qualifiers. Replaced by |
boro | 0:4beb2ea291ec | 96 | * attach(callback(obj, method), t). |
boro | 0:4beb2ea291ec | 97 | */ |
boro | 0:4beb2ea291ec | 98 | template<typename T, typename M> |
boro | 0:4beb2ea291ec | 99 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
boro | 0:4beb2ea291ec | 100 | "The attach function does not support cv-qualifiers. Replaced by " |
boro | 0:4beb2ea291ec | 101 | "attach(callback(obj, method), t).") |
boro | 0:4beb2ea291ec | 102 | void attach(T *obj, M method, float t) { |
boro | 0:4beb2ea291ec | 103 | attach(callback(obj, method), t); |
boro | 0:4beb2ea291ec | 104 | } |
boro | 0:4beb2ea291ec | 105 | |
boro | 0:4beb2ea291ec | 106 | /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds |
boro | 0:4beb2ea291ec | 107 | * |
boro | 0:4beb2ea291ec | 108 | * @param func pointer to the function to be called |
boro | 0:4beb2ea291ec | 109 | * @param t the time between calls in micro-seconds |
boro | 0:4beb2ea291ec | 110 | * |
boro | 0:4beb2ea291ec | 111 | * @note setting @a t to a value shorter that it takes to process the ticker callback |
boro | 0:4beb2ea291ec | 112 | * will cause the system to hang. Ticker callback will be called constantly with no time |
boro | 0:4beb2ea291ec | 113 | * for threads scheduling. |
boro | 0:4beb2ea291ec | 114 | * |
boro | 0:4beb2ea291ec | 115 | */ |
boro | 0:4beb2ea291ec | 116 | void attach_us(Callback<void()> func, us_timestamp_t t) { |
boro | 0:4beb2ea291ec | 117 | core_util_critical_section_enter(); |
boro | 0:4beb2ea291ec | 118 | // lock only for the initial callback setup and this is not low power ticker |
boro | 0:4beb2ea291ec | 119 | if(!_function && _lock_deepsleep) { |
boro | 0:4beb2ea291ec | 120 | sleep_manager_lock_deep_sleep(); |
boro | 0:4beb2ea291ec | 121 | } |
boro | 0:4beb2ea291ec | 122 | _function = func; |
boro | 0:4beb2ea291ec | 123 | setup(t); |
boro | 0:4beb2ea291ec | 124 | core_util_critical_section_exit(); |
boro | 0:4beb2ea291ec | 125 | } |
boro | 0:4beb2ea291ec | 126 | |
boro | 0:4beb2ea291ec | 127 | /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds |
boro | 0:4beb2ea291ec | 128 | * |
boro | 0:4beb2ea291ec | 129 | * @param obj pointer to the object to call the member function on |
boro | 0:4beb2ea291ec | 130 | * @param method pointer to the member function to be called |
boro | 0:4beb2ea291ec | 131 | * @param t the time between calls in micro-seconds |
boro | 0:4beb2ea291ec | 132 | * @deprecated |
boro | 0:4beb2ea291ec | 133 | * The attach_us function does not support cv-qualifiers. Replaced by |
boro | 0:4beb2ea291ec | 134 | * attach_us(callback(obj, method), t). |
boro | 0:4beb2ea291ec | 135 | */ |
boro | 0:4beb2ea291ec | 136 | template<typename T, typename M> |
boro | 0:4beb2ea291ec | 137 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
boro | 0:4beb2ea291ec | 138 | "The attach_us function does not support cv-qualifiers. Replaced by " |
boro | 0:4beb2ea291ec | 139 | "attach_us(callback(obj, method), t).") |
boro | 0:4beb2ea291ec | 140 | void attach_us(T *obj, M method, us_timestamp_t t) { |
boro | 0:4beb2ea291ec | 141 | attach_us(Callback<void()>(obj, method), t); |
boro | 0:4beb2ea291ec | 142 | } |
boro | 0:4beb2ea291ec | 143 | |
boro | 0:4beb2ea291ec | 144 | virtual ~Ticker() { |
boro | 0:4beb2ea291ec | 145 | detach(); |
boro | 0:4beb2ea291ec | 146 | } |
boro | 0:4beb2ea291ec | 147 | |
boro | 0:4beb2ea291ec | 148 | /** Detach the function |
boro | 0:4beb2ea291ec | 149 | */ |
boro | 0:4beb2ea291ec | 150 | void detach(); |
boro | 0:4beb2ea291ec | 151 | |
boro | 0:4beb2ea291ec | 152 | protected: |
boro | 0:4beb2ea291ec | 153 | void setup(us_timestamp_t t); |
boro | 0:4beb2ea291ec | 154 | virtual void handler(); |
boro | 0:4beb2ea291ec | 155 | |
boro | 0:4beb2ea291ec | 156 | protected: |
boro | 0:4beb2ea291ec | 157 | us_timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ |
boro | 0:4beb2ea291ec | 158 | Callback<void()> _function; /**< Callback. */ |
boro | 0:4beb2ea291ec | 159 | bool _lock_deepsleep; /**< Flag which indicates if deep-sleep should be disabled. */ |
boro | 0:4beb2ea291ec | 160 | }; |
boro | 0:4beb2ea291ec | 161 | |
boro | 0:4beb2ea291ec | 162 | } // namespace mbed |
boro | 0:4beb2ea291ec | 163 | |
boro | 0:4beb2ea291ec | 164 | #endif |