mbed-os5 only for TYBLE16
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
rtos/RtosTimer.h@1:9db0e321a9f4, 2019-12-31 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Dec 31 06:02:27 2019 +0000
- Revision:
- 1:9db0e321a9f4
- Parent:
- 0:5b88d5760320
updated based on mbed-os5.15.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:5b88d5760320 | 1 | /* mbed Microcontroller Library |
kenjiArai | 1:9db0e321a9f4 | 2 | * Copyright (c) 2006-2019 ARM Limited |
kenjiArai | 0:5b88d5760320 | 3 | * |
kenjiArai | 0:5b88d5760320 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kenjiArai | 0:5b88d5760320 | 5 | * of this software and associated documentation files (the "Software"), to deal |
kenjiArai | 0:5b88d5760320 | 6 | * in the Software without restriction, including without limitation the rights |
kenjiArai | 0:5b88d5760320 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenjiArai | 0:5b88d5760320 | 8 | * copies of the Software, and to permit persons to whom the Software is |
kenjiArai | 0:5b88d5760320 | 9 | * furnished to do so, subject to the following conditions: |
kenjiArai | 0:5b88d5760320 | 10 | * |
kenjiArai | 0:5b88d5760320 | 11 | * The above copyright notice and this permission notice shall be included in |
kenjiArai | 0:5b88d5760320 | 12 | * all copies or substantial portions of the Software. |
kenjiArai | 0:5b88d5760320 | 13 | * |
kenjiArai | 0:5b88d5760320 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenjiArai | 0:5b88d5760320 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenjiArai | 0:5b88d5760320 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenjiArai | 0:5b88d5760320 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenjiArai | 0:5b88d5760320 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenjiArai | 0:5b88d5760320 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
kenjiArai | 0:5b88d5760320 | 20 | * SOFTWARE. |
kenjiArai | 0:5b88d5760320 | 21 | */ |
kenjiArai | 0:5b88d5760320 | 22 | #ifndef RTOS_TIMER_H |
kenjiArai | 0:5b88d5760320 | 23 | #define RTOS_TIMER_H |
kenjiArai | 0:5b88d5760320 | 24 | |
kenjiArai | 0:5b88d5760320 | 25 | #include <stdint.h> |
kenjiArai | 1:9db0e321a9f4 | 26 | #include "rtos/mbed_rtos_types.h" |
kenjiArai | 1:9db0e321a9f4 | 27 | #include "rtos/mbed_rtos_storage.h" |
kenjiArai | 0:5b88d5760320 | 28 | #include "platform/Callback.h" |
kenjiArai | 0:5b88d5760320 | 29 | #include "platform/NonCopyable.h" |
kenjiArai | 0:5b88d5760320 | 30 | #include "platform/mbed_toolchain.h" |
kenjiArai | 1:9db0e321a9f4 | 31 | #include "rtos/mbed_rtos1_types.h" |
kenjiArai | 1:9db0e321a9f4 | 32 | |
kenjiArai | 1:9db0e321a9f4 | 33 | #if MBED_CONF_RTOS_PRESENT || defined(DOXYGEN_ONLY) |
kenjiArai | 0:5b88d5760320 | 34 | |
kenjiArai | 0:5b88d5760320 | 35 | namespace rtos { |
kenjiArai | 1:9db0e321a9f4 | 36 | /** \addtogroup rtos-public-api */ |
kenjiArai | 0:5b88d5760320 | 37 | /** @{*/ |
kenjiArai | 1:9db0e321a9f4 | 38 | |
kenjiArai | 0:5b88d5760320 | 39 | /** |
kenjiArai | 0:5b88d5760320 | 40 | * \defgroup rtos_RtosTimer RtosTimer class |
kenjiArai | 0:5b88d5760320 | 41 | * @{ |
kenjiArai | 0:5b88d5760320 | 42 | */ |
kenjiArai | 0:5b88d5760320 | 43 | |
kenjiArai | 0:5b88d5760320 | 44 | /** The RtosTimer class allow creating and and controlling of timer functions in the system. |
kenjiArai | 0:5b88d5760320 | 45 | A timer function is called when a time period expires whereby both on-shot and |
kenjiArai | 0:5b88d5760320 | 46 | periodic timers are possible. A timer can be started, restarted, or stopped. |
kenjiArai | 0:5b88d5760320 | 47 | |
kenjiArai | 0:5b88d5760320 | 48 | Timers are handled in the thread osTimerThread. |
kenjiArai | 0:5b88d5760320 | 49 | Callback functions run under control of this thread and may use CMSIS-RTOS API calls. |
kenjiArai | 0:5b88d5760320 | 50 | |
kenjiArai | 0:5b88d5760320 | 51 | @deprecated |
kenjiArai | 0:5b88d5760320 | 52 | The RtosTimer has been superseded by the EventQueue. The RtosTimer and EventQueue duplicate |
kenjiArai | 0:5b88d5760320 | 53 | the functionality of timing events outside of interrupt context, however the EventQueue |
kenjiArai | 0:5b88d5760320 | 54 | has additional features to handle deferring other events to multiple contexts. |
kenjiArai | 0:5b88d5760320 | 55 | |
kenjiArai | 0:5b88d5760320 | 56 | For an example, the following code shows a simple use of the RtosTimer: |
kenjiArai | 0:5b88d5760320 | 57 | @code |
kenjiArai | 0:5b88d5760320 | 58 | DigitalOut led(LED1); |
kenjiArai | 0:5b88d5760320 | 59 | void blink() { |
kenjiArai | 0:5b88d5760320 | 60 | led = !led; |
kenjiArai | 0:5b88d5760320 | 61 | } |
kenjiArai | 0:5b88d5760320 | 62 | |
kenjiArai | 0:5b88d5760320 | 63 | RtosTimer timer(&blink); |
kenjiArai | 0:5b88d5760320 | 64 | int main() { |
kenjiArai | 0:5b88d5760320 | 65 | timer.start(1000); // call blink every 1s |
kenjiArai | 1:9db0e321a9f4 | 66 | ThisThread::sleep_for(5000); |
kenjiArai | 0:5b88d5760320 | 67 | timer.stop(); // stop after 5s |
kenjiArai | 0:5b88d5760320 | 68 | } |
kenjiArai | 0:5b88d5760320 | 69 | @endcode |
kenjiArai | 0:5b88d5760320 | 70 | |
kenjiArai | 0:5b88d5760320 | 71 | This is the above example rewritten to use the EventQueue: |
kenjiArai | 0:5b88d5760320 | 72 | @code |
kenjiArai | 0:5b88d5760320 | 73 | DigitalOut led(LED1); |
kenjiArai | 0:5b88d5760320 | 74 | void blink() { |
kenjiArai | 0:5b88d5760320 | 75 | led = !led; |
kenjiArai | 0:5b88d5760320 | 76 | } |
kenjiArai | 0:5b88d5760320 | 77 | |
kenjiArai | 0:5b88d5760320 | 78 | EventQueue queue(4*EVENTS_EVENT_SIZE); |
kenjiArai | 0:5b88d5760320 | 79 | int main() { |
kenjiArai | 0:5b88d5760320 | 80 | int blink_id = queue.call_every(1000, &blink); // call blink every 1s |
kenjiArai | 0:5b88d5760320 | 81 | queue.dispatch(5000); |
kenjiArai | 0:5b88d5760320 | 82 | queue.cancel(blink_id); // stop after 5s |
kenjiArai | 0:5b88d5760320 | 83 | } |
kenjiArai | 0:5b88d5760320 | 84 | @endcode |
kenjiArai | 0:5b88d5760320 | 85 | |
kenjiArai | 0:5b88d5760320 | 86 | @note |
kenjiArai | 0:5b88d5760320 | 87 | Memory considerations: The timer control structures will be created on current thread's stack, both for the mbed OS |
kenjiArai | 0:5b88d5760320 | 88 | and underlying RTOS objects (static or dynamic RTOS memory pools are not being used). |
kenjiArai | 0:5b88d5760320 | 89 | */ |
kenjiArai | 0:5b88d5760320 | 90 | class RtosTimer : private mbed::NonCopyable<RtosTimer> { |
kenjiArai | 0:5b88d5760320 | 91 | public: |
kenjiArai | 0:5b88d5760320 | 92 | /** Create timer. |
kenjiArai | 0:5b88d5760320 | 93 | @param func function to be executed by this timer. |
kenjiArai | 0:5b88d5760320 | 94 | @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic) |
kenjiArai | 1:9db0e321a9f4 | 95 | @param argument argument to the timer call back function. (default: nullptr) |
kenjiArai | 0:5b88d5760320 | 96 | @deprecated Replaced with RtosTimer(Callback<void()>, os_timer_type) |
kenjiArai | 0:5b88d5760320 | 97 | @deprecated |
kenjiArai | 0:5b88d5760320 | 98 | The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details |
kenjiArai | 0:5b88d5760320 | 99 | |
kenjiArai | 0:5b88d5760320 | 100 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 101 | */ |
kenjiArai | 0:5b88d5760320 | 102 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
kenjiArai | 0:5b88d5760320 | 103 | "Replaced with RtosTimer(Callback<void()>, os_timer_type)") |
kenjiArai | 0:5b88d5760320 | 104 | MBED_DEPRECATED_SINCE("mbed-os-5.2", |
kenjiArai | 0:5b88d5760320 | 105 | "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details") |
kenjiArai | 1:9db0e321a9f4 | 106 | RtosTimer(void (*func)(void const *argument), os_timer_type type = osTimerPeriodic, void *argument = nullptr) |
kenjiArai | 0:5b88d5760320 | 107 | { |
kenjiArai | 0:5b88d5760320 | 108 | constructor(mbed::callback((void (*)(void *))func, argument), type); |
kenjiArai | 0:5b88d5760320 | 109 | } |
kenjiArai | 0:5b88d5760320 | 110 | |
kenjiArai | 0:5b88d5760320 | 111 | /** Create timer. |
kenjiArai | 0:5b88d5760320 | 112 | @param func function to be executed by this timer. |
kenjiArai | 0:5b88d5760320 | 113 | @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic) |
kenjiArai | 0:5b88d5760320 | 114 | @deprecated |
kenjiArai | 0:5b88d5760320 | 115 | The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details |
kenjiArai | 0:5b88d5760320 | 116 | |
kenjiArai | 0:5b88d5760320 | 117 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 118 | */ |
kenjiArai | 0:5b88d5760320 | 119 | MBED_DEPRECATED_SINCE("mbed-os-5.2", |
kenjiArai | 0:5b88d5760320 | 120 | "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details") |
kenjiArai | 0:5b88d5760320 | 121 | RtosTimer(mbed::Callback<void()> func, os_timer_type type = osTimerPeriodic) |
kenjiArai | 0:5b88d5760320 | 122 | { |
kenjiArai | 0:5b88d5760320 | 123 | constructor(func, type); |
kenjiArai | 0:5b88d5760320 | 124 | } |
kenjiArai | 0:5b88d5760320 | 125 | |
kenjiArai | 0:5b88d5760320 | 126 | /** Create timer. |
kenjiArai | 0:5b88d5760320 | 127 | @param obj pointer to the object to call the member function on. |
kenjiArai | 0:5b88d5760320 | 128 | @param method member function to be executed by this timer. |
kenjiArai | 0:5b88d5760320 | 129 | @param type osTimerOnce for one-shot or osTimerPeriodic for periodic behavior. (default: osTimerPeriodic) |
kenjiArai | 0:5b88d5760320 | 130 | @deprecated |
kenjiArai | 0:5b88d5760320 | 131 | The RtosTimer constructor does not support cv-qualifiers. Replaced by |
kenjiArai | 0:5b88d5760320 | 132 | RtosTimer(callback(obj, method), os_timer_type). |
kenjiArai | 0:5b88d5760320 | 133 | @deprecated |
kenjiArai | 0:5b88d5760320 | 134 | The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details |
kenjiArai | 0:5b88d5760320 | 135 | |
kenjiArai | 0:5b88d5760320 | 136 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 137 | */ |
kenjiArai | 0:5b88d5760320 | 138 | template <typename T, typename M> |
kenjiArai | 0:5b88d5760320 | 139 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
kenjiArai | 0:5b88d5760320 | 140 | "The RtosTimer constructor does not support cv-qualifiers. Replaced by " |
kenjiArai | 0:5b88d5760320 | 141 | "RtosTimer(callback(obj, method), os_timer_type).") |
kenjiArai | 0:5b88d5760320 | 142 | MBED_DEPRECATED_SINCE("mbed-os-5.2", |
kenjiArai | 0:5b88d5760320 | 143 | "The RtosTimer has been superseded by the EventQueue. See RtosTimer.h for more details") |
kenjiArai | 0:5b88d5760320 | 144 | RtosTimer(T *obj, M method, os_timer_type type = osTimerPeriodic) |
kenjiArai | 0:5b88d5760320 | 145 | { |
kenjiArai | 0:5b88d5760320 | 146 | constructor(mbed::callback(obj, method), type); |
kenjiArai | 0:5b88d5760320 | 147 | } |
kenjiArai | 0:5b88d5760320 | 148 | |
kenjiArai | 0:5b88d5760320 | 149 | /** Stop the timer. |
kenjiArai | 0:5b88d5760320 | 150 | @return status code that indicates the execution status of the function: |
kenjiArai | 0:5b88d5760320 | 151 | @a osOK the timer has been stopped. |
kenjiArai | 0:5b88d5760320 | 152 | @a osErrorISR @a stop cannot be called from interrupt service routines. |
kenjiArai | 0:5b88d5760320 | 153 | @a osErrorParameter internal error. |
kenjiArai | 0:5b88d5760320 | 154 | @a osErrorResource the timer is not running. |
kenjiArai | 0:5b88d5760320 | 155 | |
kenjiArai | 0:5b88d5760320 | 156 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 157 | */ |
kenjiArai | 0:5b88d5760320 | 158 | osStatus stop(void); |
kenjiArai | 0:5b88d5760320 | 159 | |
kenjiArai | 0:5b88d5760320 | 160 | /** Start or restart the timer. |
kenjiArai | 0:5b88d5760320 | 161 | @param millisec non-zero value of the timer. |
kenjiArai | 0:5b88d5760320 | 162 | @return status code that indicates the execution status of the function: |
kenjiArai | 0:5b88d5760320 | 163 | @a osOK the timer has been started or restarted. |
kenjiArai | 0:5b88d5760320 | 164 | @a osErrorISR @a start cannot be called from interrupt service routines. |
kenjiArai | 0:5b88d5760320 | 165 | @a osErrorParameter internal error or incorrect parameter value. |
kenjiArai | 0:5b88d5760320 | 166 | @a osErrorResource internal error (the timer is in an invalid timer state). |
kenjiArai | 0:5b88d5760320 | 167 | |
kenjiArai | 0:5b88d5760320 | 168 | @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 169 | */ |
kenjiArai | 0:5b88d5760320 | 170 | osStatus start(uint32_t millisec); |
kenjiArai | 0:5b88d5760320 | 171 | |
kenjiArai | 0:5b88d5760320 | 172 | /** RtosTimer destructor |
kenjiArai | 0:5b88d5760320 | 173 | * |
kenjiArai | 0:5b88d5760320 | 174 | * @note You cannot call this function from ISR context. |
kenjiArai | 0:5b88d5760320 | 175 | */ |
kenjiArai | 0:5b88d5760320 | 176 | ~RtosTimer(); |
kenjiArai | 0:5b88d5760320 | 177 | |
kenjiArai | 0:5b88d5760320 | 178 | private: |
kenjiArai | 0:5b88d5760320 | 179 | // Required to share definitions without |
kenjiArai | 0:5b88d5760320 | 180 | // delegated constructors |
kenjiArai | 0:5b88d5760320 | 181 | void constructor(mbed::Callback<void()> func, os_timer_type type); |
kenjiArai | 0:5b88d5760320 | 182 | |
kenjiArai | 0:5b88d5760320 | 183 | osTimerId_t _id; |
kenjiArai | 0:5b88d5760320 | 184 | mbed_rtos_storage_timer_t _obj_mem; |
kenjiArai | 0:5b88d5760320 | 185 | mbed::Callback<void()> _function; |
kenjiArai | 0:5b88d5760320 | 186 | }; |
kenjiArai | 0:5b88d5760320 | 187 | /** @}*/ |
kenjiArai | 0:5b88d5760320 | 188 | /** @}*/ |
kenjiArai | 0:5b88d5760320 | 189 | |
kenjiArai | 0:5b88d5760320 | 190 | } |
kenjiArai | 0:5b88d5760320 | 191 | |
kenjiArai | 0:5b88d5760320 | 192 | #endif |
kenjiArai | 0:5b88d5760320 | 193 | |
kenjiArai | 1:9db0e321a9f4 | 194 | #endif |