00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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