mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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