Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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