Rahul Dahiya / Mbed OS STM32F7 Ethernet
Committer:
rahul_dahiya
Date:
Wed Jan 15 15:57:15 2020 +0530
Revision:
0:fb8047b156bb
STM32F7 LWIP

Who changed what in which revision?

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