mbed library sources
Fork of mbed-src by
api/InterruptIn.h@445:9a3ffe6cfa19, 2015-01-14 (annotated)
- Committer:
- moirans2
- Date:
- Wed Jan 14 20:53:08 2015 +0000
- Revision:
- 445:9a3ffe6cfa19
- Parent:
- 69:49e45cb70de1
internal clock stm32L051
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 | #include "FunctionPointer.h" |
bogdanm | 13:0645d8841f51 | 26 | |
bogdanm | 13:0645d8841f51 | 27 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 28 | |
bogdanm | 13:0645d8841f51 | 29 | /** A digital interrupt input, used to call a function on a rising or falling edge |
bogdanm | 13:0645d8841f51 | 30 | * |
bogdanm | 13:0645d8841f51 | 31 | * Example: |
bogdanm | 13:0645d8841f51 | 32 | * @code |
bogdanm | 13:0645d8841f51 | 33 | * // Flash an LED while waiting for events |
bogdanm | 13:0645d8841f51 | 34 | * |
bogdanm | 13:0645d8841f51 | 35 | * #include "mbed.h" |
bogdanm | 13:0645d8841f51 | 36 | * |
bogdanm | 13:0645d8841f51 | 37 | * InterruptIn event(p16); |
bogdanm | 13:0645d8841f51 | 38 | * DigitalOut led(LED1); |
bogdanm | 13:0645d8841f51 | 39 | * |
bogdanm | 13:0645d8841f51 | 40 | * void trigger() { |
bogdanm | 13:0645d8841f51 | 41 | * printf("triggered!\n"); |
bogdanm | 13:0645d8841f51 | 42 | * } |
bogdanm | 13:0645d8841f51 | 43 | * |
bogdanm | 13:0645d8841f51 | 44 | * int main() { |
bogdanm | 13:0645d8841f51 | 45 | * event.rise(&trigger); |
bogdanm | 13:0645d8841f51 | 46 | * while(1) { |
bogdanm | 13:0645d8841f51 | 47 | * led = !led; |
bogdanm | 13:0645d8841f51 | 48 | * wait(0.25); |
bogdanm | 13:0645d8841f51 | 49 | * } |
bogdanm | 13:0645d8841f51 | 50 | * } |
bogdanm | 13:0645d8841f51 | 51 | * @endcode |
bogdanm | 13:0645d8841f51 | 52 | */ |
bogdanm | 13:0645d8841f51 | 53 | class InterruptIn { |
bogdanm | 13:0645d8841f51 | 54 | |
bogdanm | 13:0645d8841f51 | 55 | public: |
bogdanm | 13:0645d8841f51 | 56 | |
bogdanm | 13:0645d8841f51 | 57 | /** Create an InterruptIn connected to the specified pin |
bogdanm | 13:0645d8841f51 | 58 | * |
bogdanm | 13:0645d8841f51 | 59 | * @param pin InterruptIn pin to connect to |
bogdanm | 13:0645d8841f51 | 60 | * @param name (optional) A string to identify the object |
bogdanm | 13:0645d8841f51 | 61 | */ |
bogdanm | 13:0645d8841f51 | 62 | InterruptIn(PinName pin); |
bogdanm | 13:0645d8841f51 | 63 | virtual ~InterruptIn(); |
bogdanm | 13:0645d8841f51 | 64 | |
bogdanm | 13:0645d8841f51 | 65 | int read(); |
bogdanm | 13:0645d8841f51 | 66 | #ifdef MBED_OPERATORS |
bogdanm | 13:0645d8841f51 | 67 | operator int(); |
bogdanm | 13:0645d8841f51 | 68 | |
bogdanm | 13:0645d8841f51 | 69 | #endif |
bogdanm | 13:0645d8841f51 | 70 | |
bogdanm | 13:0645d8841f51 | 71 | /** Attach a function to call when a rising edge occurs on the input |
bogdanm | 13:0645d8841f51 | 72 | * |
bogdanm | 13:0645d8841f51 | 73 | * @param fptr A pointer to a void function, or 0 to set as none |
bogdanm | 13:0645d8841f51 | 74 | */ |
mbed_official | 36:ab3ee77451e7 | 75 | void rise(void (*fptr)(void)); |
bogdanm | 13:0645d8841f51 | 76 | |
bogdanm | 13:0645d8841f51 | 77 | /** Attach a member function to call when a rising edge occurs on the input |
bogdanm | 13:0645d8841f51 | 78 | * |
bogdanm | 13:0645d8841f51 | 79 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 80 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 81 | */ |
bogdanm | 13:0645d8841f51 | 82 | template<typename T> |
mbed_official | 36:ab3ee77451e7 | 83 | void rise(T* tptr, void (T::*mptr)(void)) { |
mbed_official | 36:ab3ee77451e7 | 84 | _rise.attach(tptr, mptr); |
mbed_official | 36:ab3ee77451e7 | 85 | gpio_irq_set(&gpio_irq, IRQ_RISE, 1); |
bogdanm | 13:0645d8841f51 | 86 | } |
bogdanm | 13:0645d8841f51 | 87 | |
bogdanm | 13:0645d8841f51 | 88 | /** Attach a function to call when a falling edge occurs on the input |
bogdanm | 13:0645d8841f51 | 89 | * |
bogdanm | 13:0645d8841f51 | 90 | * @param fptr A pointer to a void function, or 0 to set as none |
bogdanm | 13:0645d8841f51 | 91 | */ |
mbed_official | 36:ab3ee77451e7 | 92 | void fall(void (*fptr)(void)); |
bogdanm | 13:0645d8841f51 | 93 | |
bogdanm | 13:0645d8841f51 | 94 | /** Attach a member function to call when a falling edge occurs on the input |
bogdanm | 13:0645d8841f51 | 95 | * |
bogdanm | 13:0645d8841f51 | 96 | * @param tptr pointer to the object to call the member function on |
bogdanm | 13:0645d8841f51 | 97 | * @param mptr pointer to the member function to be called |
bogdanm | 13:0645d8841f51 | 98 | */ |
bogdanm | 13:0645d8841f51 | 99 | template<typename T> |
mbed_official | 36:ab3ee77451e7 | 100 | void fall(T* tptr, void (T::*mptr)(void)) { |
mbed_official | 36:ab3ee77451e7 | 101 | _fall.attach(tptr, mptr); |
mbed_official | 36:ab3ee77451e7 | 102 | gpio_irq_set(&gpio_irq, IRQ_FALL, 1); |
bogdanm | 13:0645d8841f51 | 103 | } |
bogdanm | 13:0645d8841f51 | 104 | |
bogdanm | 13:0645d8841f51 | 105 | /** Set the input pin mode |
bogdanm | 13:0645d8841f51 | 106 | * |
bogdanm | 13:0645d8841f51 | 107 | * @param mode PullUp, PullDown, PullNone |
bogdanm | 13:0645d8841f51 | 108 | */ |
bogdanm | 13:0645d8841f51 | 109 | void mode(PinMode pull); |
bogdanm | 13:0645d8841f51 | 110 | |
mbed_official | 69:49e45cb70de1 | 111 | /** Enable IRQ. This method depends on hw implementation, might enable one |
mbed_official | 69:49e45cb70de1 | 112 | * port interrupts. For further information, check gpio_irq_enable(). |
mbed_official | 35:371630885ad6 | 113 | */ |
mbed_official | 35:371630885ad6 | 114 | void enable_irq(); |
mbed_official | 35:371630885ad6 | 115 | |
mbed_official | 69:49e45cb70de1 | 116 | /** Disable IRQ. This method depends on hw implementation, might disable one |
mbed_official | 69:49e45cb70de1 | 117 | * port interrupts. For further information, check gpio_irq_disable(). |
mbed_official | 35:371630885ad6 | 118 | */ |
mbed_official | 35:371630885ad6 | 119 | void disable_irq(); |
mbed_official | 35:371630885ad6 | 120 | |
bogdanm | 13:0645d8841f51 | 121 | static void _irq_handler(uint32_t id, gpio_irq_event event); |
bogdanm | 13:0645d8841f51 | 122 | |
bogdanm | 13:0645d8841f51 | 123 | protected: |
bogdanm | 13:0645d8841f51 | 124 | gpio_t gpio; |
bogdanm | 13:0645d8841f51 | 125 | gpio_irq_t gpio_irq; |
bogdanm | 13:0645d8841f51 | 126 | |
mbed_official | 36:ab3ee77451e7 | 127 | FunctionPointer _rise; |
mbed_official | 36:ab3ee77451e7 | 128 | FunctionPointer _fall; |
bogdanm | 13:0645d8841f51 | 129 | }; |
bogdanm | 13:0645d8841f51 | 130 | |
bogdanm | 13:0645d8841f51 | 131 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 132 | |
bogdanm | 13:0645d8841f51 | 133 | #endif |
bogdanm | 13:0645d8841f51 | 134 | |
bogdanm | 13:0645d8841f51 | 135 | #endif |