Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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