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