mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
api/InterruptIn.h@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #ifndef MBED_INTERRUPTIN_H |
bogdanm | 0:9b334a45a8ff | 17 | #define MBED_INTERRUPTIN_H |
bogdanm | 0:9b334a45a8ff | 18 | |
bogdanm | 0:9b334a45a8ff | 19 | #include "platform.h" |
bogdanm | 0:9b334a45a8ff | 20 | |
bogdanm | 0:9b334a45a8ff | 21 | #if DEVICE_INTERRUPTIN |
bogdanm | 0:9b334a45a8ff | 22 | |
bogdanm | 0:9b334a45a8ff | 23 | #include "gpio_api.h" |
bogdanm | 0:9b334a45a8ff | 24 | #include "gpio_irq_api.h" |
bogdanm | 0:9b334a45a8ff | 25 | #include "FunctionPointer.h" |
bogdanm | 0:9b334a45a8ff | 26 | |
bogdanm | 0:9b334a45a8ff | 27 | namespace mbed { |
bogdanm | 0:9b334a45a8ff | 28 | |
bogdanm | 0:9b334a45a8ff | 29 | /** A digital interrupt input, used to call a function on a rising or falling edge |
bogdanm | 0:9b334a45a8ff | 30 | * |
bogdanm | 0:9b334a45a8ff | 31 | * Example: |
bogdanm | 0:9b334a45a8ff | 32 | * @code |
bogdanm | 0:9b334a45a8ff | 33 | * // Flash an LED while waiting for events |
bogdanm | 0:9b334a45a8ff | 34 | * |
bogdanm | 0:9b334a45a8ff | 35 | * #include "mbed.h" |
bogdanm | 0:9b334a45a8ff | 36 | * |
bogdanm | 0:9b334a45a8ff | 37 | * InterruptIn event(p16); |
bogdanm | 0:9b334a45a8ff | 38 | * DigitalOut led(LED1); |
bogdanm | 0:9b334a45a8ff | 39 | * |
bogdanm | 0:9b334a45a8ff | 40 | * void trigger() { |
bogdanm | 0:9b334a45a8ff | 41 | * printf("triggered!\n"); |
bogdanm | 0:9b334a45a8ff | 42 | * } |
bogdanm | 0:9b334a45a8ff | 43 | * |
bogdanm | 0:9b334a45a8ff | 44 | * int main() { |
bogdanm | 0:9b334a45a8ff | 45 | * event.rise(&trigger); |
bogdanm | 0:9b334a45a8ff | 46 | * while(1) { |
bogdanm | 0:9b334a45a8ff | 47 | * led = !led; |
bogdanm | 0:9b334a45a8ff | 48 | * wait(0.25); |
bogdanm | 0:9b334a45a8ff | 49 | * } |
bogdanm | 0:9b334a45a8ff | 50 | * } |
bogdanm | 0:9b334a45a8ff | 51 | * @endcode |
bogdanm | 0:9b334a45a8ff | 52 | */ |
bogdanm | 0:9b334a45a8ff | 53 | class InterruptIn { |
bogdanm | 0:9b334a45a8ff | 54 | |
bogdanm | 0:9b334a45a8ff | 55 | public: |
bogdanm | 0:9b334a45a8ff | 56 | |
bogdanm | 0:9b334a45a8ff | 57 | /** Create an InterruptIn connected to the specified pin |
bogdanm | 0:9b334a45a8ff | 58 | * |
bogdanm | 0:9b334a45a8ff | 59 | * @param pin InterruptIn pin to connect to |
bogdanm | 0:9b334a45a8ff | 60 | * @param name (optional) A string to identify the object |
bogdanm | 0:9b334a45a8ff | 61 | */ |
bogdanm | 0:9b334a45a8ff | 62 | InterruptIn(PinName pin); |
bogdanm | 0:9b334a45a8ff | 63 | virtual ~InterruptIn(); |
bogdanm | 0:9b334a45a8ff | 64 | |
bogdanm | 0:9b334a45a8ff | 65 | int read(); |
bogdanm | 0:9b334a45a8ff | 66 | #ifdef MBED_OPERATORS |
bogdanm | 0:9b334a45a8ff | 67 | operator int(); |
bogdanm | 0:9b334a45a8ff | 68 | |
bogdanm | 0:9b334a45a8ff | 69 | #endif |
bogdanm | 0:9b334a45a8ff | 70 | |
bogdanm | 0:9b334a45a8ff | 71 | /** Attach a function to call when a rising edge occurs on the input |
bogdanm | 0:9b334a45a8ff | 72 | * |
bogdanm | 0:9b334a45a8ff | 73 | * @param fptr A pointer to a void function, or 0 to set as none |
bogdanm | 0:9b334a45a8ff | 74 | */ |
bogdanm | 0:9b334a45a8ff | 75 | void rise(void (*fptr)(void)); |
bogdanm | 0:9b334a45a8ff | 76 | |
bogdanm | 0:9b334a45a8ff | 77 | /** Attach a member function to call when a rising edge occurs on the input |
bogdanm | 0:9b334a45a8ff | 78 | * |
bogdanm | 0:9b334a45a8ff | 79 | * @param tptr pointer to the object to call the member function on |
bogdanm | 0:9b334a45a8ff | 80 | * @param mptr pointer to the member function to be called |
bogdanm | 0:9b334a45a8ff | 81 | */ |
bogdanm | 0:9b334a45a8ff | 82 | template<typename T> |
bogdanm | 0:9b334a45a8ff | 83 | void rise(T* tptr, void (T::*mptr)(void)) { |
bogdanm | 0:9b334a45a8ff | 84 | _rise.attach(tptr, mptr); |
bogdanm | 0:9b334a45a8ff | 85 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
bogdanm | 0:9b334a45a8ff | 86 | } |
bogdanm | 0:9b334a45a8ff | 87 | |
bogdanm | 0:9b334a45a8ff | 88 | /** Attach a function to call when a falling edge occurs on the input |
bogdanm | 0:9b334a45a8ff | 89 | * |
bogdanm | 0:9b334a45a8ff | 90 | * @param fptr A pointer to a void function, or 0 to set as none |
bogdanm | 0:9b334a45a8ff | 91 | */ |
bogdanm | 0:9b334a45a8ff | 92 | void fall(void (*fptr)(void)); |
bogdanm | 0:9b334a45a8ff | 93 | |
bogdanm | 0:9b334a45a8ff | 94 | /** Attach a member function to call when a falling edge occurs on the input |
bogdanm | 0:9b334a45a8ff | 95 | * |
bogdanm | 0:9b334a45a8ff | 96 | * @param tptr pointer to the object to call the member function on |
bogdanm | 0:9b334a45a8ff | 97 | * @param mptr pointer to the member function to be called |
bogdanm | 0:9b334a45a8ff | 98 | */ |
bogdanm | 0:9b334a45a8ff | 99 | template<typename T> |
bogdanm | 0:9b334a45a8ff | 100 | void fall(T* tptr, void (T::*mptr)(void)) { |
bogdanm | 0:9b334a45a8ff | 101 | _fall.attach(tptr, mptr); |
bogdanm | 0:9b334a45a8ff | 102 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
bogdanm | 0:9b334a45a8ff | 103 | } |
bogdanm | 0:9b334a45a8ff | 104 | |
bogdanm | 0:9b334a45a8ff | 105 | /** Set the input pin mode |
bogdanm | 0:9b334a45a8ff | 106 | * |
bogdanm | 0:9b334a45a8ff | 107 | * @param mode PullUp, PullDown, PullNone |
bogdanm | 0:9b334a45a8ff | 108 | */ |
bogdanm | 0:9b334a45a8ff | 109 | void mode(PinMode pull); |
bogdanm | 0:9b334a45a8ff | 110 | |
bogdanm | 0:9b334a45a8ff | 111 | /** Enable IRQ. This method depends on hw implementation, might enable one |
bogdanm | 0:9b334a45a8ff | 112 | * port interrupts. For further information, check gpio_irq_enable(). |
bogdanm | 0:9b334a45a8ff | 113 | */ |
bogdanm | 0:9b334a45a8ff | 114 | void enable_irq(); |
bogdanm | 0:9b334a45a8ff | 115 | |
bogdanm | 0:9b334a45a8ff | 116 | /** Disable IRQ. This method depends on hw implementation, might disable one |
bogdanm | 0:9b334a45a8ff | 117 | * port interrupts. For further information, check gpio_irq_disable(). |
bogdanm | 0:9b334a45a8ff | 118 | */ |
bogdanm | 0:9b334a45a8ff | 119 | void disable_irq(); |
bogdanm | 0:9b334a45a8ff | 120 | |
bogdanm | 0:9b334a45a8ff | 121 | static void _irq_handler(uint32_t id, gpio_irq_event event); |
bogdanm | 0:9b334a45a8ff | 122 | |
bogdanm | 0:9b334a45a8ff | 123 | protected: |
bogdanm | 0:9b334a45a8ff | 124 | gpio_t gpio; |
bogdanm | 0:9b334a45a8ff | 125 | gpio_irq_t gpio_irq; |
bogdanm | 0:9b334a45a8ff | 126 | |
bogdanm | 0:9b334a45a8ff | 127 | FunctionPointer _rise; |
bogdanm | 0:9b334a45a8ff | 128 | FunctionPointer _fall; |
bogdanm | 0:9b334a45a8ff | 129 | }; |
bogdanm | 0:9b334a45a8ff | 130 | |
bogdanm | 0:9b334a45a8ff | 131 | } // namespace mbed |
bogdanm | 0:9b334a45a8ff | 132 | |
bogdanm | 0:9b334a45a8ff | 133 | #endif |
bogdanm | 0:9b334a45a8ff | 134 | |
bogdanm | 0:9b334a45a8ff | 135 | #endif |