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