mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BusInOut.cpp Source File

BusInOut.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "drivers/BusInOut.h"
00018 #include "platform/mbed_assert.h"
00019 
00020 namespace mbed {
00021 
00022 BusInOut::BusInOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15)
00023 {
00024     PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
00025 
00026     // No lock needed in the constructor
00027     _nc_mask = 0;
00028     for (int i = 0; i < 16; i++) {
00029         _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
00030         if (pins[i] != NC) {
00031             _nc_mask |= (1 << i);
00032         }
00033     }
00034 }
00035 
00036 BusInOut::BusInOut(PinName pins[16])
00037 {
00038     // No lock needed in the constructor
00039     _nc_mask = 0;
00040     for (int i = 0; i < 16; i++) {
00041         _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
00042         if (pins[i] != NC) {
00043             _nc_mask |= (1 << i);
00044         }
00045     }
00046 }
00047 
00048 BusInOut::~BusInOut()
00049 {
00050     // No lock needed in the destructor
00051     for (int i = 0; i < 16; i++) {
00052         if (_pin[i] != 0) {
00053             delete _pin[i];
00054         }
00055     }
00056 }
00057 
00058 void BusInOut::write(int value)
00059 {
00060     lock();
00061     for (int i = 0; i < 16; i++) {
00062         if (_pin[i] != 0) {
00063             _pin[i]->write((value >> i) & 1);
00064         }
00065     }
00066     unlock();
00067 }
00068 
00069 int BusInOut::read()
00070 {
00071     lock();
00072     int v = 0;
00073     for (int i = 0; i < 16; i++) {
00074         if (_pin[i] != 0) {
00075             v |= _pin[i]->read() << i;
00076         }
00077     }
00078     unlock();
00079     return v;
00080 }
00081 
00082 void BusInOut::output()
00083 {
00084     lock();
00085     for (int i = 0; i < 16; i++) {
00086         if (_pin[i] != 0) {
00087             _pin[i]->output();
00088         }
00089     }
00090     unlock();
00091 }
00092 
00093 void BusInOut::input()
00094 {
00095     lock();
00096     for (int i = 0; i < 16; i++) {
00097         if (_pin[i] != 0) {
00098             _pin[i]->input();
00099         }
00100     }
00101     unlock();
00102 }
00103 
00104 void BusInOut::mode(PinMode pull)
00105 {
00106     lock();
00107     for (int i = 0; i < 16; i++) {
00108         if (_pin[i] != 0) {
00109             _pin[i]->mode(pull);
00110         }
00111     }
00112     unlock();
00113 }
00114 
00115 BusInOut &BusInOut::operator= (int v)
00116 {
00117     // Underlying write is thread safe
00118     write(v);
00119     return *this;
00120 }
00121 
00122 BusInOut &BusInOut::operator= (BusInOut &rhs)
00123 {
00124     // Underlying read is thread safe
00125     write(rhs.read());
00126     return *this;
00127 }
00128 
00129 DigitalInOut &BusInOut::operator[](int index)
00130 {
00131     // No lock needed since _pin is not modified outside the constructor
00132     MBED_ASSERT(index >= 0 && index < 16);
00133     MBED_ASSERT(_pin[index]);
00134     return *_pin[index];
00135 }
00136 
00137 BusInOut::operator int()
00138 {
00139     // Underlying read is thread safe
00140     return read();
00141 }
00142 
00143 void BusInOut::lock()
00144 {
00145     _mutex.lock();
00146 }
00147 
00148 void BusInOut::unlock()
00149 {
00150     _mutex.unlock();
00151 }
00152 
00153 } // namespace mbed