Mistake on this page?
Report an issue in GitHub or email us
DigitalInOut.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_DIGITALINOUT_H
18 #define MBED_DIGITALINOUT_H
19 
20 #include "platform/platform.h"
21 
22 #include "hal/gpio_api.h"
23 #include "platform/mbed_critical.h"
24 
25 namespace mbed {
26 /** \addtogroup drivers */
27 
28 /** A digital input/output, used for setting or reading a bi-directional pin
29  *
30  * @note Synchronization level: Interrupt safe
31  * @ingroup drivers
32  */
33 class DigitalInOut {
34 
35 public:
36  /** Create a DigitalInOut connected to the specified pin
37  *
38  * @param pin DigitalInOut pin to connect to
39  */
40  DigitalInOut(PinName pin) : gpio()
41  {
42  // No lock needed in the constructor
43  gpio_init_in(&gpio, pin);
44  }
45 
46  /** Create a DigitalInOut connected to the specified pin
47  *
48  * @param pin DigitalInOut pin to connect to
49  * @param direction the initial direction of the pin
50  * @param mode the initial mode of the pin
51  * @param value the initial value of the pin if is an output
52  */
53  DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
54  {
55  // No lock needed in the constructor
56  gpio_init_inout(&gpio, pin, direction, mode, value);
57  }
58 
59  /** Set the output, specified as 0 or 1 (int)
60  *
61  * @param value An integer specifying the pin output value,
62  * 0 for logical 0, 1 (or any other non-zero value) for logical 1
63  */
64  void write(int value)
65  {
66  // Thread safe / atomic HAL call
67  gpio_write(&gpio, value);
68  }
69 
70  /** Return the output setting, represented as 0 or 1 (int)
71  *
72  * @returns
73  * an integer representing the output setting of the pin if it is an output,
74  * or read the input if set as an input
75  */
76  int read()
77  {
78  // Thread safe / atomic HAL call
79  return gpio_read(&gpio);
80  }
81 
82  /** Set as an output
83  */
84  void output()
85  {
87  gpio_dir(&gpio, PIN_OUTPUT);
89  }
90 
91  /** Set as an input
92  */
93  void input()
94  {
96  gpio_dir(&gpio, PIN_INPUT);
98  }
99 
100  /** Set the input pin mode
101  *
102  * @param pull PullUp, PullDown, PullNone, OpenDrain
103  */
104  void mode(PinMode pull)
105  {
107  gpio_mode(&gpio, pull);
109  }
110 
111  /** Return the output setting, represented as 0 or 1 (int)
112  *
113  * @returns
114  * Non zero value if pin is connected to uc GPIO
115  * 0 if gpio object was initialized with NC
116  */
118  {
119  // Thread safe / atomic HAL call
120  return gpio_is_connected(&gpio);
121  }
122 
123  /** A shorthand for write()
124  * \sa DigitalInOut::write()
125  * @code
126  * DigitalInOut inout(PIN);
127  * DigitalIn button(BUTTON1);
128  * inout.output();
129  *
130  * inout = button; // Equivalent to inout.write(button.read())
131  * @endcode
132  */
134  {
135  // Underlying write is thread safe
136  write(value);
137  return *this;
138  }
139 
140  /**A shorthand for write() using the assignment operator which copies the
141  * state from the DigitalInOut argument.
142  * \sa DigitalInOut::write()
143  */
145  {
147  write(rhs.read());
149  return *this;
150  }
151 
152  /** A shorthand for read()
153  * \sa DigitalInOut::read()
154  * @code
155  * DigitalInOut inout(PIN);
156  * DigitalOut led(LED1);
157  *
158  * inout.input();
159  * led = inout; // Equivalent to led.write(inout.read())
160  * @endcode
161  */
162  operator int()
163  {
164  // Underlying call is thread safe
165  return read();
166  }
167 
168 protected:
169 #if !defined(DOXYGEN_ONLY)
170  gpio_t gpio;
171 #endif //!defined(DOXYGEN_ONLY)
172 };
173 
174 } // namespace mbed
175 
176 #endif
void gpio_init_inout(gpio_t *gpio, PinName pin, PinDirection direction, PinMode mode, int value)
Init the pin to be in/out.
void output()
Set as an output.
Definition: DigitalInOut.h:84
void input()
Set as an input.
Definition: DigitalInOut.h:93
DigitalInOut & operator=(int value)
A shorthand for write()
Definition: DigitalInOut.h:133
A digital input/output, used for setting or reading a bi-directional pin.
Definition: DigitalInOut.h:33
DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value)
Create a DigitalInOut connected to the specified pin.
Definition: DigitalInOut.h:53
void write(int value)
Set the output, specified as 0 or 1 (int)
Definition: DigitalInOut.h:64
void core_util_critical_section_exit(void)
Mark the end of a critical section.
void gpio_write(gpio_t *obj, int value)
Set the output value.
int read()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalInOut.h:76
int gpio_read(gpio_t *obj)
Read the input value.
void core_util_critical_section_enter(void)
Mark the start of a critical section.
void gpio_dir(gpio_t *obj, PinDirection direction)
Set the pin direction.
void gpio_mode(gpio_t *obj, PinMode mode)
Set the input pin mode.
int is_connected()
Return the output setting, represented as 0 or 1 (int)
Definition: DigitalInOut.h:117
void mode(PinMode pull)
Set the input pin mode.
Definition: DigitalInOut.h:104
DigitalInOut(PinName pin)
Create a DigitalInOut connected to the specified pin.
Definition: DigitalInOut.h:40
Definition: AnalogIn.h:28
void gpio_init_in(gpio_t *gpio, PinName pin)
Init the input pin and set mode to PullDefault.
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.