Daniel Vizcaya / Mbed OS 04_RTOS_Embebidos
Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

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-2013 ARM Limited
Bethory 0:6ad07c9019fd 3 *
Bethory 0:6ad07c9019fd 4 * Licensed under the Apache License, Version 2.0 (the "License");
Bethory 0:6ad07c9019fd 5 * you may not use this file except in compliance with the License.
Bethory 0:6ad07c9019fd 6 * You may obtain a copy of the License at
Bethory 0:6ad07c9019fd 7 *
Bethory 0:6ad07c9019fd 8 * http://www.apache.org/licenses/LICENSE-2.0
Bethory 0:6ad07c9019fd 9 *
Bethory 0:6ad07c9019fd 10 * Unless required by applicable law or agreed to in writing, software
Bethory 0:6ad07c9019fd 11 * distributed under the License is distributed on an "AS IS" BASIS,
Bethory 0:6ad07c9019fd 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Bethory 0:6ad07c9019fd 13 * See the License for the specific language governing permissions and
Bethory 0:6ad07c9019fd 14 * limitations under the License.
Bethory 0:6ad07c9019fd 15 */
Bethory 0:6ad07c9019fd 16 #ifndef MBED_TICKER_H
Bethory 0:6ad07c9019fd 17 #define MBED_TICKER_H
Bethory 0:6ad07c9019fd 18
Bethory 0:6ad07c9019fd 19 #include "drivers/TimerEvent.h"
Bethory 0:6ad07c9019fd 20 #include "platform/Callback.h"
Bethory 0:6ad07c9019fd 21 #include "platform/mbed_toolchain.h"
Bethory 0:6ad07c9019fd 22 #include "platform/NonCopyable.h"
Bethory 0:6ad07c9019fd 23 #include "platform/mbed_power_mgmt.h"
Bethory 0:6ad07c9019fd 24 #include "hal/lp_ticker_api.h"
Bethory 0:6ad07c9019fd 25 #include "platform/mbed_critical.h"
Bethory 0:6ad07c9019fd 26
Bethory 0:6ad07c9019fd 27 namespace mbed {
Bethory 0:6ad07c9019fd 28 /** \addtogroup drivers */
Bethory 0:6ad07c9019fd 29
Bethory 0:6ad07c9019fd 30 /** A Ticker is used to call a function at a recurring interval
Bethory 0:6ad07c9019fd 31 *
Bethory 0:6ad07c9019fd 32 * You can use as many separate Ticker objects as you require.
Bethory 0:6ad07c9019fd 33 *
Bethory 0:6ad07c9019fd 34 * @note Synchronization level: Interrupt safe
Bethory 0:6ad07c9019fd 35 *
Bethory 0:6ad07c9019fd 36 * Example:
Bethory 0:6ad07c9019fd 37 * @code
Bethory 0:6ad07c9019fd 38 * // Toggle the blinking led after 5 seconds
Bethory 0:6ad07c9019fd 39 *
Bethory 0:6ad07c9019fd 40 * #include "mbed.h"
Bethory 0:6ad07c9019fd 41 *
Bethory 0:6ad07c9019fd 42 * Ticker timer;
Bethory 0:6ad07c9019fd 43 * DigitalOut led1(LED1);
Bethory 0:6ad07c9019fd 44 * DigitalOut led2(LED2);
Bethory 0:6ad07c9019fd 45 *
Bethory 0:6ad07c9019fd 46 * int flip = 0;
Bethory 0:6ad07c9019fd 47 *
Bethory 0:6ad07c9019fd 48 * void attime() {
Bethory 0:6ad07c9019fd 49 * flip = !flip;
Bethory 0:6ad07c9019fd 50 * }
Bethory 0:6ad07c9019fd 51 *
Bethory 0:6ad07c9019fd 52 * int main() {
Bethory 0:6ad07c9019fd 53 * timer.attach(&attime, 5);
Bethory 0:6ad07c9019fd 54 * while(1) {
Bethory 0:6ad07c9019fd 55 * if(flip == 0) {
Bethory 0:6ad07c9019fd 56 * led1 = !led1;
Bethory 0:6ad07c9019fd 57 * } else {
Bethory 0:6ad07c9019fd 58 * led2 = !led2;
Bethory 0:6ad07c9019fd 59 * }
Bethory 0:6ad07c9019fd 60 * wait(0.2);
Bethory 0:6ad07c9019fd 61 * }
Bethory 0:6ad07c9019fd 62 * }
Bethory 0:6ad07c9019fd 63 * @endcode
Bethory 0:6ad07c9019fd 64 * @ingroup drivers
Bethory 0:6ad07c9019fd 65 */
Bethory 0:6ad07c9019fd 66 class Ticker : public TimerEvent, private NonCopyable<Ticker> {
Bethory 0:6ad07c9019fd 67
Bethory 0:6ad07c9019fd 68 public:
Bethory 0:6ad07c9019fd 69 Ticker() : TimerEvent(), _function(0), _lock_deepsleep(true) {
Bethory 0:6ad07c9019fd 70 }
Bethory 0:6ad07c9019fd 71
Bethory 0:6ad07c9019fd 72 // When low power ticker is in use, then do not disable deep-sleep.
Bethory 0:6ad07c9019fd 73 Ticker(const ticker_data_t *data) : TimerEvent(data), _function(0), _lock_deepsleep(true) {
Bethory 0:6ad07c9019fd 74 #if DEVICE_LOWPOWERTIMER
Bethory 0:6ad07c9019fd 75 _lock_deepsleep = (data != get_lp_ticker_data());
Bethory 0:6ad07c9019fd 76 #endif
Bethory 0:6ad07c9019fd 77 }
Bethory 0:6ad07c9019fd 78
Bethory 0:6ad07c9019fd 79 /** Attach a function to be called by the Ticker, specifying the interval in seconds
Bethory 0:6ad07c9019fd 80 *
Bethory 0:6ad07c9019fd 81 * @param func pointer to the function to be called
Bethory 0:6ad07c9019fd 82 * @param t the time between calls in seconds
Bethory 0:6ad07c9019fd 83 */
Bethory 0:6ad07c9019fd 84 void attach(Callback<void()> func, float t) {
Bethory 0:6ad07c9019fd 85 attach_us(func, t * 1000000.0f);
Bethory 0:6ad07c9019fd 86 }
Bethory 0:6ad07c9019fd 87
Bethory 0:6ad07c9019fd 88 /** Attach a member function to be called by the Ticker, specifying the interval in seconds
Bethory 0:6ad07c9019fd 89 *
Bethory 0:6ad07c9019fd 90 * @param obj pointer to the object to call the member function on
Bethory 0:6ad07c9019fd 91 * @param method pointer to the member function to be called
Bethory 0:6ad07c9019fd 92 * @param t the time between calls in seconds
Bethory 0:6ad07c9019fd 93 * @deprecated
Bethory 0:6ad07c9019fd 94 * The attach function does not support cv-qualifiers. Replaced by
Bethory 0:6ad07c9019fd 95 * attach(callback(obj, method), t).
Bethory 0:6ad07c9019fd 96 */
Bethory 0:6ad07c9019fd 97 template<typename T, typename M>
Bethory 0:6ad07c9019fd 98 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Bethory 0:6ad07c9019fd 99 "The attach function does not support cv-qualifiers. Replaced by "
Bethory 0:6ad07c9019fd 100 "attach(callback(obj, method), t).")
Bethory 0:6ad07c9019fd 101 void attach(T *obj, M method, float t) {
Bethory 0:6ad07c9019fd 102 attach(callback(obj, method), t);
Bethory 0:6ad07c9019fd 103 }
Bethory 0:6ad07c9019fd 104
Bethory 0:6ad07c9019fd 105 /** Attach a function to be called by the Ticker, specifying the interval in micro-seconds
Bethory 0:6ad07c9019fd 106 *
Bethory 0:6ad07c9019fd 107 * @param func pointer to the function to be called
Bethory 0:6ad07c9019fd 108 * @param t the time between calls in micro-seconds
Bethory 0:6ad07c9019fd 109 *
Bethory 0:6ad07c9019fd 110 * @note setting @a t to a value shorter that it takes to process the ticker callback
Bethory 0:6ad07c9019fd 111 * will cause the system to hang. Ticker callback will be called constantly with no time
Bethory 0:6ad07c9019fd 112 * for threads scheduling.
Bethory 0:6ad07c9019fd 113 *
Bethory 0:6ad07c9019fd 114 */
Bethory 0:6ad07c9019fd 115 void attach_us(Callback<void()> func, us_timestamp_t t) {
Bethory 0:6ad07c9019fd 116 core_util_critical_section_enter();
Bethory 0:6ad07c9019fd 117 // lock only for the initial callback setup and this is not low power ticker
Bethory 0:6ad07c9019fd 118 if(!_function && _lock_deepsleep) {
Bethory 0:6ad07c9019fd 119 sleep_manager_lock_deep_sleep();
Bethory 0:6ad07c9019fd 120 }
Bethory 0:6ad07c9019fd 121 _function = func;
Bethory 0:6ad07c9019fd 122 setup(t);
Bethory 0:6ad07c9019fd 123 core_util_critical_section_exit();
Bethory 0:6ad07c9019fd 124 }
Bethory 0:6ad07c9019fd 125
Bethory 0:6ad07c9019fd 126 /** Attach a member function to be called by the Ticker, specifying the interval in micro-seconds
Bethory 0:6ad07c9019fd 127 *
Bethory 0:6ad07c9019fd 128 * @param obj pointer to the object to call the member function on
Bethory 0:6ad07c9019fd 129 * @param method pointer to the member function to be called
Bethory 0:6ad07c9019fd 130 * @param t the time between calls in micro-seconds
Bethory 0:6ad07c9019fd 131 * @deprecated
Bethory 0:6ad07c9019fd 132 * The attach_us function does not support cv-qualifiers. Replaced by
Bethory 0:6ad07c9019fd 133 * attach_us(callback(obj, method), t).
Bethory 0:6ad07c9019fd 134 */
Bethory 0:6ad07c9019fd 135 template<typename T, typename M>
Bethory 0:6ad07c9019fd 136 MBED_DEPRECATED_SINCE("mbed-os-5.1",
Bethory 0:6ad07c9019fd 137 "The attach_us function does not support cv-qualifiers. Replaced by "
Bethory 0:6ad07c9019fd 138 "attach_us(callback(obj, method), t).")
Bethory 0:6ad07c9019fd 139 void attach_us(T *obj, M method, us_timestamp_t t) {
Bethory 0:6ad07c9019fd 140 attach_us(Callback<void()>(obj, method), t);
Bethory 0:6ad07c9019fd 141 }
Bethory 0:6ad07c9019fd 142
Bethory 0:6ad07c9019fd 143 virtual ~Ticker() {
Bethory 0:6ad07c9019fd 144 detach();
Bethory 0:6ad07c9019fd 145 }
Bethory 0:6ad07c9019fd 146
Bethory 0:6ad07c9019fd 147 /** Detach the function
Bethory 0:6ad07c9019fd 148 */
Bethory 0:6ad07c9019fd 149 void detach();
Bethory 0:6ad07c9019fd 150
Bethory 0:6ad07c9019fd 151 protected:
Bethory 0:6ad07c9019fd 152 void setup(us_timestamp_t t);
Bethory 0:6ad07c9019fd 153 virtual void handler();
Bethory 0:6ad07c9019fd 154
Bethory 0:6ad07c9019fd 155 protected:
Bethory 0:6ad07c9019fd 156 us_timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
Bethory 0:6ad07c9019fd 157 Callback<void()> _function; /**< Callback. */
Bethory 0:6ad07c9019fd 158 bool _lock_deepsleep; /**< Flag which indicates if deep-sleep should be disabled. */
Bethory 0:6ad07c9019fd 159 };
Bethory 0:6ad07c9019fd 160
Bethory 0:6ad07c9019fd 161 } // namespace mbed
Bethory 0:6ad07c9019fd 162
Bethory 0:6ad07c9019fd 163 #endif