Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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