Mistake on this page?
Report an issue in GitHub or email us
BusInOut.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_BUSINOUT_H
18 #define MBED_BUSINOUT_H
19 
20 #include "drivers/DigitalInOut.h"
21 #include "platform/PlatformMutex.h"
22 #include "platform/NonCopyable.h"
23 
24 namespace mbed {
25 /**
26  * \defgroup drivers_BusInOut BusInOut class
27  * \ingroup drivers-public-api-gpio
28  * @{
29  */
30 
31 /** A digital input output bus, used for setting the state of a collection of pins.
32  * Implemented as an array of DigitalInOut pins, the bus can be constructed by any
33  * pins without restriction other than being capable of digital input or output
34  * capabilities
35  *
36  * @note Synchronization level: Thread safe
37  */
38 class BusInOut : private NonCopyable<BusInOut> {
39 
40 public:
41 
42  /** Create a BusInOut, connected to the specified pins
43  *
44  * @param p0 DigitalInOut pin to connect to bus bit
45  * @param p1 DigitalInOut pin to connect to bus bit
46  * @param p2 DigitalInOut pin to connect to bus bit
47  * @param p3 DigitalInOut pin to connect to bus bit
48  * @param p4 DigitalInOut pin to connect to bus bit
49  * @param p5 DigitalInOut pin to connect to bus bit
50  * @param p6 DigitalInOut pin to connect to bus bit
51  * @param p7 DigitalInOut pin to connect to bus bit
52  * @param p8 DigitalInOut pin to connect to bus bit
53  * @param p9 DigitalInOut pin to connect to bus bit
54  * @param p10 DigitalInOut pin to connect to bus bit
55  * @param p11 DigitalInOut pin to connect to bus bit
56  * @param p12 DigitalInOut pin to connect to bus bit
57  * @param p13 DigitalInOut pin to connect to bus bit
58  * @param p14 DigitalInOut pin to connect to bus bit
59  * @param p15 DigitalInOut pin to connect to bus bit
60  *
61  * @note
62  * It is only required to specify as many pin variables as is required
63  * for the bus; the rest will default to NC (not connected)
64  */
65  BusInOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
66  PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC,
67  PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
68  PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
69 
70  /** Create a BusInOut, connected to the specified pins
71  *
72  * @param pins An array of pins (PinName) to construct a BusInOut from. The maximum
73  * number of pins in the array is 16 and any pins that are unspecified or are not to be
74  * connected must be specified as NC in the array that is passed in
75  */
76  BusInOut(PinName pins[16]);
77 
78  virtual ~BusInOut();
79 
80  /* Group: Access Methods */
81 
82  /** Write the value to the output bus
83  *
84  * @param value An integer specifying a bit to write for every corresponding DigitalInOut pin
85  */
86  void write(int value);
87 
88  /** Read the value currently output on the bus
89  *
90  * @returns
91  * An integer with each bit corresponding to associated DigitalInOut pin setting
92  */
93  int read();
94 
95  /** Set all the pins in bus as output
96  */
97  void output();
98 
99  /** Set all the pins in bus as an input
100  */
101  void input();
102 
103  /** Set the input pin mode for all the pins in bus
104  *
105  * @param pull PullUp, PullDown, PullNone
106  */
107  void mode(PinMode pull);
108 
109  /** Binary mask of bus pins connected to actual pins (not NC pins)
110  * If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
111  *
112  * @returns
113  * Binary mask of connected pins
114  */
115  int mask()
116  {
117  // No lock needed since _nc_mask is not modified outside the constructor
118  return _nc_mask;
119  }
120 
121  /** A shorthand for write()
122  * \sa BusInOut::write()
123  */
124  BusInOut &operator= (int v);
126 
127  /** Access to particular bit in random-iterator fashion
128  * @param index Bit Position
129  */
130  DigitalInOut &operator[](int index);
131 
132  /** A shorthand for read()
133  * \sa BusInOut::read()
134  */
135  operator int();
136 
137 protected:
138 #if !defined(DOXYGEN_ONLY)
139  virtual void lock();
140  virtual void unlock();
141  DigitalInOut *_pin[16];
142 
143  /* Mask of bus's NC pins
144  * If bit[n] is set to 1 - pin is connected
145  * if bit[n] is cleared - pin is not connected (NC)
146  */
147  int _nc_mask;
148 
149  PlatformMutex _mutex;
150 #endif //!defined(DOXYGEN_ONLY)
151 };
152 
153 /** @}*/
154 
155 } // namespace mbed
156 
157 #endif
DigitalInOut & operator[](int index)
Access to particular bit in random-iterator fashion.
A digital input output bus, used for setting the state of a collection of pins.
Definition: BusInOut.h:38
int mask()
Binary mask of bus pins connected to actual pins (not NC pins) If bus pin is in NC state make corresp...
Definition: BusInOut.h:115
void input()
Set all the pins in bus as an input.
A digital input/output, used for setting or reading a bi-directional pin.
Definition: DigitalInOut.h:36
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:162
The PlatformMutex class is used to synchronize the execution of threads.
Definition: PlatformMutex.h:47
void output()
Set all the pins in bus as output.
BusInOut(PinName p0, PinName p1=NC, PinName p2=NC, PinName p3=NC, PinName p4=NC, PinName p5=NC, PinName p6=NC, PinName p7=NC, PinName p8=NC, PinName p9=NC, PinName p10=NC, PinName p11=NC, PinName p12=NC, PinName p13=NC, PinName p14=NC, PinName p15=NC)
Create a BusInOut, connected to the specified pins.
void mode(PinMode pull)
Set the input pin mode for all the pins in bus.
void write(int value)
Write the value to the output bus.
int read()
Read the value currently output on the bus.
BusInOut & operator=(int v)
A shorthand for write()
Definition: ATHandler.h:46
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.