Mistake on this page?
Report an issue in GitHub or email us
PortInOut.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_PORTINOUT_H
18 #define MBED_PORTINOUT_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_PORTINOUT || defined(DOXYGEN_ONLY)
23 
24 #include "hal/port_api.h"
25 #include "platform/mbed_critical.h"
26 
27 namespace mbed {
28 /** \addtogroup drivers */
29 
30 /** A multiple pin digital in/out used to set/read multiple bi-directional pins
31  *
32  * @note Synchronization level: Interrupt safe
33  * @ingroup drivers
34  */
35 class PortInOut {
36 public:
37 
38  /** Create an PortInOut, connected to the specified port
39  *
40  * @param port Port to connect to (Port0-Port5)
41  * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
42  */
43  PortInOut(PortName port, int mask = 0xFFFFFFFF)
44  {
46  port_init(&_port, port, mask, PIN_INPUT);
48  }
49 
50  /** Write the value to the output port
51  *
52  * @param value An integer specifying a bit to write for every corresponding port pin
53  */
54  void write(int value)
55  {
56  port_write(&_port, value);
57  }
58 
59  /** Read the value currently output on the port
60  *
61  * @returns
62  * An integer with each bit corresponding to associated port pin setting
63  */
64  int read()
65  {
66  return port_read(&_port);
67  }
68 
69  /** Set as an output
70  */
71  void output()
72  {
74  port_dir(&_port, PIN_OUTPUT);
76  }
77 
78  /** Set as an input
79  */
80  void input()
81  {
83  port_dir(&_port, PIN_INPUT);
85  }
86 
87  /** Set the input pin mode
88  *
89  * @param mode PullUp, PullDown, PullNone, OpenDrain
90  */
91  void mode(PinMode mode)
92  {
94  port_mode(&_port, mode);
96  }
97 
98  /** A shorthand for write()
99  * \sa PortInOut::write()
100  */
101  PortInOut &operator= (int value)
102  {
103  write(value);
104  return *this;
105  }
106 
107  /** A shorthand for write()
108  * \sa PortInOut::write()
109  */
111  {
112  write(rhs.read());
113  return *this;
114  }
115 
116  /** A shorthand for read()
117  * \sa PortInOut::read()
118  */
119  operator int()
120  {
121  return read();
122  }
123 
124 private:
125  port_t _port;
126 };
127 
128 } // namespace mbed
129 
130 #endif
131 
132 #endif
int read()
Read the value currently output on the port.
Definition: PortInOut.h:64
A multiple pin digital in/out used to set/read multiple bi-directional pins.
Definition: PortInOut.h:35
void core_util_critical_section_exit(void)
Mark the end of a critical section.
void mode(PinMode mode)
Set the input pin mode.
Definition: PortInOut.h:91
void core_util_critical_section_enter(void)
Mark the start of a critical section.
int port_read(port_t *obj)
Read the current value on the port.
void write(int value)
Write the value to the output port.
Definition: PortInOut.h:54
void port_init(port_t *obj, PortName port, int mask, PinDirection dir)
Initilize the port.
PortInOut(PortName port, int mask=0xFFFFFFFF)
Create an PortInOut, connected to the specified port.
Definition: PortInOut.h:43
void port_dir(port_t *obj, PinDirection dir)
Set port direction (in/out)
void input()
Set as an input.
Definition: PortInOut.h:80
PortInOut & operator=(int value)
A shorthand for write()
Definition: PortInOut.h:101
struct port_s port_t
Port HAL structure.
Definition: port_api.h:33
void output()
Set as an output.
Definition: PortInOut.h:71
void port_mode(port_t *obj, PinMode mode)
Set the input port mode.
void port_write(port_t *obj, int value)
Write value to the port.
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.