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