1

Committer:
valeyev
Date:
Tue Mar 13 07:17:50 2018 +0000
Revision:
0:e056ac8fecf8
looking for...

Who changed what in which revision?

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