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