Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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