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