mbed library sources
Fork of mbed-src by
api/Ticker.h@305:89b9c3a9a045, 2014-08-29 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Aug 29 17:15:07 2014 +0100
- Revision:
- 305:89b9c3a9a045
- Parent:
- 36:ab3ee77451e7
Synchronized with git revision 734f365d7da26ef199751f4b0d91611479b495ea
Full URL: https://github.com/mbedmicro/mbed/commit/734f365d7da26ef199751f4b0d91611479b495ea/
1. timestamp_t as an abstraction for time values managed by
Ticker. Using uint64_t for timestamp_t allows a wraparound-free
Ticker. This change forces us to update the definitions of usTicker
for all platforms; but the changes beyond nRF51822 aren't major.
2. reduce power consumption on the nRF51822 by removing the need for
the high-frequency processor timer; and reimplementing it using the
RTC.
I've also replaced high-frequency clock with low-frequency external
clock during system startup of the nRF51822. This brings a major win
in power consumption.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 13:0645d8841f51 | 1 | /* mbed Microcontroller Library |
bogdanm | 13:0645d8841f51 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 13:0645d8841f51 | 3 | * |
bogdanm | 13:0645d8841f51 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 13:0645d8841f51 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 13:0645d8841f51 | 6 | * You may obtain a copy of the License at |
bogdanm | 13:0645d8841f51 | 7 | * |
bogdanm | 13:0645d8841f51 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 13:0645d8841f51 | 9 | * |
bogdanm | 13:0645d8841f51 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 13:0645d8841f51 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 13:0645d8841f51 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 13:0645d8841f51 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 13:0645d8841f51 | 14 | * limitations under the License. |
bogdanm | 13:0645d8841f51 | 15 | */ |
bogdanm | 13:0645d8841f51 | 16 | #ifndef MBED_TICKER_H |
bogdanm | 13:0645d8841f51 | 17 | #define MBED_TICKER_H |
bogdanm | 13:0645d8841f51 | 18 | |
bogdanm | 13:0645d8841f51 | 19 | #include "TimerEvent.h" |
bogdanm | 13:0645d8841f51 | 20 | #include "FunctionPointer.h" |
bogdanm | 13:0645d8841f51 | 21 | |
bogdanm | 13:0645d8841f51 | 22 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 23 | |
bogdanm | 13:0645d8841f51 | 24 | /** A Ticker is used to call a function at a recurring interval |
bogdanm | 13:0645d8841f51 | 25 | * |
bogdanm | 13:0645d8841f51 | 26 | * You can use as many seperate Ticker objects as you require. |
bogdanm | 13:0645d8841f51 | 27 | * |
bogdanm | 13:0645d8841f51 | 28 | * Example: |
bogdanm | 13:0645d8841f51 | 29 | * @code |
bogdanm | 13:0645d8841f51 | 30 | * // Toggle the blinking led after 5 seconds |
bogdanm | 13:0645d8841f51 | 31 | * |
bogdanm | 13:0645d8841f51 | 32 | * #include "mbed.h" |
bogdanm | 13:0645d8841f51 | 33 | * |
bogdanm | 13:0645d8841f51 | 34 | * Ticker timer; |
bogdanm | 13:0645d8841f51 | 35 | * DigitalOut led1(LED1); |
bogdanm | 13:0645d8841f51 | 36 | * DigitalOut led2(LED2); |
bogdanm | 13:0645d8841f51 | 37 | * |
bogdanm | 13:0645d8841f51 | 38 | * int flip = 0; |
bogdanm | 13:0645d8841f51 | 39 | * |
bogdanm | 13:0645d8841f51 | 40 | * void attime() { |
bogdanm | 13:0645d8841f51 | 41 | * flip = !flip; |
bogdanm | 13:0645d8841f51 | 42 | * } |
bogdanm | 13:0645d8841f51 | 43 | * |
bogdanm | 13:0645d8841f51 | 44 | * int main() { |
bogdanm | 13:0645d8841f51 | 45 | * timer.attach(&attime, 5); |
bogdanm | 13:0645d8841f51 | 46 | * while(1) { |
bogdanm | 13:0645d8841f51 | 47 | * if(flip == 0) { |
bogdanm | 13:0645d8841f51 | 48 | * led1 = !led1; |
bogdanm | 13:0645d8841f51 | 49 | * } else { |
bogdanm | 13:0645d8841f51 | 50 | * led2 = !led2; |
bogdanm | 13:0645d8841f51 | 51 | * } |
bogdanm | 13:0645d8841f51 | 52 | * wait(0.2); |
bogdanm | 13:0645d8841f51 | 53 | * } |
bogdanm | 13:0645d8841f51 | 54 | * } |
bogdanm | 13:0645d8841f51 | 55 | * @endcode |
bogdanm | 13:0645d8841f51 | 56 | */ |
bogdanm | 13:0645d8841f51 | 57 | class Ticker : public TimerEvent { |
bogdanm | 13:0645d8841f51 | 58 | |
bogdanm | 13:0645d8841f51 | 59 | public: |
bogdanm | 13:0645d8841f51 | 60 | |
bogdanm | 13:0645d8841f51 | 61 | /** Attach a function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 13:0645d8841f51 | 62 | * |
bogdanm | 13:0645d8841f51 | 63 | * @param fptr pointer to the function to be called |
bogdanm | 13:0645d8841f51 | 64 | * @param t the time between calls in seconds |
bogdanm | 13:0645d8841f51 | 65 | */ |
mbed_official | 36:ab3ee77451e7 | 66 | void attach(void (*fptr)(void), float t) { |
mbed_official | 36:ab3ee77451e7 | 67 | attach_us(fptr, t * 1000000.0f); |
bogdanm | 13:0645d8841f51 | 68 | } |
bogdanm | 13:0645d8841f51 | 69 | |
bogdanm | 13:0645d8841f51 | 70 | /** Attach a member function to be called by the Ticker, specifiying the interval in seconds |
bogdanm | 13:0645d8841f51 | 71 | * |
bogdanm | 13:0645d8841f51 | 72 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 73 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 74 | * @param t the time between calls in seconds |
bogdanm | 13:0645d8841f51 | 75 | */ |
bogdanm | 13:0645d8841f51 | 76 | template<typename T> |
mbed_official | 36:ab3ee77451e7 | 77 | void attach(T* tptr, void (T::*mptr)(void), float t) { |
mbed_official | 36:ab3ee77451e7 | 78 | attach_us(tptr, mptr, t * 1000000.0f); |
bogdanm | 13:0645d8841f51 | 79 | } |
bogdanm | 13:0645d8841f51 | 80 | |
bogdanm | 13:0645d8841f51 | 81 | /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 13:0645d8841f51 | 82 | * |
bogdanm | 13:0645d8841f51 | 83 | * @param fptr pointer to the function to be called |
bogdanm | 13:0645d8841f51 | 84 | * @param t the time between calls in micro-seconds |
bogdanm | 13:0645d8841f51 | 85 | */ |
mbed_official | 305:89b9c3a9a045 | 86 | void attach_us(void (*fptr)(void), timestamp_t t) { |
mbed_official | 36:ab3ee77451e7 | 87 | _function.attach(fptr); |
bogdanm | 13:0645d8841f51 | 88 | setup(t); |
bogdanm | 13:0645d8841f51 | 89 | } |
bogdanm | 13:0645d8841f51 | 90 | |
bogdanm | 13:0645d8841f51 | 91 | /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds |
bogdanm | 13:0645d8841f51 | 92 | * |
bogdanm | 13:0645d8841f51 | 93 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 94 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 95 | * @param t the time between calls in micro-seconds |
bogdanm | 13:0645d8841f51 | 96 | */ |
bogdanm | 13:0645d8841f51 | 97 | template<typename T> |
mbed_official | 305:89b9c3a9a045 | 98 | void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) { |
mbed_official | 36:ab3ee77451e7 | 99 | _function.attach(tptr, mptr); |
bogdanm | 13:0645d8841f51 | 100 | setup(t); |
bogdanm | 13:0645d8841f51 | 101 | } |
bogdanm | 13:0645d8841f51 | 102 | |
mbed_official | 305:89b9c3a9a045 | 103 | virtual ~Ticker() { |
mbed_official | 305:89b9c3a9a045 | 104 | detach(); |
mbed_official | 305:89b9c3a9a045 | 105 | } |
mbed_official | 305:89b9c3a9a045 | 106 | |
bogdanm | 13:0645d8841f51 | 107 | /** Detach the function |
bogdanm | 13:0645d8841f51 | 108 | */ |
bogdanm | 13:0645d8841f51 | 109 | void detach(); |
bogdanm | 13:0645d8841f51 | 110 | |
bogdanm | 13:0645d8841f51 | 111 | protected: |
mbed_official | 305:89b9c3a9a045 | 112 | void setup(timestamp_t t); |
bogdanm | 13:0645d8841f51 | 113 | virtual void handler(); |
bogdanm | 13:0645d8841f51 | 114 | |
mbed_official | 305:89b9c3a9a045 | 115 | protected: |
mbed_official | 305:89b9c3a9a045 | 116 | timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ |
mbed_official | 305:89b9c3a9a045 | 117 | FunctionPointer _function; /**< Callback. */ |
bogdanm | 13:0645d8841f51 | 118 | }; |
bogdanm | 13:0645d8841f51 | 119 | |
bogdanm | 13:0645d8841f51 | 120 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 121 | |
bogdanm | 13:0645d8841f51 | 122 | #endif |