| 1 | /* mbed Microcontroller Library - InterruptIn |
|---|
| 2 | * Copyright (c) 2006-2011 ARM Limited. All rights reserved. |
|---|
| 3 | */ |
|---|
| 4 | |
|---|
| 5 | #ifndef MBED_INTERRUPTIN_H |
|---|
| 6 | #define MBED_INTERRUPTIN_H |
|---|
| 7 | |
|---|
| 8 | #include "device.h" |
|---|
| 9 | |
|---|
| 10 | #if DEVICE_INTERRUPTIN |
|---|
| 11 | |
|---|
| 12 | #include "platform.h" |
|---|
| 13 | #include "PinNames.h" |
|---|
| 14 | #include "PeripheralNames.h" |
|---|
| 15 | #include "Base.h" |
|---|
| 16 | #include "FunctionPointer.h" |
|---|
| 17 | |
|---|
| 18 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
|---|
| 19 | #define CHANNEL_NUM 48 |
|---|
| 20 | #elif defined(TARGET_LPC11U24) |
|---|
| 21 | #define CHANNEL_NUM 8 |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | namespace mbed { |
|---|
| 25 | |
|---|
| 26 | /* Class: InterruptIn |
|---|
| 27 | * A digital interrupt input, used to call a function on a rising or falling edge |
|---|
| 28 | * |
|---|
| 29 | * Example: |
|---|
| 30 | * > // Flash an LED while waiting for events |
|---|
| 31 | * > |
|---|
| 32 | * > #include "mbed.h" |
|---|
| 33 | * > |
|---|
| 34 | * > InterruptIn event(p16); |
|---|
| 35 | * > DigitalOut led(LED1); |
|---|
| 36 | * > |
|---|
| 37 | * > void trigger() { |
|---|
| 38 | * > printf("triggered!\n"); |
|---|
| 39 | * > } |
|---|
| 40 | * > |
|---|
| 41 | * > int main() { |
|---|
| 42 | * > event.rise(&trigger); |
|---|
| 43 | * > while(1) { |
|---|
| 44 | * > led = !led; |
|---|
| 45 | * > wait(0.25); |
|---|
| 46 | * > } |
|---|
| 47 | * > } |
|---|
| 48 | */ |
|---|
| 49 | class InterruptIn : public Base { |
|---|
| 50 | |
|---|
| 51 | public: |
|---|
| 52 | |
|---|
| 53 | /* Constructor: InterruptIn |
|---|
| 54 | * Create an InterruptIn connected to the specified pin |
|---|
| 55 | * |
|---|
| 56 | * Variables: |
|---|
| 57 | * pin - InterruptIn pin to connect to |
|---|
| 58 | * name - (optional) A string to identify the object |
|---|
| 59 | */ |
|---|
| 60 | InterruptIn(PinName pin, const char *name = NULL); |
|---|
| 61 | #if defined(TARGET_LPC11U24) |
|---|
| 62 | virtual ~InterruptIn(); |
|---|
| 63 | #endif |
|---|
| 64 | |
|---|
| 65 | int read(); |
|---|
| 66 | #ifdef MBED_OPERATORS |
|---|
| 67 | operator int(); |
|---|
| 68 | |
|---|
| 69 | #endif |
|---|
| 70 | |
|---|
| 71 | /* Function: rise |
|---|
| 72 | * Attach a function to call when a rising edge occurs on the input |
|---|
| 73 | * |
|---|
| 74 | * Variables: |
|---|
| 75 | * fptr - A pointer to a void function, or 0 to set as none |
|---|
| 76 | */ |
|---|
| 77 | void rise(void (*fptr)(void)); |
|---|
| 78 | |
|---|
| 79 | /* Function: rise |
|---|
| 80 | * Attach a member function to call when a rising edge occurs on the input |
|---|
| 81 | * |
|---|
| 82 | * Variables: |
|---|
| 83 | * tptr - pointer to the object to call the member function on |
|---|
| 84 | * mptr - pointer to the member function to be called |
|---|
| 85 | */ |
|---|
| 86 | template<typename T> |
|---|
| 87 | void rise(T* tptr, void (T::*mptr)(void)) { |
|---|
| 88 | _rise.attach(tptr, mptr); |
|---|
| 89 | setup_interrupt(1, 1); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /* Function: fall |
|---|
| 93 | * Attach a function to call when a falling edge occurs on the input |
|---|
| 94 | * |
|---|
| 95 | * Variables: |
|---|
| 96 | * fptr - A pointer to a void function, or 0 to set as none |
|---|
| 97 | */ |
|---|
| 98 | void fall(void (*fptr)(void)); |
|---|
| 99 | |
|---|
| 100 | /* Function: fall |
|---|
| 101 | * Attach a member function to call when a falling edge occurs on the input |
|---|
| 102 | * |
|---|
| 103 | * Variables: |
|---|
| 104 | * tptr - pointer to the object to call the member function on |
|---|
| 105 | * mptr - pointer to the member function to be called |
|---|
| 106 | */ |
|---|
| 107 | template<typename T> |
|---|
| 108 | void fall(T* tptr, void (T::*mptr)(void)) { |
|---|
| 109 | _fall.attach(tptr, mptr); |
|---|
| 110 | setup_interrupt(0, 1); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /* Function: mode |
|---|
| 114 | * Set the input pin mode |
|---|
| 115 | * |
|---|
| 116 | * Variables: |
|---|
| 117 | * mode - PullUp, PullDown, PullNone |
|---|
| 118 | */ |
|---|
| 119 | void mode(PinMode pull); |
|---|
| 120 | |
|---|
| 121 | static InterruptIn *_irq_objects[CHANNEL_NUM]; |
|---|
| 122 | |
|---|
| 123 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
|---|
| 124 | static void _irq(); |
|---|
| 125 | #elif defined(TARGET_LPC11U24) |
|---|
| 126 | static void handle_interrupt_in(unsigned int channel); |
|---|
| 127 | static void _irq0(); static void _irq1(); |
|---|
| 128 | static void _irq2(); static void _irq3(); |
|---|
| 129 | static void _irq4(); static void _irq5(); |
|---|
| 130 | static void _irq6(); static void _irq7(); |
|---|
| 131 | #endif |
|---|
| 132 | |
|---|
| 133 | protected: |
|---|
| 134 | PinName _pin; |
|---|
| 135 | #if defined(TARGET_LPC11U24) |
|---|
| 136 | Channel _channel; |
|---|
| 137 | #endif |
|---|
| 138 | FunctionPointer _rise; |
|---|
| 139 | FunctionPointer _fall; |
|---|
| 140 | |
|---|
| 141 | void setup_interrupt(int rising, int enable); |
|---|
| 142 | |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | } // namespace mbed |
|---|
| 146 | |
|---|
| 147 | #endif |
|---|
| 148 | |
|---|
| 149 | #endif |
|---|