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