dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/hal/api/InterruptIn.h@9:6bb35cef007d, 2016-10-22 (annotated)
- Committer:
- cyberjoey
- Date:
- Sat Oct 22 01:31:58 2016 +0000
- Revision:
- 9:6bb35cef007d
- Parent:
- 1:55a6170b404f
WORKING
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* mbed Microcontroller Library |
nexpaq | 1:55a6170b404f | 2 | * Copyright (c) 2006-2013 ARM Limited |
nexpaq | 1:55a6170b404f | 3 | * |
nexpaq | 1:55a6170b404f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 1:55a6170b404f | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 1:55a6170b404f | 6 | * You may obtain a copy of the License at |
nexpaq | 1:55a6170b404f | 7 | * |
nexpaq | 1:55a6170b404f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 1:55a6170b404f | 9 | * |
nexpaq | 1:55a6170b404f | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 1:55a6170b404f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 1:55a6170b404f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 1:55a6170b404f | 13 | * See the License for the specific language governing permissions and |
nexpaq | 1:55a6170b404f | 14 | * limitations under the License. |
nexpaq | 1:55a6170b404f | 15 | */ |
nexpaq | 1:55a6170b404f | 16 | #ifndef MBED_INTERRUPTIN_H |
nexpaq | 1:55a6170b404f | 17 | #define MBED_INTERRUPTIN_H |
nexpaq | 1:55a6170b404f | 18 | |
nexpaq | 1:55a6170b404f | 19 | #include "platform.h" |
nexpaq | 1:55a6170b404f | 20 | |
nexpaq | 1:55a6170b404f | 21 | #if DEVICE_INTERRUPTIN |
nexpaq | 1:55a6170b404f | 22 | |
nexpaq | 1:55a6170b404f | 23 | #include "gpio_api.h" |
nexpaq | 1:55a6170b404f | 24 | #include "gpio_irq_api.h" |
nexpaq | 1:55a6170b404f | 25 | #include "Callback.h" |
nexpaq | 1:55a6170b404f | 26 | #include "critical.h" |
nexpaq | 1:55a6170b404f | 27 | #include "toolchain.h" |
nexpaq | 1:55a6170b404f | 28 | |
nexpaq | 1:55a6170b404f | 29 | namespace mbed { |
nexpaq | 1:55a6170b404f | 30 | |
nexpaq | 1:55a6170b404f | 31 | /** A digital interrupt input, used to call a function on a rising or falling edge |
nexpaq | 1:55a6170b404f | 32 | * |
nexpaq | 1:55a6170b404f | 33 | * @Note Synchronization level: Interrupt safe |
nexpaq | 1:55a6170b404f | 34 | * |
nexpaq | 1:55a6170b404f | 35 | * Example: |
nexpaq | 1:55a6170b404f | 36 | * @code |
nexpaq | 1:55a6170b404f | 37 | * // Flash an LED while waiting for events |
nexpaq | 1:55a6170b404f | 38 | * |
nexpaq | 1:55a6170b404f | 39 | * #include "mbed.h" |
nexpaq | 1:55a6170b404f | 40 | * |
nexpaq | 1:55a6170b404f | 41 | * InterruptIn event(p16); |
nexpaq | 1:55a6170b404f | 42 | * DigitalOut led(LED1); |
nexpaq | 1:55a6170b404f | 43 | * |
nexpaq | 1:55a6170b404f | 44 | * void trigger() { |
nexpaq | 1:55a6170b404f | 45 | * printf("triggered!\n"); |
nexpaq | 1:55a6170b404f | 46 | * } |
nexpaq | 1:55a6170b404f | 47 | * |
nexpaq | 1:55a6170b404f | 48 | * int main() { |
nexpaq | 1:55a6170b404f | 49 | * event.rise(&trigger); |
nexpaq | 1:55a6170b404f | 50 | * while(1) { |
nexpaq | 1:55a6170b404f | 51 | * led = !led; |
nexpaq | 1:55a6170b404f | 52 | * wait(0.25); |
nexpaq | 1:55a6170b404f | 53 | * } |
nexpaq | 1:55a6170b404f | 54 | * } |
nexpaq | 1:55a6170b404f | 55 | * @endcode |
nexpaq | 1:55a6170b404f | 56 | */ |
nexpaq | 1:55a6170b404f | 57 | class InterruptIn { |
nexpaq | 1:55a6170b404f | 58 | |
nexpaq | 1:55a6170b404f | 59 | public: |
nexpaq | 1:55a6170b404f | 60 | |
nexpaq | 1:55a6170b404f | 61 | /** Create an InterruptIn connected to the specified pin |
nexpaq | 1:55a6170b404f | 62 | * |
nexpaq | 1:55a6170b404f | 63 | * @param pin InterruptIn pin to connect to |
nexpaq | 1:55a6170b404f | 64 | * @param name (optional) A string to identify the object |
nexpaq | 1:55a6170b404f | 65 | */ |
nexpaq | 1:55a6170b404f | 66 | InterruptIn(PinName pin); |
nexpaq | 1:55a6170b404f | 67 | virtual ~InterruptIn(); |
nexpaq | 1:55a6170b404f | 68 | |
nexpaq | 1:55a6170b404f | 69 | /** Read the input, represented as 0 or 1 (int) |
nexpaq | 1:55a6170b404f | 70 | * |
nexpaq | 1:55a6170b404f | 71 | * @returns |
nexpaq | 1:55a6170b404f | 72 | * An integer representing the state of the input pin, |
nexpaq | 1:55a6170b404f | 73 | * 0 for logical 0, 1 for logical 1 |
nexpaq | 1:55a6170b404f | 74 | */ |
nexpaq | 1:55a6170b404f | 75 | int read(); |
nexpaq | 1:55a6170b404f | 76 | |
nexpaq | 1:55a6170b404f | 77 | /** An operator shorthand for read() |
nexpaq | 1:55a6170b404f | 78 | */ |
nexpaq | 1:55a6170b404f | 79 | operator int(); |
nexpaq | 1:55a6170b404f | 80 | |
nexpaq | 1:55a6170b404f | 81 | |
nexpaq | 1:55a6170b404f | 82 | /** Attach a function to call when a rising edge occurs on the input |
nexpaq | 1:55a6170b404f | 83 | * |
nexpaq | 1:55a6170b404f | 84 | * @param func A pointer to a void function, or 0 to set as none |
nexpaq | 1:55a6170b404f | 85 | */ |
nexpaq | 1:55a6170b404f | 86 | void rise(Callback<void()> func); |
nexpaq | 1:55a6170b404f | 87 | |
nexpaq | 1:55a6170b404f | 88 | /** Attach a member function to call when a rising edge occurs on the input |
nexpaq | 1:55a6170b404f | 89 | * |
nexpaq | 1:55a6170b404f | 90 | * @param obj pointer to the object to call the member function on |
nexpaq | 1:55a6170b404f | 91 | * @param method pointer to the member function to be called |
nexpaq | 1:55a6170b404f | 92 | * @deprecated |
nexpaq | 1:55a6170b404f | 93 | * The rise function does not support cv-qualifiers. Replaced by |
nexpaq | 1:55a6170b404f | 94 | * rise(callback(obj, method)). |
nexpaq | 1:55a6170b404f | 95 | */ |
nexpaq | 1:55a6170b404f | 96 | template<typename T, typename M> |
nexpaq | 1:55a6170b404f | 97 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
nexpaq | 1:55a6170b404f | 98 | "The rise function does not support cv-qualifiers. Replaced by " |
nexpaq | 1:55a6170b404f | 99 | "rise(callback(obj, method)).") |
nexpaq | 1:55a6170b404f | 100 | void rise(T *obj, M method) { |
nexpaq | 1:55a6170b404f | 101 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 102 | rise(callback(obj, method)); |
nexpaq | 1:55a6170b404f | 103 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 104 | } |
nexpaq | 1:55a6170b404f | 105 | |
nexpaq | 1:55a6170b404f | 106 | /** Attach a function to call when a falling edge occurs on the input |
nexpaq | 1:55a6170b404f | 107 | * |
nexpaq | 1:55a6170b404f | 108 | * @param func A pointer to a void function, or 0 to set as none |
nexpaq | 1:55a6170b404f | 109 | */ |
nexpaq | 1:55a6170b404f | 110 | void fall(Callback<void()> func); |
nexpaq | 1:55a6170b404f | 111 | |
nexpaq | 1:55a6170b404f | 112 | /** Attach a member function to call when a falling edge occurs on the input |
nexpaq | 1:55a6170b404f | 113 | * |
nexpaq | 1:55a6170b404f | 114 | * @param obj pointer to the object to call the member function on |
nexpaq | 1:55a6170b404f | 115 | * @param method pointer to the member function to be called |
nexpaq | 1:55a6170b404f | 116 | * @deprecated |
nexpaq | 1:55a6170b404f | 117 | * The rise function does not support cv-qualifiers. Replaced by |
nexpaq | 1:55a6170b404f | 118 | * rise(callback(obj, method)). |
nexpaq | 1:55a6170b404f | 119 | */ |
nexpaq | 1:55a6170b404f | 120 | template<typename T, typename M> |
nexpaq | 1:55a6170b404f | 121 | MBED_DEPRECATED_SINCE("mbed-os-5.1", |
nexpaq | 1:55a6170b404f | 122 | "The fall function does not support cv-qualifiers. Replaced by " |
nexpaq | 1:55a6170b404f | 123 | "fall(callback(obj, method)).") |
nexpaq | 1:55a6170b404f | 124 | void fall(T *obj, M method) { |
nexpaq | 1:55a6170b404f | 125 | core_util_critical_section_enter(); |
nexpaq | 1:55a6170b404f | 126 | fall(callback(obj, method)); |
nexpaq | 1:55a6170b404f | 127 | core_util_critical_section_exit(); |
nexpaq | 1:55a6170b404f | 128 | } |
nexpaq | 1:55a6170b404f | 129 | |
nexpaq | 1:55a6170b404f | 130 | /** Set the input pin mode |
nexpaq | 1:55a6170b404f | 131 | * |
nexpaq | 1:55a6170b404f | 132 | * @param mode PullUp, PullDown, PullNone |
nexpaq | 1:55a6170b404f | 133 | */ |
nexpaq | 1:55a6170b404f | 134 | void mode(PinMode pull); |
nexpaq | 1:55a6170b404f | 135 | |
nexpaq | 1:55a6170b404f | 136 | /** Enable IRQ. This method depends on hw implementation, might enable one |
nexpaq | 1:55a6170b404f | 137 | * port interrupts. For further information, check gpio_irq_enable(). |
nexpaq | 1:55a6170b404f | 138 | */ |
nexpaq | 1:55a6170b404f | 139 | void enable_irq(); |
nexpaq | 1:55a6170b404f | 140 | |
nexpaq | 1:55a6170b404f | 141 | /** Disable IRQ. This method depends on hw implementation, might disable one |
nexpaq | 1:55a6170b404f | 142 | * port interrupts. For further information, check gpio_irq_disable(). |
nexpaq | 1:55a6170b404f | 143 | */ |
nexpaq | 1:55a6170b404f | 144 | void disable_irq(); |
nexpaq | 1:55a6170b404f | 145 | |
nexpaq | 1:55a6170b404f | 146 | static void _irq_handler(uint32_t id, gpio_irq_event event); |
nexpaq | 1:55a6170b404f | 147 | |
nexpaq | 1:55a6170b404f | 148 | protected: |
nexpaq | 1:55a6170b404f | 149 | gpio_t gpio; |
nexpaq | 1:55a6170b404f | 150 | gpio_irq_t gpio_irq; |
nexpaq | 1:55a6170b404f | 151 | |
nexpaq | 1:55a6170b404f | 152 | Callback<void()> _rise; |
nexpaq | 1:55a6170b404f | 153 | Callback<void()> _fall; |
nexpaq | 1:55a6170b404f | 154 | }; |
nexpaq | 1:55a6170b404f | 155 | |
nexpaq | 1:55a6170b404f | 156 | } // namespace mbed |
nexpaq | 1:55a6170b404f | 157 | |
nexpaq | 1:55a6170b404f | 158 | #endif |
nexpaq | 1:55a6170b404f | 159 | |
nexpaq | 1:55a6170b404f | 160 | #endif |