mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
mbed library release version 165

Who changed what in which revision?

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