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 /** \addtogroup drivers */
33 
34 /** A digital interrupt input, used to call a function on a rising or falling edge
35  *
36  * @note Synchronization level: Interrupt safe
37  *
38  * Example:
39  * @code
40  * // Flash an LED while waiting for events
41  *
42  * #include "mbed.h"
43  *
44  * InterruptIn event(p16);
45  * DigitalOut led(LED1);
46  *
47  * void trigger() {
48  * printf("triggered!\n");
49  * }
50  *
51  * int main() {
52  * // register trigger() to be called upon the rising edge of event
53  * event.rise(&trigger);
54  * while(1) {
55  * led = !led;
56  * wait(0.25);
57  * }
58  * }
59  * @endcode
60  * @ingroup drivers
61  */
62 class InterruptIn : private NonCopyable<InterruptIn> {
63 
64 public:
65 
66  /** Create an InterruptIn connected to the specified pin
67  *
68  * @param pin InterruptIn pin to connect to
69  */
70  InterruptIn(PinName pin);
71 
72  /** Create an InterruptIn connected to the specified pin,
73  * and the pin configured to the specified mode.
74  *
75  * @param pin InterruptIn pin to connect to
76  * @param mode Desired Pin mode configuration.
77  * (Valid values could be PullNone, PullDown, PullUp and PullDefault.
78  * See PinNames.h for your target for definitions)
79  *
80  */
81  InterruptIn(PinName pin, PinMode mode);
82 
83  virtual ~InterruptIn();
84 
85  /** Read the input, represented as 0 or 1 (int)
86  *
87  * @returns
88  * An integer representing the state of the input pin,
89  * 0 for logical 0, 1 for logical 1
90  */
91  int read();
92 
93  /** An operator shorthand for read()
94  */
95  operator int();
96 
97 
98  /** Attach a function to call when a rising edge occurs on the input
99  *
100  * @param func A pointer to a void function, or 0 to set as none
101  */
102  void rise(Callback<void()> func);
103 
104  /** Attach a member function to call when a rising edge occurs on the input
105  *
106  * @param obj pointer to the object to call the member function on
107  * @param method pointer to the member function to be called
108  * @deprecated
109  * The rise function does not support cv-qualifiers. Replaced by
110  * rise(callback(obj, method)).
111  */
112  template<typename T, typename M>
113  MBED_DEPRECATED_SINCE("mbed-os-5.1",
114  "The rise function does not support cv-qualifiers. Replaced by "
115  "rise(callback(obj, method)).")
116  void rise(T *obj, M method)
117  {
119  rise(callback(obj, method));
121  }
122 
123  /** Attach a function to call when a falling edge occurs on the input
124  *
125  * @param func A pointer to a void function, or 0 to set as none
126  */
127  void fall(Callback<void()> func);
128 
129  /** Attach a member function to call when a falling edge occurs on the input
130  *
131  * @param obj pointer to the object to call the member function on
132  * @param method pointer to the member function to be called
133  * @deprecated
134  * The rise function does not support cv-qualifiers. Replaced by
135  * rise(callback(obj, method)).
136  */
137  template<typename T, typename M>
138  MBED_DEPRECATED_SINCE("mbed-os-5.1",
139  "The fall function does not support cv-qualifiers. Replaced by "
140  "fall(callback(obj, method)).")
141  void fall(T *obj, M method)
142  {
144  fall(callback(obj, method));
146  }
147 
148  /** Set the input pin mode
149  *
150  * @param pull PullUp, PullDown, PullNone, PullDefault
151  * See PinNames.h for your target for definitions)
152  */
153  void mode(PinMode pull);
154 
155  /** Enable IRQ. This method depends on hardware implementation, might enable one
156  * port interrupts. For further information, check gpio_irq_enable().
157  */
158  void enable_irq();
159 
160  /** Disable IRQ. This method depends on hardware implementation, might disable one
161  * port interrupts. For further information, check gpio_irq_disable().
162  */
163  void disable_irq();
164 
165  static void _irq_handler(uint32_t id, gpio_irq_event event);
166 #if !defined(DOXYGEN_ONLY)
167 protected:
168  gpio_t gpio;
169  gpio_irq_t gpio_irq;
170 
171  Callback<void()> _rise;
172  Callback<void()> _fall;
173 
174  void irq_init(PinName pin);
175 #endif
176 };
177 
178 } // namespace mbed
179 
180 #endif
181 
182 #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:168
gpio_irq_event
GPIO IRQ events.
Definition: gpio_irq_api.h:33
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
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:41
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.
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:62
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.