mbed lib with startup delay fixed for Nucleo401RE

Fork of mbed-src by mbed official

Committer:
bogdanm
Date:
Wed Aug 07 16:43:59 2013 +0300
Revision:
15:4892fe388435
Parent:
13:0645d8841f51
Child:
19:398f4c622e1b
Added LPC4088 target and interrupt chaining code

Who changed what in which revision?

UserRevisionLine numberNew 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 15:4892fe388435 21 #include "CallChain.h"
bogdanm 13:0645d8841f51 22
bogdanm 13:0645d8841f51 23 namespace mbed {
bogdanm 13:0645d8841f51 24
bogdanm 13:0645d8841f51 25 /** A Ticker is used to call a function at a recurring interval
bogdanm 13:0645d8841f51 26 *
bogdanm 13:0645d8841f51 27 * You can use as many seperate Ticker objects as you require.
bogdanm 13:0645d8841f51 28 *
bogdanm 13:0645d8841f51 29 * Example:
bogdanm 13:0645d8841f51 30 * @code
bogdanm 13:0645d8841f51 31 * // Toggle the blinking led after 5 seconds
bogdanm 13:0645d8841f51 32 *
bogdanm 13:0645d8841f51 33 * #include "mbed.h"
bogdanm 13:0645d8841f51 34 *
bogdanm 13:0645d8841f51 35 * Ticker timer;
bogdanm 13:0645d8841f51 36 * DigitalOut led1(LED1);
bogdanm 13:0645d8841f51 37 * DigitalOut led2(LED2);
bogdanm 13:0645d8841f51 38 *
bogdanm 13:0645d8841f51 39 * int flip = 0;
bogdanm 13:0645d8841f51 40 *
bogdanm 13:0645d8841f51 41 * void attime() {
bogdanm 13:0645d8841f51 42 * flip = !flip;
bogdanm 13:0645d8841f51 43 * }
bogdanm 13:0645d8841f51 44 *
bogdanm 13:0645d8841f51 45 * int main() {
bogdanm 13:0645d8841f51 46 * timer.attach(&attime, 5);
bogdanm 13:0645d8841f51 47 * while(1) {
bogdanm 13:0645d8841f51 48 * if(flip == 0) {
bogdanm 13:0645d8841f51 49 * led1 = !led1;
bogdanm 13:0645d8841f51 50 * } else {
bogdanm 13:0645d8841f51 51 * led2 = !led2;
bogdanm 13:0645d8841f51 52 * }
bogdanm 13:0645d8841f51 53 * wait(0.2);
bogdanm 13:0645d8841f51 54 * }
bogdanm 13:0645d8841f51 55 * }
bogdanm 13:0645d8841f51 56 * @endcode
bogdanm 13:0645d8841f51 57 */
bogdanm 13:0645d8841f51 58 class Ticker : public TimerEvent {
bogdanm 13:0645d8841f51 59
bogdanm 13:0645d8841f51 60 public:
bogdanm 13:0645d8841f51 61
bogdanm 13:0645d8841f51 62 /** Attach a function to be called by the Ticker, specifiying the interval in seconds
bogdanm 13:0645d8841f51 63 *
bogdanm 13:0645d8841f51 64 * @param fptr pointer to the function to be called
bogdanm 13:0645d8841f51 65 * @param t the time between calls in seconds
bogdanm 15:4892fe388435 66 *
bogdanm 15:4892fe388435 67 * @returns
bogdanm 15:4892fe388435 68 * The function object created for 'fptr'
bogdanm 13:0645d8841f51 69 */
bogdanm 15:4892fe388435 70 pFunctionPointer_t attach(void (*fptr)(void), float t) {
bogdanm 15:4892fe388435 71 return attach_us(fptr, t * 1000000.0f);
bogdanm 15:4892fe388435 72 }
bogdanm 15:4892fe388435 73
bogdanm 15:4892fe388435 74 /** Add a function to be called by the Ticker at the end of the call chain
bogdanm 15:4892fe388435 75 *
bogdanm 15:4892fe388435 76 * @param fptr the function to add
bogdanm 15:4892fe388435 77 *
bogdanm 15:4892fe388435 78 * @returns
bogdanm 15:4892fe388435 79 * The function object created for 'fptr'
bogdanm 15:4892fe388435 80 */
bogdanm 15:4892fe388435 81 pFunctionPointer_t add_function(void (*fptr)(void)) {
bogdanm 15:4892fe388435 82 return add_function_helper(fptr);
bogdanm 15:4892fe388435 83 }
bogdanm 15:4892fe388435 84
bogdanm 15:4892fe388435 85 /** Add a function to be called by the Ticker at the beginning of the call chain
bogdanm 15:4892fe388435 86 *
bogdanm 15:4892fe388435 87 * @param fptr the function to add
bogdanm 15:4892fe388435 88 *
bogdanm 15:4892fe388435 89 * @returns
bogdanm 15:4892fe388435 90 * The function object created for 'fptr'
bogdanm 15:4892fe388435 91 */
bogdanm 15:4892fe388435 92 pFunctionPointer_t add_function_front(void (*fptr)(void)) {
bogdanm 15:4892fe388435 93 return add_function_helper(fptr, true);
bogdanm 13:0645d8841f51 94 }
bogdanm 13:0645d8841f51 95
bogdanm 13:0645d8841f51 96 /** Attach a member function to be called by the Ticker, specifiying the interval in seconds
bogdanm 13:0645d8841f51 97 *
bogdanm 13:0645d8841f51 98 * @param tptr pointer to the object to call the member function on
bogdanm 13:0645d8841f51 99 * @param mptr pointer to the member function to be called
bogdanm 13:0645d8841f51 100 * @param t the time between calls in seconds
bogdanm 15:4892fe388435 101 *
bogdanm 15:4892fe388435 102 * @returns
bogdanm 15:4892fe388435 103 * The function object created for 'tptr' and 'mptr'
bogdanm 13:0645d8841f51 104 */
bogdanm 13:0645d8841f51 105 template<typename T>
bogdanm 15:4892fe388435 106 pFunctionPointer_t attach(T* tptr, void (T::*mptr)(void), float t) {
bogdanm 15:4892fe388435 107 return attach_us(tptr, mptr, t * 1000000.0f);
bogdanm 15:4892fe388435 108 }
bogdanm 15:4892fe388435 109
bogdanm 15:4892fe388435 110 /** Add a function to be called by the Ticker at the end of the call chain
bogdanm 15:4892fe388435 111 *
bogdanm 15:4892fe388435 112 * @param tptr pointer to the object to call the member function on
bogdanm 15:4892fe388435 113 * @param mptr pointer to the member function to be called
bogdanm 15:4892fe388435 114 *
bogdanm 15:4892fe388435 115 * @returns
bogdanm 15:4892fe388435 116 * The function object created for 'tptr' and 'mptr'
bogdanm 15:4892fe388435 117 */
bogdanm 15:4892fe388435 118 template<typename T>
bogdanm 15:4892fe388435 119 pFunctionPointer_t add_function(T* tptr, void (T::*mptr)(void)) {
bogdanm 15:4892fe388435 120 return add_function_helper(tptr, mptr);
bogdanm 15:4892fe388435 121 }
bogdanm 15:4892fe388435 122
bogdanm 15:4892fe388435 123 /** Add a function to be called by the Ticker at the beginning of the call chain
bogdanm 15:4892fe388435 124 *
bogdanm 15:4892fe388435 125 * @param tptr pointer to the object to call the member function on
bogdanm 15:4892fe388435 126 * @param mptr pointer to the member function to be called
bogdanm 15:4892fe388435 127 *
bogdanm 15:4892fe388435 128 * @returns
bogdanm 15:4892fe388435 129 * The function object created for 'tptr' and 'mptr'
bogdanm 15:4892fe388435 130 */
bogdanm 15:4892fe388435 131 template<typename T>
bogdanm 15:4892fe388435 132 pFunctionPointer_t add_function_front(T* tptr, void (T::*mptr)(void)) {
bogdanm 15:4892fe388435 133 return add_function_helper(tptr, mptr, true);
bogdanm 13:0645d8841f51 134 }
bogdanm 13:0645d8841f51 135
bogdanm 13:0645d8841f51 136 /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds
bogdanm 13:0645d8841f51 137 *
bogdanm 13:0645d8841f51 138 * @param fptr pointer to the function to be called
bogdanm 13:0645d8841f51 139 * @param t the time between calls in micro-seconds
bogdanm 15:4892fe388435 140 *
bogdanm 15:4892fe388435 141 * @returns
bogdanm 15:4892fe388435 142 * The function object created for 'fptr'
bogdanm 13:0645d8841f51 143 */
bogdanm 15:4892fe388435 144 pFunctionPointer_t attach_us(void (*fptr)(void), unsigned int t) {
bogdanm 15:4892fe388435 145 pFunctionPointer_t pf = _chain.add(fptr);
bogdanm 13:0645d8841f51 146 setup(t);
bogdanm 15:4892fe388435 147 return pf;
bogdanm 13:0645d8841f51 148 }
bogdanm 13:0645d8841f51 149
bogdanm 13:0645d8841f51 150 /** Attach a member function to be called by the Ticker, specifiying the interval in micro-seconds
bogdanm 13:0645d8841f51 151 *
bogdanm 13:0645d8841f51 152 * @param tptr pointer to the object to call the member function on
bogdanm 13:0645d8841f51 153 * @param mptr pointer to the member function to be called
bogdanm 13:0645d8841f51 154 * @param t the time between calls in micro-seconds
bogdanm 15:4892fe388435 155 *
bogdanm 15:4892fe388435 156 * @returns
bogdanm 15:4892fe388435 157 * The function object created for 'tptr' and 'mptr'
bogdanm 13:0645d8841f51 158 */
bogdanm 13:0645d8841f51 159 template<typename T>
bogdanm 15:4892fe388435 160 pFunctionPointer_t attach_us(T* tptr, void (T::*mptr)(void), unsigned int t) {
bogdanm 15:4892fe388435 161 pFunctionPointer_t pf = _chain.add(mptr, tptr);
bogdanm 13:0645d8841f51 162 setup(t);
bogdanm 15:4892fe388435 163 return pf;
bogdanm 13:0645d8841f51 164 }
bogdanm 13:0645d8841f51 165
bogdanm 13:0645d8841f51 166 /** Detach the function
bogdanm 13:0645d8841f51 167 */
bogdanm 13:0645d8841f51 168 void detach();
bogdanm 13:0645d8841f51 169
bogdanm 15:4892fe388435 170 /** Remove a function from the Ticker's call chain
bogdanm 15:4892fe388435 171 *
bogdanm 15:4892fe388435 172 * @param pf the function object to remove
bogdanm 15:4892fe388435 173 *
bogdanm 15:4892fe388435 174 * @returns
bogdanm 15:4892fe388435 175 * true if the function was found and removed, false otherwise
bogdanm 15:4892fe388435 176 */
bogdanm 15:4892fe388435 177 bool remove_function(pFunctionPointer_t pf) {
bogdanm 15:4892fe388435 178 bool res = _chain.remove(pf);
bogdanm 15:4892fe388435 179 if (res && _chain.size() == 0)
bogdanm 15:4892fe388435 180 detach();
bogdanm 15:4892fe388435 181 return res;
bogdanm 15:4892fe388435 182 }
bogdanm 15:4892fe388435 183
bogdanm 13:0645d8841f51 184 protected:
bogdanm 13:0645d8841f51 185 void setup(unsigned int t);
bogdanm 15:4892fe388435 186 pFunctionPointer_t add_function_helper(void (*fptr)(void), bool front=false);
bogdanm 13:0645d8841f51 187 virtual void handler();
bogdanm 13:0645d8841f51 188
bogdanm 15:4892fe388435 189 template<typename T>
bogdanm 15:4892fe388435 190 pFunctionPointer_t add_function_helper(T* tptr, void (T::*mptr)(void), bool front=false) {
bogdanm 15:4892fe388435 191 if (_chain.size() == 0)
bogdanm 15:4892fe388435 192 return NULL;
bogdanm 15:4892fe388435 193 return front ? _chain.add_front(tptr, mptr) : _chain.add(tptr, mptr);
bogdanm 15:4892fe388435 194 }
bogdanm 15:4892fe388435 195
bogdanm 13:0645d8841f51 196 unsigned int _delay;
bogdanm 15:4892fe388435 197 CallChain _chain;
bogdanm 13:0645d8841f51 198 };
bogdanm 13:0645d8841f51 199
bogdanm 13:0645d8841f51 200 } // namespace mbed
bogdanm 13:0645d8841f51 201
bogdanm 13:0645d8841f51 202 #endif