The official mbed C/C SDK provides the software platform and libraries to build your applications.

Fork of mbed by mbed official

Committer:
bogdanm
Date:
Mon Aug 12 13:17:46 2013 +0300
Revision:
65:5798e58a58b1
Parent:
59:0883845fe643
Child:
68:f37f3b9c9f0b
New target (LPC4088), new features (interrupt chaining), bug fixes (KL25Z I2C).

Who changed what in which revision?

UserRevisionLine numberNew 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_INTERRUPTIN_H
bogdanm 65:5798e58a58b1 17 #define MBED_INTERRUPTIN_H
bogdanm 65:5798e58a58b1 18
bogdanm 65:5798e58a58b1 19 #include "platform.h"
bogdanm 65:5798e58a58b1 20
bogdanm 65:5798e58a58b1 21 #if DEVICE_INTERRUPTIN
bogdanm 65:5798e58a58b1 22
bogdanm 65:5798e58a58b1 23 #include "gpio_api.h"
bogdanm 65:5798e58a58b1 24 #include "gpio_irq_api.h"
bogdanm 65:5798e58a58b1 25
bogdanm 65:5798e58a58b1 26 #include "FunctionPointer.h"
bogdanm 65:5798e58a58b1 27 #include "CallChain.h"
bogdanm 65:5798e58a58b1 28
bogdanm 65:5798e58a58b1 29 namespace mbed {
bogdanm 65:5798e58a58b1 30
bogdanm 65:5798e58a58b1 31 /** A digital interrupt input, used to call a function on a rising or falling edge
bogdanm 65:5798e58a58b1 32 *
bogdanm 65:5798e58a58b1 33 * Example:
bogdanm 65:5798e58a58b1 34 * @code
bogdanm 65:5798e58a58b1 35 * // Flash an LED while waiting for events
bogdanm 65:5798e58a58b1 36 *
bogdanm 65:5798e58a58b1 37 * #include "mbed.h"
bogdanm 65:5798e58a58b1 38 *
bogdanm 65:5798e58a58b1 39 * InterruptIn event(p16);
bogdanm 65:5798e58a58b1 40 * DigitalOut led(LED1);
bogdanm 65:5798e58a58b1 41 *
bogdanm 65:5798e58a58b1 42 * void trigger() {
bogdanm 65:5798e58a58b1 43 * printf("triggered!\n");
bogdanm 65:5798e58a58b1 44 * }
bogdanm 65:5798e58a58b1 45 *
bogdanm 65:5798e58a58b1 46 * int main() {
bogdanm 65:5798e58a58b1 47 * event.rise(&trigger);
bogdanm 65:5798e58a58b1 48 * while(1) {
bogdanm 65:5798e58a58b1 49 * led = !led;
bogdanm 65:5798e58a58b1 50 * wait(0.25);
bogdanm 65:5798e58a58b1 51 * }
bogdanm 65:5798e58a58b1 52 * }
bogdanm 65:5798e58a58b1 53 * @endcode
bogdanm 65:5798e58a58b1 54 */
bogdanm 65:5798e58a58b1 55 class InterruptIn {
bogdanm 65:5798e58a58b1 56
bogdanm 65:5798e58a58b1 57 public:
bogdanm 65:5798e58a58b1 58
bogdanm 65:5798e58a58b1 59 /** Create an InterruptIn connected to the specified pin
bogdanm 65:5798e58a58b1 60 *
bogdanm 65:5798e58a58b1 61 * @param pin InterruptIn pin to connect to
bogdanm 65:5798e58a58b1 62 * @param name (optional) A string to identify the object
bogdanm 65:5798e58a58b1 63 */
bogdanm 65:5798e58a58b1 64 InterruptIn(PinName pin);
bogdanm 65:5798e58a58b1 65 virtual ~InterruptIn();
bogdanm 65:5798e58a58b1 66
bogdanm 65:5798e58a58b1 67 int read();
bogdanm 65:5798e58a58b1 68 #ifdef MBED_OPERATORS
bogdanm 65:5798e58a58b1 69 operator int();
bogdanm 65:5798e58a58b1 70
bogdanm 65:5798e58a58b1 71 #endif
bogdanm 65:5798e58a58b1 72
bogdanm 65:5798e58a58b1 73 /** Attach a function to call when a rising edge occurs on the input
bogdanm 65:5798e58a58b1 74 *
bogdanm 65:5798e58a58b1 75 * @param fptr A pointer to a void function, or 0 to set as none
bogdanm 65:5798e58a58b1 76 *
bogdanm 65:5798e58a58b1 77 * @returns
bogdanm 65:5798e58a58b1 78 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 79 */
bogdanm 65:5798e58a58b1 80 pFunctionPointer_t rise(void (*fptr)(void));
bogdanm 65:5798e58a58b1 81
bogdanm 65:5798e58a58b1 82 /** Add a function to be called when a rising edge occurs at the end of the call chain
bogdanm 65:5798e58a58b1 83 *
bogdanm 65:5798e58a58b1 84 * @param fptr the function to add
bogdanm 65:5798e58a58b1 85 *
bogdanm 65:5798e58a58b1 86 * @returns
bogdanm 65:5798e58a58b1 87 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 88 */
bogdanm 65:5798e58a58b1 89 pFunctionPointer_t rise_add(void (*fptr)(void)) {
bogdanm 65:5798e58a58b1 90 return rise_add_common(fptr);
bogdanm 65:5798e58a58b1 91 }
bogdanm 65:5798e58a58b1 92
bogdanm 65:5798e58a58b1 93 /** Add a function to be called when a rising edge occurs at the beginning of the call chain
bogdanm 65:5798e58a58b1 94 *
bogdanm 65:5798e58a58b1 95 * @param fptr the function to add
bogdanm 65:5798e58a58b1 96 *
bogdanm 65:5798e58a58b1 97 * @returns
bogdanm 65:5798e58a58b1 98 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 99 */
bogdanm 65:5798e58a58b1 100 pFunctionPointer_t rise_add_front(void (*fptr)(void)) {
bogdanm 65:5798e58a58b1 101 return rise_add_common(fptr, true);
bogdanm 65:5798e58a58b1 102 }
bogdanm 65:5798e58a58b1 103
bogdanm 65:5798e58a58b1 104 /** Attach a member function to call when a rising edge occurs on the input
bogdanm 65:5798e58a58b1 105 *
bogdanm 65:5798e58a58b1 106 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 107 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 108 *
bogdanm 65:5798e58a58b1 109 * @returns
bogdanm 65:5798e58a58b1 110 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 111 */
bogdanm 65:5798e58a58b1 112 template<typename T>
bogdanm 65:5798e58a58b1 113 pFunctionPointer_t rise(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 114 _rise.clear();
bogdanm 65:5798e58a58b1 115 pFunctionPointer_t pf = _rise.add(tptr, mptr);
bogdanm 65:5798e58a58b1 116 gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
bogdanm 65:5798e58a58b1 117 return pf;
bogdanm 65:5798e58a58b1 118 }
bogdanm 65:5798e58a58b1 119
bogdanm 65:5798e58a58b1 120 /** Add a function to be called when a rising edge occurs at the end of the call chain
bogdanm 65:5798e58a58b1 121 *
bogdanm 65:5798e58a58b1 122 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 123 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 124 *
bogdanm 65:5798e58a58b1 125 * @returns
bogdanm 65:5798e58a58b1 126 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 127 */
bogdanm 65:5798e58a58b1 128 template<typename T>
bogdanm 65:5798e58a58b1 129 pFunctionPointer_t rise_add(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 130 return rise_add_common(tptr, mptr);
bogdanm 65:5798e58a58b1 131 }
bogdanm 65:5798e58a58b1 132
bogdanm 65:5798e58a58b1 133 /** Add a function to be called when a rising edge occurs at the beginning of the call chain
bogdanm 65:5798e58a58b1 134 *
bogdanm 65:5798e58a58b1 135 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 136 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 137 *
bogdanm 65:5798e58a58b1 138 * @returns
bogdanm 65:5798e58a58b1 139 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 140 */
bogdanm 65:5798e58a58b1 141 template<typename T>
bogdanm 65:5798e58a58b1 142 pFunctionPointer_t rise_add_front(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 143 return rise_add_common(tptr, mptr, true);
bogdanm 65:5798e58a58b1 144 }
bogdanm 65:5798e58a58b1 145
bogdanm 65:5798e58a58b1 146 /** Remove a function from the list of functions to be called when a rising edge occurs
bogdanm 65:5798e58a58b1 147 *
bogdanm 65:5798e58a58b1 148 * @param pf the function object to remove
bogdanm 65:5798e58a58b1 149 *
bogdanm 65:5798e58a58b1 150 * @returns
bogdanm 65:5798e58a58b1 151 * true if the function was found and removed, false otherwise
bogdanm 65:5798e58a58b1 152 */
bogdanm 65:5798e58a58b1 153 bool rise_remove(pFunctionPointer_t pf);
bogdanm 65:5798e58a58b1 154
bogdanm 65:5798e58a58b1 155 /** Attach a function to call when a falling edge occurs on the input
bogdanm 65:5798e58a58b1 156 *
bogdanm 65:5798e58a58b1 157 * @param fptr A pointer to a void function, or 0 to set as none
bogdanm 65:5798e58a58b1 158 *
bogdanm 65:5798e58a58b1 159 * @returns
bogdanm 65:5798e58a58b1 160 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 161 */
bogdanm 65:5798e58a58b1 162 pFunctionPointer_t fall(void (*fptr)(void));
bogdanm 65:5798e58a58b1 163
bogdanm 65:5798e58a58b1 164 /** Add a function to be called when a falling edge occurs at the end of the call chain
bogdanm 65:5798e58a58b1 165 *
bogdanm 65:5798e58a58b1 166 * @param fptr the function to add
bogdanm 65:5798e58a58b1 167 *
bogdanm 65:5798e58a58b1 168 * @returns
bogdanm 65:5798e58a58b1 169 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 170 */
bogdanm 65:5798e58a58b1 171 pFunctionPointer_t fall_add(void (*fptr)(void)) {
bogdanm 65:5798e58a58b1 172 return fall_add_common(fptr);
bogdanm 65:5798e58a58b1 173 }
bogdanm 65:5798e58a58b1 174
bogdanm 65:5798e58a58b1 175 /** Add a function to be called when a falling edge occurs at the beginning of the call chain
bogdanm 65:5798e58a58b1 176 *
bogdanm 65:5798e58a58b1 177 * @param fptr the function to add
bogdanm 65:5798e58a58b1 178 *
bogdanm 65:5798e58a58b1 179 * @returns
bogdanm 65:5798e58a58b1 180 * The function object created for 'fptr'
bogdanm 65:5798e58a58b1 181 */
bogdanm 65:5798e58a58b1 182 pFunctionPointer_t fall_add_front(void (*fptr)(void)) {
bogdanm 65:5798e58a58b1 183 return fall_add_common(fptr, true);
bogdanm 65:5798e58a58b1 184 }
bogdanm 65:5798e58a58b1 185
bogdanm 65:5798e58a58b1 186 /** Attach a member function to call when a falling edge occurs on the input
bogdanm 65:5798e58a58b1 187 *
bogdanm 65:5798e58a58b1 188 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 189 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 190 *
bogdanm 65:5798e58a58b1 191 * @returns
bogdanm 65:5798e58a58b1 192 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 193 */
bogdanm 65:5798e58a58b1 194 template<typename T>
bogdanm 65:5798e58a58b1 195 pFunctionPointer_t fall(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 196 _fall.clear();
bogdanm 65:5798e58a58b1 197 pFunctionPointer_t pf = _fall.add(tptr, mptr);
bogdanm 65:5798e58a58b1 198 gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
bogdanm 65:5798e58a58b1 199 return pf;
bogdanm 65:5798e58a58b1 200 }
bogdanm 65:5798e58a58b1 201
bogdanm 65:5798e58a58b1 202 /** Add a function to be called when a falling edge occurs at the end of the call chain
bogdanm 65:5798e58a58b1 203 *
bogdanm 65:5798e58a58b1 204 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 205 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 206 *
bogdanm 65:5798e58a58b1 207 * @returns
bogdanm 65:5798e58a58b1 208 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 209 */
bogdanm 65:5798e58a58b1 210 template<typename T>
bogdanm 65:5798e58a58b1 211 pFunctionPointer_t fall_add(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 212 return fall_add_common(tptr, mptr);
bogdanm 65:5798e58a58b1 213 }
bogdanm 65:5798e58a58b1 214
bogdanm 65:5798e58a58b1 215 /** Add a function to be called when a falling edge occurs at the beginning of the call chain
bogdanm 65:5798e58a58b1 216 *
bogdanm 65:5798e58a58b1 217 * @param tptr pointer to the object to call the member function on
bogdanm 65:5798e58a58b1 218 * @param mptr pointer to the member function to be called
bogdanm 65:5798e58a58b1 219 *
bogdanm 65:5798e58a58b1 220 * @returns
bogdanm 65:5798e58a58b1 221 * The function object created for 'tptr' and 'mptr'
bogdanm 65:5798e58a58b1 222 */
bogdanm 65:5798e58a58b1 223 template<typename T>
bogdanm 65:5798e58a58b1 224 pFunctionPointer_t fall_add_front(T* tptr, void (T::*mptr)(void)) {
bogdanm 65:5798e58a58b1 225 return fall_add_common(tptr, mptr, true);
bogdanm 65:5798e58a58b1 226 }
bogdanm 65:5798e58a58b1 227
bogdanm 65:5798e58a58b1 228 /** Remove a function from the list of functions to be called when a falling edge occurs
bogdanm 65:5798e58a58b1 229 *
bogdanm 65:5798e58a58b1 230 * @param pf the function object to remove
bogdanm 65:5798e58a58b1 231 *
bogdanm 65:5798e58a58b1 232 * @returns
bogdanm 65:5798e58a58b1 233 * true if the function was found and removed, false otherwise
bogdanm 65:5798e58a58b1 234 */
bogdanm 65:5798e58a58b1 235 bool fall_remove(pFunctionPointer_t pf);
bogdanm 65:5798e58a58b1 236
bogdanm 65:5798e58a58b1 237 /** Set the input pin mode
bogdanm 65:5798e58a58b1 238 *
bogdanm 65:5798e58a58b1 239 * @param mode PullUp, PullDown, PullNone
bogdanm 65:5798e58a58b1 240 */
bogdanm 65:5798e58a58b1 241 void mode(PinMode pull);
bogdanm 65:5798e58a58b1 242
bogdanm 65:5798e58a58b1 243 static void _irq_handler(uint32_t id, gpio_irq_event event);
bogdanm 65:5798e58a58b1 244
bogdanm 65:5798e58a58b1 245 protected:
bogdanm 65:5798e58a58b1 246 pFunctionPointer_t rise_add_common(void (*fptr)(void), bool front=false);
bogdanm 65:5798e58a58b1 247 pFunctionPointer_t fall_add_common(void (*fptr)(void), bool front=false);
bogdanm 65:5798e58a58b1 248
bogdanm 65:5798e58a58b1 249 template<typename T>
bogdanm 65:5798e58a58b1 250 pFunctionPointer_t rise_add_common(T* tptr, void (T::*mptr)(void), bool front=false) {
bogdanm 65:5798e58a58b1 251 pFunctionPointer_t pf = front ? _rise.add_front(tptr, mptr) : _rise.add(tptr, mptr);
bogdanm 65:5798e58a58b1 252 gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
bogdanm 65:5798e58a58b1 253 return pf;
bogdanm 65:5798e58a58b1 254 }
bogdanm 65:5798e58a58b1 255 template<typename T>
bogdanm 65:5798e58a58b1 256 pFunctionPointer_t fall_add_common(T* tptr, void (T::*mptr)(void), bool front=false) {
bogdanm 65:5798e58a58b1 257 pFunctionPointer_t pf = front ? _fall.add_front(tptr, mptr) : _fall.add(tptr, mptr);
bogdanm 65:5798e58a58b1 258 gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
bogdanm 65:5798e58a58b1 259 return pf;
bogdanm 65:5798e58a58b1 260 }
bogdanm 65:5798e58a58b1 261
bogdanm 65:5798e58a58b1 262 gpio_t gpio;
bogdanm 65:5798e58a58b1 263 gpio_irq_t gpio_irq;
bogdanm 65:5798e58a58b1 264
bogdanm 65:5798e58a58b1 265 CallChain _rise;
bogdanm 65:5798e58a58b1 266 CallChain _fall;
bogdanm 65:5798e58a58b1 267 };
bogdanm 65:5798e58a58b1 268
bogdanm 65:5798e58a58b1 269 } // namespace mbed
bogdanm 65:5798e58a58b1 270
bogdanm 65:5798e58a58b1 271 #endif
bogdanm 65:5798e58a58b1 272
bogdanm 65:5798e58a58b1 273 #endif