Mbed OS Reference
Loading...
Searching...
No Matches
PortInOut.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_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
26namespace mbed {
27/**
28 * \defgroup drivers_PortInOut PortInOut class
29 * \ingroup drivers-public-api-gpio
30 * @{
31 */
32
33/** A multiple pin digital in/out used to set/read multiple bi-directional pins
34 *
35 * @note Synchronization level: Interrupt safe
36 */
37class PortInOut {
38public:
39
40 /** Create an PortInOut, connected to the specified port
41 *
42 * @param port Port to connect to (Port0-Port5)
43 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
44 */
45 PortInOut(PortName port, int mask = 0xFFFFFFFF);
46
47 /** Write the value to the output port
48 *
49 * @param value An integer specifying a bit to write for every corresponding port pin
50 */
51 void write(int value)
52 {
53 port_write(&_port, value);
54 }
55
56 /** Read the value currently output on the port
57 *
58 * @returns
59 * An integer with each bit corresponding to associated port pin setting
60 */
61 int read()
62 {
63 return port_read(&_port);
64 }
65
66 /** Set as an output
67 */
68 void output();
69
70 /** Set as an input
71 */
72 void input();
73
74 /** Set the input pin mode
75 *
76 * @param mode PullUp, PullDown, PullNone, OpenDrain
77 */
78 void mode(PinMode mode);
79
80 /** A shorthand for write()
81 * \sa PortInOut::write()
82 */
84 {
85 write(value);
86 return *this;
87 }
88
89 /** A shorthand for write()
90 * \sa PortInOut::write()
91 */
93 {
94 write(rhs.read());
95 return *this;
96 }
97
98 /** A shorthand for read()
99 * \sa PortInOut::read()
100 */
101 operator int()
102 {
103 return read();
104 }
105
106private:
107 port_t _port;
108};
109
110/** @}*/
111
112} // namespace mbed
113
114#endif
115
116#endif
A multiple pin digital in/out used to set/read multiple bi-directional pins.
Definition: PortInOut.h:37
void write(int value)
Write the value to the output port.
Definition: PortInOut.h:51
PortInOut(PortName port, int mask=0xFFFFFFFF)
Create an PortInOut, connected to the specified port.
void mode(PinMode mode)
Set the input pin mode.
void output()
Set as an output.
PortInOut & operator=(int value)
A shorthand for write()
Definition: PortInOut.h:83
int read()
Read the value currently output on the port.
Definition: PortInOut.h:61
void input()
Set as an input.
int port_read(port_t *obj)
Read the current value on the port.
void port_write(port_t *obj, int value)
Write value to the port.
struct port_s port_t
Port HAL structure.
Definition: port_api.h:33