IOTON Technology / mbed-ton

Fork of mbed-dev by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BusInOut.h Source File

BusInOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #ifndef MBED_BUSINOUT_H
00017 #define MBED_BUSINOUT_H
00018 
00019 #include "drivers/DigitalInOut.h"
00020 #include "platform/PlatformMutex.h"
00021 
00022 namespace mbed {
00023 /** \addtogroup drivers */
00024 
00025 /** A digital input output bus, used for setting the state of a collection of pins
00026  *
00027  * @note Synchronization level: Thread safe
00028  * @ingroup drivers
00029  */
00030 class BusInOut {
00031 
00032 public:
00033 
00034     /** Create an BusInOut, connected to the specified pins
00035      *
00036      *  @param p0 DigitalInOut pin to connect to bus bit
00037      *  @param p1 DigitalInOut pin to connect to bus bit
00038      *  @param p2 DigitalInOut pin to connect to bus bit
00039      *  @param p3 DigitalInOut pin to connect to bus bit
00040      *  @param p4 DigitalInOut pin to connect to bus bit
00041      *  @param p5 DigitalInOut pin to connect to bus bit
00042      *  @param p6 DigitalInOut pin to connect to bus bit
00043      *  @param p7 DigitalInOut pin to connect to bus bit
00044      *  @param p8 DigitalInOut pin to connect to bus bit
00045      *  @param p9 DigitalInOut pin to connect to bus bit
00046      *  @param p10 DigitalInOut pin to connect to bus bit
00047      *  @param p11 DigitalInOut pin to connect to bus bit
00048      *  @param p12 DigitalInOut pin to connect to bus bit
00049      *  @param p13 DigitalInOut pin to connect to bus bit
00050      *  @param p14 DigitalInOut pin to connect to bus bit
00051      *  @param p15 DigitalInOut pin to connect to bus bit
00052      *
00053      *  @note
00054      *  It is only required to specify as many pin variables as is required
00055      *  for the bus; the rest will default to NC (not connected)
00056      */
00057     BusInOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC,
00058              PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC,
00059              PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC,
00060              PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC);
00061 
00062     /** Create an BusInOut, connected to the specified pins
00063      *
00064      *  @param pins An array of pins to construct a BusInOut from
00065      */
00066     BusInOut(PinName pins[16]);
00067 
00068     virtual ~BusInOut();
00069 
00070     /* Group: Access Methods */
00071 
00072     /** Write the value to the output bus
00073      *
00074      *  @param value An integer specifying a bit to write for every corresponding DigitalInOut pin
00075      */
00076     void write(int value);
00077 
00078     /** Read the value currently output on the bus
00079      *
00080      *  @returns
00081      *    An integer with each bit corresponding to associated DigitalInOut pin setting
00082      */
00083     int read();
00084 
00085     /** Set as an output
00086      */
00087     void output();
00088 
00089     /** Set as an input
00090      */
00091     void input();
00092 
00093     /** Set the input pin mode
00094      *
00095      *  @param pull PullUp, PullDown, PullNone
00096      */
00097     void mode(PinMode pull);
00098 
00099     /** Binary mask of bus pins connected to actual pins (not NC pins)
00100      *  If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1
00101      *
00102      *  @returns
00103      *    Binary mask of connected pins
00104      */
00105     int mask() {
00106         // No lock needed since _nc_mask is not modified outside the constructor
00107         return _nc_mask;
00108     }
00109 
00110      /** A shorthand for write()
00111     * \sa BusInOut::write()
00112       */
00113     BusInOut& operator= (int v);
00114     BusInOut& operator= (BusInOut& rhs);
00115 
00116     /** Access to particular bit in random-iterator fashion
00117      * @param index  Bit Position
00118      */
00119     DigitalInOut& operator[] (int index);
00120 
00121     /** A shorthand for read()
00122      * \sa BusInOut::read()
00123      */
00124     operator int();
00125 
00126 protected:
00127     virtual void lock();
00128     virtual void unlock();
00129     DigitalInOut* _pin[16];
00130 
00131     /* Mask of bus's NC pins
00132      * If bit[n] is set to 1 - pin is connected
00133      * if bit[n] is cleared - pin is not connected (NC)
00134      */
00135     int _nc_mask;
00136 
00137     PlatformMutex _mutex;
00138 
00139     /* disallow copy constructor and assignment operators */
00140 private:
00141     BusInOut(const BusInOut&);
00142     BusInOut & operator = (const BusInOut&);
00143 };
00144 
00145 } // namespace mbed
00146 
00147 #endif