mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
BusOut.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/BusOut.h" 00018 #include "platform/mbed_assert.h" 00019 00020 namespace mbed { 00021 00022 BusOut::BusOut(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 DigitalOut(pins[i]) : 0; 00030 if (pins[i] != NC) { 00031 _nc_mask |= (1 << i); 00032 } 00033 } 00034 } 00035 00036 BusOut::BusOut(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 DigitalOut(pins[i]) : 0; 00042 if (pins[i] != NC) { 00043 _nc_mask |= (1 << i); 00044 } 00045 } 00046 } 00047 00048 BusOut::~BusOut() 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 BusOut::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 BusOut::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 BusOut &BusOut::operator= (int v) 00083 { 00084 // Underlying write is thread safe 00085 write(v); 00086 return *this; 00087 } 00088 00089 BusOut &BusOut::operator= (BusOut &rhs) 00090 { 00091 // Underlying write is thread safe 00092 write(rhs.read()); 00093 return *this; 00094 } 00095 00096 DigitalOut &BusOut::operator[](int index) 00097 { 00098 // No lock needed since _pin is not modified outside the constructor 00099 MBED_ASSERT(index >= 0 && index < 16); 00100 MBED_ASSERT(_pin[index]); 00101 return *_pin[index]; 00102 } 00103 00104 BusOut::operator int() 00105 { 00106 // Underlying read is thread safe 00107 return read(); 00108 } 00109 00110 void BusOut::lock() 00111 { 00112 _mutex.lock(); 00113 } 00114 00115 void BusOut::unlock() 00116 { 00117 _mutex.unlock(); 00118 } 00119 00120 } // namespace mbed
Generated on Tue Jul 12 2022 20:41:14 by 1.7.2