1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef MBED_BUSOUT_H
17 #define MBED_BUSOUT_H
18 
19 #include "drivers/DigitalOut.h"
20 #include "platform/PlatformMutex.h"
21 
22 namespace mbed {
23 /** \addtogroup drivers */
24 /** @{*/
25 
26 /** A digital output bus, used for setting the state of a collection of pins
27  */
28 class BusOut {
29 
30 public:
31 
32     /** Create an BusOut, connected to the specified pins
33      *
34      *  @param p<n> DigitalOut pin to connect to bus bit <n> (p5-p30, NC)
35      *
36      *  @Note Synchronization level: Thread safe
37      *
38      *  @note
39      *  It is only required to specify as many pin variables as is required
40      *  for the bus; the rest will default to NC (not connected)
41      */
42     BusOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
43            PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC,
44            PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
45            PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
46 
47     BusOut(PinName pins[16]);
48 
49     virtual ~BusOut();
50 
51     /** Write the value to the output bus
52      *
53      *  @param value An integer specifying a bit to write for every corresponding DigitalOut pin
54      */
55     void write(int value);
56 
57     /** Read the value currently output on the bus
58      *
59      *  @returns
60      *    An integer with each bit corresponding to associated DigitalOut pin setting
61      */
62     int read();
63 
64     /** Binary mask of bus pins connected to actual pins (not NC pins)
65      *  If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
66      *
67      *  @returns
68      *    Binary mask of connected pins
69      */
mask()70     int mask() {
71         // No lock needed since _nc_mask is not modified outside the constructor
72         return _nc_mask;
73     }
74 
75     /** A shorthand for write()
76      */
77     BusOut& operator= (int v);
78     BusOut& operator= (BusOut& rhs);
79 
80     /** Access to particular bit in random-iterator fashion
81      */
82     DigitalOut& operator[] (int index);
83 
84     /** A shorthand for read()
85      */
86     operator int();
87 
88 protected:
89     virtual void lock();
90     virtual void unlock();
91     DigitalOut* _pin[16];
92 
93     /** Mask of bus's NC pins
94      * If bit[n] is set to 1 - pin is connected
95      * if bit[n] is cleared - pin is not connected (NC)
96      */
97     int _nc_mask;
98 
99     PlatformMutex _mutex;
100 
101    /* disallow copy constructor and assignment operators */
102 private:
103     BusOut(const BusOut&);
104     BusOut & operator = (const BusOut&);
105 };
106 
107 } // namespace mbed
108 
109 #endif
110 
111 /** @}*/
112