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
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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