Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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