mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
mbed library release version 165

Who changed what in which revision?

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