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