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