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