1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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