Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

Who changed what in which revision?

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