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