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  * ThisThread::sleep_for(250);
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 function to call when a falling edge occurs on the input
108  *
109  * @param func A pointer to a void function, or 0 to set as none
110  */
111  void fall(Callback<void()> func);
112 
113  /** Set the input pin mode
114  *
115  * @param pull PullUp, PullDown, PullNone, PullDefault
116  * See PinNames.h for your target for definitions)
117  */
118  void mode(PinMode pull);
119 
120  /** Enable IRQ. This method depends on hardware implementation, might enable one
121  * port interrupts. For further information, check gpio_irq_enable().
122  */
123  void enable_irq();
124 
125  /** Disable IRQ. This method depends on hardware implementation, might disable one
126  * port interrupts. For further information, check gpio_irq_disable().
127  */
128  void disable_irq();
129 
130  static void _irq_handler(uint32_t id, gpio_irq_event event);
131 #if !defined(DOXYGEN_ONLY)
132 protected:
133  gpio_t gpio;
134  gpio_irq_t gpio_irq;
135 
136  Callback<void()> _rise;
137  Callback<void()> _fall;
138 
139  void irq_init(PinName pin);
140 #endif
141 };
142 
143 /** @}*/
144 
145 } // namespace mbed
146 
147 #endif
148 
149 #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:162
gpio_irq_event
GPIO IRQ events.
Definition: gpio_irq_api.h:34
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.
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:46
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.