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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.