BA / Mbed OS BaBoRo_test2
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers InterruptIn.h Source File

InterruptIn.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_INTERRUPTIN_H
00017 #define MBED_INTERRUPTIN_H
00018 
00019 #include "platform/platform.h"
00020 
00021 #if defined (DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
00022 
00023 #include "hal/gpio_api.h"
00024 #include "hal/gpio_irq_api.h"
00025 #include "platform/Callback.h"
00026 #include "platform/mbed_critical.h"
00027 #include "platform/mbed_toolchain.h"
00028 #include "platform/NonCopyable.h"
00029 
00030 namespace mbed {
00031 /** \addtogroup drivers */
00032 
00033 /** A digital interrupt input, used to call a function on a rising or falling edge
00034  *
00035  * @note Synchronization level: Interrupt safe
00036  *
00037  * Example:
00038  * @code
00039  * // Flash an LED while waiting for events
00040  *
00041  * #include "mbed.h"
00042  *
00043  * InterruptIn event(p16);
00044  * DigitalOut led(LED1);
00045  *
00046  * void trigger() {
00047  *     printf("triggered!\n");
00048  * }
00049  *
00050  * int main() {
00051  *     event.rise(&trigger);
00052  *     while(1) {
00053  *         led = !led;
00054  *         wait(0.25);
00055  *     }
00056  * }
00057  * @endcode
00058  * @ingroup drivers
00059  */
00060 class InterruptIn : private NonCopyable<InterruptIn> {
00061 
00062 public:
00063 
00064     /** Create an InterruptIn connected to the specified pin
00065      *
00066      *  @param pin InterruptIn pin to connect to
00067      */
00068     InterruptIn(PinName pin);
00069 
00070     /** Create an InterruptIn connected to the specified pin,
00071      *  and the pin configured to the specified mode.
00072      *
00073      *  @param pin InterruptIn pin to connect to
00074      *  @param mode The mode to set the pin to (PullUp/PullDown/etc.)
00075      */
00076     InterruptIn(PinName pin, PinMode mode);
00077 
00078     virtual ~InterruptIn();
00079 
00080     /** Read the input, represented as 0 or 1 (int)
00081      *
00082      *  @returns
00083      *    An integer representing the state of the input pin,
00084      *    0 for logical 0, 1 for logical 1
00085      */
00086     int read();
00087 
00088     /** An operator shorthand for read()
00089      */
00090     operator int();
00091 
00092 
00093     /** Attach a function to call when a rising edge occurs on the input
00094      *
00095      *  @param func A pointer to a void function, or 0 to set as none
00096      */
00097     void rise(Callback<void()> func);
00098 
00099     /** Attach a member function to call when a rising edge occurs on the input
00100      *
00101      *  @param obj pointer to the object to call the member function on
00102      *  @param method pointer to the member function to be called
00103      *  @deprecated
00104      *      The rise function does not support cv-qualifiers. Replaced by
00105      *      rise(callback(obj, method)).
00106      */
00107     template<typename T, typename M>
00108     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00109         "The rise function does not support cv-qualifiers. Replaced by "
00110         "rise(callback(obj, method)).")
00111     void rise(T *obj, M method) {
00112         core_util_critical_section_enter();
00113         rise(callback(obj, method));
00114         core_util_critical_section_exit();
00115     }
00116 
00117     /** Attach a function to call when a falling edge occurs on the input
00118      *
00119      *  @param func A pointer to a void function, or 0 to set as none
00120      */
00121     void fall(Callback<void()> func);
00122 
00123     /** Attach a member function to call when a falling edge occurs on the input
00124      *
00125      *  @param obj pointer to the object to call the member function on
00126      *  @param method pointer to the member function to be called
00127      *  @deprecated
00128      *      The rise function does not support cv-qualifiers. Replaced by
00129      *      rise(callback(obj, method)).
00130      */
00131     template<typename T, typename M>
00132     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00133         "The fall function does not support cv-qualifiers. Replaced by "
00134         "fall(callback(obj, method)).")
00135     void fall(T *obj, M method) {
00136         core_util_critical_section_enter();
00137         fall(callback(obj, method));
00138         core_util_critical_section_exit();
00139     }
00140 
00141     /** Set the input pin mode
00142      *
00143      *  @param pull PullUp, PullDown, PullNone
00144      */
00145     void mode(PinMode pull);
00146 
00147     /** Enable IRQ. This method depends on hw implementation, might enable one
00148      *  port interrupts. For further information, check gpio_irq_enable().
00149      */
00150     void enable_irq();
00151 
00152     /** Disable IRQ. This method depends on hw implementation, might disable one
00153      *  port interrupts. For further information, check gpio_irq_disable().
00154      */
00155     void disable_irq();
00156 
00157     static void _irq_handler(uint32_t id, gpio_irq_event event);
00158 
00159 protected:
00160     gpio_t gpio;
00161     gpio_irq_t gpio_irq;
00162 
00163     Callback<void()> _rise;
00164     Callback<void()> _fall;
00165 
00166     void irq_init(PinName pin);
00167 };
00168 
00169 } // namespace mbed
00170 
00171 #endif
00172 
00173 #endif