Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:02dd72d1d465 1 /* mbed Microcontroller Library
borlanic 0:02dd72d1d465 2 * Copyright (c) 2006-2013 ARM Limited
borlanic 0:02dd72d1d465 3 *
borlanic 0:02dd72d1d465 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:02dd72d1d465 5 * you may not use this file except in compliance with the License.
borlanic 0:02dd72d1d465 6 * You may obtain a copy of the License at
borlanic 0:02dd72d1d465 7 *
borlanic 0:02dd72d1d465 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:02dd72d1d465 9 *
borlanic 0:02dd72d1d465 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:02dd72d1d465 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:02dd72d1d465 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:02dd72d1d465 13 * See the License for the specific language governing permissions and
borlanic 0:02dd72d1d465 14 * limitations under the License.
borlanic 0:02dd72d1d465 15 */
borlanic 0:02dd72d1d465 16 #include "drivers/BusOut.h"
borlanic 0:02dd72d1d465 17 #include "platform/mbed_assert.h"
borlanic 0:02dd72d1d465 18
borlanic 0:02dd72d1d465 19 namespace mbed {
borlanic 0:02dd72d1d465 20
borlanic 0:02dd72d1d465 21 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) {
borlanic 0:02dd72d1d465 22 PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
borlanic 0:02dd72d1d465 23
borlanic 0:02dd72d1d465 24 // No lock needed in the constructor
borlanic 0:02dd72d1d465 25 _nc_mask = 0;
borlanic 0:02dd72d1d465 26 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 27 _pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
borlanic 0:02dd72d1d465 28 if (pins[i] != NC) {
borlanic 0:02dd72d1d465 29 _nc_mask |= (1 << i);
borlanic 0:02dd72d1d465 30 }
borlanic 0:02dd72d1d465 31 }
borlanic 0:02dd72d1d465 32 }
borlanic 0:02dd72d1d465 33
borlanic 0:02dd72d1d465 34 BusOut::BusOut(PinName pins[16]) {
borlanic 0:02dd72d1d465 35 // No lock needed in the constructor
borlanic 0:02dd72d1d465 36 _nc_mask = 0;
borlanic 0:02dd72d1d465 37 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 38 _pin[i] = (pins[i] != NC) ? new DigitalOut(pins[i]) : 0;
borlanic 0:02dd72d1d465 39 if (pins[i] != NC) {
borlanic 0:02dd72d1d465 40 _nc_mask |= (1 << i);
borlanic 0:02dd72d1d465 41 }
borlanic 0:02dd72d1d465 42 }
borlanic 0:02dd72d1d465 43 }
borlanic 0:02dd72d1d465 44
borlanic 0:02dd72d1d465 45 BusOut::~BusOut() {
borlanic 0:02dd72d1d465 46 // No lock needed in the destructor
borlanic 0:02dd72d1d465 47 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 48 if (_pin[i] != 0) {
borlanic 0:02dd72d1d465 49 delete _pin[i];
borlanic 0:02dd72d1d465 50 }
borlanic 0:02dd72d1d465 51 }
borlanic 0:02dd72d1d465 52 }
borlanic 0:02dd72d1d465 53
borlanic 0:02dd72d1d465 54 void BusOut::write(int value) {
borlanic 0:02dd72d1d465 55 lock();
borlanic 0:02dd72d1d465 56 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 57 if (_pin[i] != 0) {
borlanic 0:02dd72d1d465 58 _pin[i]->write((value >> i) & 1);
borlanic 0:02dd72d1d465 59 }
borlanic 0:02dd72d1d465 60 }
borlanic 0:02dd72d1d465 61 unlock();
borlanic 0:02dd72d1d465 62 }
borlanic 0:02dd72d1d465 63
borlanic 0:02dd72d1d465 64 int BusOut::read() {
borlanic 0:02dd72d1d465 65 lock();
borlanic 0:02dd72d1d465 66 int v = 0;
borlanic 0:02dd72d1d465 67 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 68 if (_pin[i] != 0) {
borlanic 0:02dd72d1d465 69 v |= _pin[i]->read() << i;
borlanic 0:02dd72d1d465 70 }
borlanic 0:02dd72d1d465 71 }
borlanic 0:02dd72d1d465 72 unlock();
borlanic 0:02dd72d1d465 73 return v;
borlanic 0:02dd72d1d465 74 }
borlanic 0:02dd72d1d465 75
borlanic 0:02dd72d1d465 76 BusOut& BusOut::operator= (int v) {
borlanic 0:02dd72d1d465 77 // Underlying write is thread safe
borlanic 0:02dd72d1d465 78 write(v);
borlanic 0:02dd72d1d465 79 return *this;
borlanic 0:02dd72d1d465 80 }
borlanic 0:02dd72d1d465 81
borlanic 0:02dd72d1d465 82 BusOut& BusOut::operator= (BusOut& rhs) {
borlanic 0:02dd72d1d465 83 // Underlying write is thread safe
borlanic 0:02dd72d1d465 84 write(rhs.read());
borlanic 0:02dd72d1d465 85 return *this;
borlanic 0:02dd72d1d465 86 }
borlanic 0:02dd72d1d465 87
borlanic 0:02dd72d1d465 88 DigitalOut& BusOut::operator[] (int index) {
borlanic 0:02dd72d1d465 89 // No lock needed since _pin is not modified outside the constructor
borlanic 0:02dd72d1d465 90 MBED_ASSERT(index >= 0 && index <= 16);
borlanic 0:02dd72d1d465 91 MBED_ASSERT(_pin[index]);
borlanic 0:02dd72d1d465 92 return *_pin[index];
borlanic 0:02dd72d1d465 93 }
borlanic 0:02dd72d1d465 94
borlanic 0:02dd72d1d465 95 BusOut::operator int() {
borlanic 0:02dd72d1d465 96 // Underlying read is thread safe
borlanic 0:02dd72d1d465 97 return read();
borlanic 0:02dd72d1d465 98 }
borlanic 0:02dd72d1d465 99
borlanic 0:02dd72d1d465 100 void BusOut::lock() {
borlanic 0:02dd72d1d465 101 _mutex.lock();
borlanic 0:02dd72d1d465 102 }
borlanic 0:02dd72d1d465 103
borlanic 0:02dd72d1d465 104 void BusOut::unlock() {
borlanic 0:02dd72d1d465 105 _mutex.unlock();
borlanic 0:02dd72d1d465 106 }
borlanic 0:02dd72d1d465 107
borlanic 0:02dd72d1d465 108 } // namespace mbed