inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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