takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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     {
00113         core_util_critical_section_enter();
00114         rise(callback(obj, method));
00115         core_util_critical_section_exit();
00116     }
00117 
00118     /** Attach a function to call when a falling edge occurs on the input
00119      *
00120      *  @param func A pointer to a void function, or 0 to set as none
00121      */
00122     void fall(Callback<void()> func);
00123 
00124     /** Attach a member function to call when a falling edge occurs on the input
00125      *
00126      *  @param obj pointer to the object to call the member function on
00127      *  @param method pointer to the member function to be called
00128      *  @deprecated
00129      *      The rise function does not support cv-qualifiers. Replaced by
00130      *      rise(callback(obj, method)).
00131      */
00132     template<typename T, typename M>
00133     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00134                           "The fall function does not support cv-qualifiers. Replaced by "
00135                           "fall(callback(obj, method)).")
00136     void fall(T *obj, M method)
00137     {
00138         core_util_critical_section_enter();
00139         fall(callback(obj, method));
00140         core_util_critical_section_exit();
00141     }
00142 
00143     /** Set the input pin mode
00144      *
00145      *  @param pull PullUp, PullDown, PullNone
00146      */
00147     void mode(PinMode pull);
00148 
00149     /** Enable IRQ. This method depends on hw implementation, might enable one
00150      *  port interrupts. For further information, check gpio_irq_enable().
00151      */
00152     void enable_irq();
00153 
00154     /** Disable IRQ. This method depends on hw implementation, might disable one
00155      *  port interrupts. For further information, check gpio_irq_disable().
00156      */
00157     void disable_irq();
00158 
00159     static void _irq_handler(uint32_t id, gpio_irq_event event);
00160 
00161 protected:
00162     gpio_t gpio;
00163     gpio_irq_t gpio_irq;
00164 
00165     Callback<void()> _rise;
00166     Callback<void()> _fall;
00167 
00168     void irq_init(PinName pin);
00169 };
00170 
00171 } // namespace mbed
00172 
00173 #endif
00174 
00175 #endif