Fork of mbed for KL05Z based smart sensor which has no crystal on the board, so MCG FEI mode is required.

Dependents:   smart-sensor-KL05Z-debug

Fork of mbed-src by Ermanno Brusadin

Committer:
r14793
Date:
Mon Jan 29 15:38:37 2018 +0000
Revision:
1:da408b460382
Parent:
0:0a673c671a56
changed KL05 MCG mode to FEI for smart sensor boards (no crystal)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ebrus 0:0a673c671a56 1 /* mbed Microcontroller Library
ebrus 0:0a673c671a56 2 * Copyright (c) 2006-2013 ARM Limited
ebrus 0:0a673c671a56 3 *
ebrus 0:0a673c671a56 4 * Licensed under the Apache License, Version 2.0 (the "License");
ebrus 0:0a673c671a56 5 * you may not use this file except in compliance with the License.
ebrus 0:0a673c671a56 6 * You may obtain a copy of the License at
ebrus 0:0a673c671a56 7 *
ebrus 0:0a673c671a56 8 * http://www.apache.org/licenses/LICENSE-2.0
ebrus 0:0a673c671a56 9 *
ebrus 0:0a673c671a56 10 * Unless required by applicable law or agreed to in writing, software
ebrus 0:0a673c671a56 11 * distributed under the License is distributed on an "AS IS" BASIS,
ebrus 0:0a673c671a56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ebrus 0:0a673c671a56 13 * See the License for the specific language governing permissions and
ebrus 0:0a673c671a56 14 * limitations under the License.
ebrus 0:0a673c671a56 15 */
ebrus 0:0a673c671a56 16 #ifndef MBED_TICKER_H
ebrus 0:0a673c671a56 17 #define MBED_TICKER_H
ebrus 0:0a673c671a56 18
ebrus 0:0a673c671a56 19 #include "TimerEvent.h"
ebrus 0:0a673c671a56 20 #include "FunctionPointer.h"
ebrus 0:0a673c671a56 21
ebrus 0:0a673c671a56 22 namespace mbed {
ebrus 0:0a673c671a56 23
ebrus 0:0a673c671a56 24 /** A Ticker is used to call a function at a recurring interval
ebrus 0:0a673c671a56 25 *
ebrus 0:0a673c671a56 26 * You can use as many seperate Ticker objects as you require.
ebrus 0:0a673c671a56 27 *
ebrus 0:0a673c671a56 28 * Example:
ebrus 0:0a673c671a56 29 * @code
ebrus 0:0a673c671a56 30 * // Toggle the blinking led after 5 seconds
ebrus 0:0a673c671a56 31 *
ebrus 0:0a673c671a56 32 * #include "mbed.h"
ebrus 0:0a673c671a56 33 *
ebrus 0:0a673c671a56 34 * Ticker timer;
ebrus 0:0a673c671a56 35 * DigitalOut led1(LED1);
ebrus 0:0a673c671a56 36 * DigitalOut led2(LED2);
ebrus 0:0a673c671a56 37 *
ebrus 0:0a673c671a56 38 * int flip = 0;
ebrus 0:0a673c671a56 39 *
ebrus 0:0a673c671a56 40 * void attime() {
ebrus 0:0a673c671a56 41 * flip = !flip;
ebrus 0:0a673c671a56 42 * }
ebrus 0:0a673c671a56 43 *
ebrus 0:0a673c671a56 44 * int main() {
ebrus 0:0a673c671a56 45 * timer.attach(&attime, 5);
ebrus 0:0a673c671a56 46 * while(1) {
ebrus 0:0a673c671a56 47 * if(flip == 0) {
ebrus 0:0a673c671a56 48 * led1 = !led1;
ebrus 0:0a673c671a56 49 * } else {
ebrus 0:0a673c671a56 50 * led2 = !led2;
ebrus 0:0a673c671a56 51 * }
ebrus 0:0a673c671a56 52 * wait(0.2);
ebrus 0:0a673c671a56 53 * }
ebrus 0:0a673c671a56 54 * }
ebrus 0:0a673c671a56 55 * @endcode
ebrus 0:0a673c671a56 56 */
ebrus 0:0a673c671a56 57 class Ticker : public TimerEvent {
ebrus 0:0a673c671a56 58
ebrus 0:0a673c671a56 59 public:
ebrus 0:0a673c671a56 60
ebrus 0:0a673c671a56 61 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
ebrus 0:0a673c671a56 62 *
ebrus 0:0a673c671a56 63 * @param fptr pointer to the function to be called
ebrus 0:0a673c671a56 64 * @param t the time between calls in seconds
ebrus 0:0a673c671a56 65 */
ebrus 0:0a673c671a56 66 void attach(void (*fptr)(void), float t) {
ebrus 0:0a673c671a56 67 attach_us(fptr, t * 1000000.0f);
ebrus 0:0a673c671a56 68 }
ebrus 0:0a673c671a56 69
ebrus 0:0a673c671a56 70 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
ebrus 0:0a673c671a56 71 *
ebrus 0:0a673c671a56 72 * @param tptr pointer to the object to call the member function on
ebrus 0:0a673c671a56 73 * @param mptr pointer to the member function to be called
ebrus 0:0a673c671a56 74 * @param t the time between calls in seconds
ebrus 0:0a673c671a56 75 */
ebrus 0:0a673c671a56 76 template<typename T>
ebrus 0:0a673c671a56 77 void attach(T* tptr, void (T::*mptr)(void), float t) {
ebrus 0:0a673c671a56 78 attach_us(tptr, mptr, t * 1000000.0f);
ebrus 0:0a673c671a56 79 }
ebrus 0:0a673c671a56 80
ebrus 0:0a673c671a56 81 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
ebrus 0:0a673c671a56 82 *
ebrus 0:0a673c671a56 83 * @param fptr pointer to the function to be called
ebrus 0:0a673c671a56 84 * @param t the time between calls in micro-seconds
ebrus 0:0a673c671a56 85 */
ebrus 0:0a673c671a56 86 void attach_us(void (*fptr)(void), timestamp_t t) {
ebrus 0:0a673c671a56 87 _function.attach(fptr);
ebrus 0:0a673c671a56 88 setup(t);
ebrus 0:0a673c671a56 89 }
ebrus 0:0a673c671a56 90
ebrus 0:0a673c671a56 91 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
ebrus 0:0a673c671a56 92 *
ebrus 0:0a673c671a56 93 * @param tptr pointer to the object to call the member function on
ebrus 0:0a673c671a56 94 * @param mptr pointer to the member function to be called
ebrus 0:0a673c671a56 95 * @param t the time between calls in micro-seconds
ebrus 0:0a673c671a56 96 */
ebrus 0:0a673c671a56 97 template<typename T>
ebrus 0:0a673c671a56 98 void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) {
ebrus 0:0a673c671a56 99 _function.attach(tptr, mptr);
ebrus 0:0a673c671a56 100 setup(t);
ebrus 0:0a673c671a56 101 }
ebrus 0:0a673c671a56 102
ebrus 0:0a673c671a56 103 virtual ~Ticker() {
ebrus 0:0a673c671a56 104 detach();
ebrus 0:0a673c671a56 105 }
ebrus 0:0a673c671a56 106
ebrus 0:0a673c671a56 107 /** Detach the function
ebrus 0:0a673c671a56 108 */
ebrus 0:0a673c671a56 109 void detach();
ebrus 0:0a673c671a56 110
ebrus 0:0a673c671a56 111 protected:
ebrus 0:0a673c671a56 112 void setup(timestamp_t t);
ebrus 0:0a673c671a56 113 virtual void handler();
ebrus 0:0a673c671a56 114
ebrus 0:0a673c671a56 115 protected:
ebrus 0:0a673c671a56 116 timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */
ebrus 0:0a673c671a56 117 FunctionPointer _function; /**< Callback. */
ebrus 0:0a673c671a56 118 };
ebrus 0:0a673c671a56 119
ebrus 0:0a673c671a56 120 } // namespace mbed
ebrus 0:0a673c671a56 121
ebrus 0:0a673c671a56 122 #endif