00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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