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