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