Mistake on this page?
Report an issue in GitHub or email us
PortOut.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_PORTOUT_H
18 #define MBED_PORTOUT_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_PORTOUT || defined(DOXYGEN_ONLY)
23 
24 #include "hal/port_api.h"
25 
26 namespace mbed {
27 /**
28  * \defgroup drivers_PortOut PortOut class
29  * \ingroup drivers-public-api-gpio
30  * @{
31  */
32 
33 /** A multiple pin digital output
34  *
35  * @note Synchronization level: Interrupt safe
36  *
37  * Example:
38  * @code
39  * // Toggle all four LEDs
40  *
41  * #include "mbed.h"
42  *
43  * // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23
44  * #define LED_MASK 0x00B40000
45  *
46  * PortOut ledport(Port1, LED_MASK);
47  *
48  * int main() {
49  * while(1) {
50  * ledport = LED_MASK;
51  * wait(1);
52  * ledport = 0;
53  * wait(1);
54  * }
55  * }
56  * @endcode
57  */
58 class PortOut {
59 public:
60 
61  /** Create a PortOut, connected to the specified port
62  *
63  * @param port Port to connect to (as defined in target's PortNames.h)
64  * @param mask Bitmask defines which port pins are an output (0 - ignore, 1 - include)
65  */
66  PortOut(PortName port, int mask = 0xFFFFFFFF);
67 
68  /** Write the value to the output port
69  *
70  * @param value An integer specifying a bit to write for every corresponding PortOut pin
71  */
72  void write(int value)
73  {
74  port_write(&_port, value);
75  }
76 
77  /** Read the value currently output on the port
78  *
79  * @returns
80  * An integer with each bit corresponding to associated pin value
81  */
82  int read()
83  {
84  return port_read(&_port);
85  }
86 
87  /** A shorthand for write()
88  * \sa PortOut::write()
89  */
90  PortOut &operator= (int value)
91  {
92  write(value);
93  return *this;
94  }
95 
96  /** A shorthand for read()
97  * \sa PortOut::read()
98  */
100  {
101  write(rhs.read());
102  return *this;
103  }
104 
105  /** A shorthand for read()
106  * \sa PortOut::read()
107  */
108  operator int()
109  {
110  return read();
111  }
112 
113 private:
114  port_t _port;
115 };
116 
117 /** @}*/
118 
119 } // namespace mbed
120 
121 #endif
122 
123 #endif
void write(int value)
Write the value to the output port.
Definition: PortOut.h:72
PortOut(PortName port, int mask=0xFFFFFFFF)
Create a PortOut, connected to the specified port.
A multiple pin digital output.
Definition: PortOut.h:58
int port_read(port_t *obj)
Read the current value on the port.
PortOut & operator=(int value)
A shorthand for write()
Definition: PortOut.h:90
struct port_s port_t
Port HAL structure.
Definition: port_api.h:33
int read()
Read the value currently output on the port.
Definition: PortOut.h:82
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.