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