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