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