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