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