1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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