Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed by
Ticker.h@122:f9eeca106725, 2016-07-07 (annotated)
- Committer:
- Kojto
- Date:
- Thu Jul 07 14:34:11 2016 +0100
- Revision:
- 122:f9eeca106725
- Parent:
- 100:cbbeb26dbd92
- Child:
- 125:2e9cc70d1897
Release 122 of the mbed library
Changes:
- new targets - Nucleo L432KC, Beetle, Nucleo F446ZE, Nucleo L011K4
- Thread safety addition - mbed API should contain a statement about thread safety
- critical section API addition
- CAS API (core_util_atomic_incr/decr)
- DEVICE_ are generated from targets.json file, device.h deprecated
- Callback replaces FunctionPointer to provide std like interface
- mbed HAL API docs improvements
- toolchain - prexif attributes with MBED_
- add new attributes - packed, weak, forcedinline, align
- target.json - contains targets definitions
- ST - L1XX - Cube update to 1.5
- SPI clock selection fix (clock from APB domain)
- F7 - Cube update v1.4.0
- L0 - baudrate init fix
- L1 - Cube update v1.5
- F3 - baudrate init fix, 3 targets CAN support
- F4 - Cube update v1.12.0, 3 targets CAN support
- L4XX - Cube update v1.5.1
- F0 - update Cube to v1.5.0
- L4 - 2 targets (L476RG/VG) CAN support
- NXP - pwm clock fix for KSDK2 MCU
- LPC2368 - remove ARM toolchain support - due to regression
- KSDK2 - fix SPI , I2C address and repeat start
- Silabs - some fixes backported from mbed 3
- Renesas - RZ_A1H - SystemCoreClockUpdate addition
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| bogdanm | 65:5798e58a58b1 | 1 | /* mbed Microcontroller Library |
| bogdanm | 65:5798e58a58b1 | 2 | * Copyright (c) 2006-2013 ARM Limited |
| bogdanm | 65:5798e58a58b1 | 3 | * |
| bogdanm | 65:5798e58a58b1 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| bogdanm | 65:5798e58a58b1 | 5 | * you may not use this file except in compliance with the License. |
| bogdanm | 65:5798e58a58b1 | 6 | * You may obtain a copy of the License at |
| bogdanm | 65:5798e58a58b1 | 7 | * |
| bogdanm | 65:5798e58a58b1 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| bogdanm | 65:5798e58a58b1 | 9 | * |
| bogdanm | 65:5798e58a58b1 | 10 | * Unless required by applicable law or agreed to in writing, software |
| bogdanm | 65:5798e58a58b1 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| bogdanm | 65:5798e58a58b1 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| bogdanm | 65:5798e58a58b1 | 13 | * See the License for the specific language governing permissions and |
| bogdanm | 65:5798e58a58b1 | 14 | * limitations under the License. |
| bogdanm | 65:5798e58a58b1 | 15 | */ |
| bogdanm | 65:5798e58a58b1 | 16 | #ifndef MBED_TICKER_H |
| bogdanm | 65:5798e58a58b1 | 17 | #define MBED_TICKER_H |
| bogdanm | 65:5798e58a58b1 | 18 | |
| bogdanm | 65:5798e58a58b1 | 19 | #include "TimerEvent.h" |
| Kojto | 122:f9eeca106725 | 20 | #include "Callback.h" |
| bogdanm | 65:5798e58a58b1 | 21 | |
| bogdanm | 65:5798e58a58b1 | 22 | namespace mbed { |
| bogdanm | 65:5798e58a58b1 | 23 | |
| bogdanm | 65:5798e58a58b1 | 24 | /** A Ticker is used to call a function at a recurring interval |
| bogdanm | 65:5798e58a58b1 | 25 | * |
| bogdanm | 65:5798e58a58b1 | 26 | * You can use as many seperate Ticker objects as you require. |
| bogdanm | 65:5798e58a58b1 | 27 | * |
| Kojto | 122:f9eeca106725 | 28 | * @Note Synchronization level: Interrupt safe |
| Kojto | 122:f9eeca106725 | 29 | * |
| bogdanm | 65:5798e58a58b1 | 30 | * Example: |
| bogdanm | 65:5798e58a58b1 | 31 | * @code |
| bogdanm | 65:5798e58a58b1 | 32 | * // Toggle the blinking led after 5 seconds |
| bogdanm | 65:5798e58a58b1 | 33 | * |
| bogdanm | 65:5798e58a58b1 | 34 | * #include "mbed.h" |
| bogdanm | 65:5798e58a58b1 | 35 | * |
| bogdanm | 65:5798e58a58b1 | 36 | * Ticker timer; |
| bogdanm | 65:5798e58a58b1 | 37 | * DigitalOut led1(LED1); |
| bogdanm | 65:5798e58a58b1 | 38 | * DigitalOut led2(LED2); |
| bogdanm | 65:5798e58a58b1 | 39 | * |
| bogdanm | 65:5798e58a58b1 | 40 | * int flip = 0; |
| bogdanm | 65:5798e58a58b1 | 41 | * |
| bogdanm | 65:5798e58a58b1 | 42 | * void attime() { |
| bogdanm | 65:5798e58a58b1 | 43 | * flip = !flip; |
| bogdanm | 65:5798e58a58b1 | 44 | * } |
| bogdanm | 65:5798e58a58b1 | 45 | * |
| bogdanm | 65:5798e58a58b1 | 46 | * int main() { |
| bogdanm | 65:5798e58a58b1 | 47 | * timer.attach(&attime, 5); |
| bogdanm | 65:5798e58a58b1 | 48 | * while(1) { |
| bogdanm | 65:5798e58a58b1 | 49 | * if(flip == 0) { |
| bogdanm | 65:5798e58a58b1 | 50 | * led1 = !led1; |
| bogdanm | 65:5798e58a58b1 | 51 | * } else { |
| bogdanm | 65:5798e58a58b1 | 52 | * led2 = !led2; |
| bogdanm | 65:5798e58a58b1 | 53 | * } |
| bogdanm | 65:5798e58a58b1 | 54 | * wait(0.2); |
| bogdanm | 65:5798e58a58b1 | 55 | * } |
| bogdanm | 65:5798e58a58b1 | 56 | * } |
| bogdanm | 65:5798e58a58b1 | 57 | * @endcode |
| bogdanm | 65:5798e58a58b1 | 58 | */ |
| bogdanm | 65:5798e58a58b1 | 59 | class Ticker : public TimerEvent { |
| bogdanm | 65:5798e58a58b1 | 60 | |
| bogdanm | 65:5798e58a58b1 | 61 | public: |
| Kojto | 98:8ab26030e058 | 62 | Ticker() : TimerEvent() { |
| Kojto | 98:8ab26030e058 | 63 | } |
| Kojto | 98:8ab26030e058 | 64 | |
| Kojto | 100:cbbeb26dbd92 | 65 | Ticker(const ticker_data_t *data) : TimerEvent(data) { |
| Kojto | 98:8ab26030e058 | 66 | } |
| bogdanm | 65:5798e58a58b1 | 67 | |
| bogdanm | 65:5798e58a58b1 | 68 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
| bogdanm | 65:5798e58a58b1 | 69 | * |
| Kojto | 122:f9eeca106725 | 70 | * @param func pointer to the function to be called |
| bogdanm | 65:5798e58a58b1 | 71 | * @param t the time between calls in seconds |
| bogdanm | 65:5798e58a58b1 | 72 | */ |
| Kojto | 122:f9eeca106725 | 73 | void attach(Callback<void()> func, float t) { |
| Kojto | 122:f9eeca106725 | 74 | attach_us(func, t * 1000000.0f); |
| bogdanm | 65:5798e58a58b1 | 75 | } |
| bogdanm | 65:5798e58a58b1 | 76 | |
| bogdanm | 65:5798e58a58b1 | 77 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
| bogdanm | 65:5798e58a58b1 | 78 | * |
| Kojto | 122:f9eeca106725 | 79 | * @param obj pointer to the object to call the member function on |
| Kojto | 122:f9eeca106725 | 80 | * @param method pointer to the member function to be called |
| bogdanm | 65:5798e58a58b1 | 81 | * @param t the time between calls in seconds |
| bogdanm | 65:5798e58a58b1 | 82 | */ |
| Kojto | 122:f9eeca106725 | 83 | template<typename T, typename M> |
| Kojto | 122:f9eeca106725 | 84 | void attach(T *obj, M method, float t) { |
| Kojto | 122:f9eeca106725 | 85 | attach(Callback<void()>(obj, method), t); |
| bogdanm | 65:5798e58a58b1 | 86 | } |
| bogdanm | 65:5798e58a58b1 | 87 | |
| bogdanm | 65:5798e58a58b1 | 88 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
| bogdanm | 65:5798e58a58b1 | 89 | * |
| bogdanm | 65:5798e58a58b1 | 90 | * @param fptr pointer to the function to be called |
| bogdanm | 65:5798e58a58b1 | 91 | * @param t the time between calls in micro-seconds |
| bogdanm | 65:5798e58a58b1 | 92 | */ |
| Kojto | 122:f9eeca106725 | 93 | void attach_us(Callback<void()> func, timestamp_t t) { |
| Kojto | 122:f9eeca106725 | 94 | _function.attach(func); |
| bogdanm | 65:5798e58a58b1 | 95 | setup(t); |
| bogdanm | 65:5798e58a58b1 | 96 | } |
| bogdanm | 65:5798e58a58b1 | 97 | |
| bogdanm | 65:5798e58a58b1 | 98 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
| bogdanm | 65:5798e58a58b1 | 99 | * |
| bogdanm | 65:5798e58a58b1 | 100 | * @param tptr pointer to the object to call the member function on |
| bogdanm | 65:5798e58a58b1 | 101 | * @param mptr pointer to the member function to be called |
| bogdanm | 65:5798e58a58b1 | 102 | * @param t the time between calls in micro-seconds |
| bogdanm | 65:5798e58a58b1 | 103 | */ |
| Kojto | 122:f9eeca106725 | 104 | template<typename T, typename M> |
| Kojto | 122:f9eeca106725 | 105 | void attach_us(T *obj, M method, timestamp_t t) { |
| Kojto | 122:f9eeca106725 | 106 | attach_us(Callback<void()>(obj, method), t); |
| bogdanm | 65:5798e58a58b1 | 107 | } |
| bogdanm | 65:5798e58a58b1 | 108 | |
| bogdanm | 89:552587b429a1 | 109 | virtual ~Ticker() { |
| bogdanm | 89:552587b429a1 | 110 | detach(); |
| bogdanm | 89:552587b429a1 | 111 | } |
| bogdanm | 89:552587b429a1 | 112 | |
| bogdanm | 65:5798e58a58b1 | 113 | /** Detach the function |
| bogdanm | 65:5798e58a58b1 | 114 | */ |
| bogdanm | 65:5798e58a58b1 | 115 | void detach(); |
| bogdanm | 65:5798e58a58b1 | 116 | |
| bogdanm | 65:5798e58a58b1 | 117 | protected: |
| bogdanm | 89:552587b429a1 | 118 | void setup(timestamp_t t); |
| bogdanm | 65:5798e58a58b1 | 119 | virtual void handler(); |
| bogdanm | 65:5798e58a58b1 | 120 | |
| bogdanm | 89:552587b429a1 | 121 | protected: |
| Kojto | 122:f9eeca106725 | 122 | timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ |
| Kojto | 122:f9eeca106725 | 123 | Callback<void()> _function; /**< Callback. */ |
| bogdanm | 65:5798e58a58b1 | 124 | }; |
| bogdanm | 65:5798e58a58b1 | 125 | |
| bogdanm | 65:5798e58a58b1 | 126 | } // namespace mbed |
| bogdanm | 65:5798e58a58b1 | 127 | |
| bogdanm | 65:5798e58a58b1 | 128 | #endif |
