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/BusInOut.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 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) {
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 DigitalInOut(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 BusInOut::BusInOut(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 DigitalInOut(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 BusInOut::~BusInOut() {
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 BusInOut::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 BusInOut::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 void BusInOut::output() {
borlanic 0:02dd72d1d465 77 lock();
borlanic 0:02dd72d1d465 78 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 79 if (_pin[i] != 0) {
borlanic 0:02dd72d1d465 80 _pin[i]->output();
borlanic 0:02dd72d1d465 81 }
borlanic 0:02dd72d1d465 82 }
borlanic 0:02dd72d1d465 83 unlock();
borlanic 0:02dd72d1d465 84 }
borlanic 0:02dd72d1d465 85
borlanic 0:02dd72d1d465 86 void BusInOut::input() {
borlanic 0:02dd72d1d465 87 lock();
borlanic 0:02dd72d1d465 88 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 89 if (_pin[i] != 0) {
borlanic 0:02dd72d1d465 90 _pin[i]->input();
borlanic 0:02dd72d1d465 91 }
borlanic 0:02dd72d1d465 92 }
borlanic 0:02dd72d1d465 93 unlock();
borlanic 0:02dd72d1d465 94 }
borlanic 0:02dd72d1d465 95
borlanic 0:02dd72d1d465 96 void BusInOut::mode(PinMode pull) {
borlanic 0:02dd72d1d465 97 lock();
borlanic 0:02dd72d1d465 98 for (int i=0; i<16; i++) {
borlanic 0:02dd72d1d465 99 if (_pin[i] != 0) {
borlanic 0:02dd72d1d465 100 _pin[i]->mode(pull);
borlanic 0:02dd72d1d465 101 }
borlanic 0:02dd72d1d465 102 }
borlanic 0:02dd72d1d465 103 unlock();
borlanic 0:02dd72d1d465 104 }
borlanic 0:02dd72d1d465 105
borlanic 0:02dd72d1d465 106 BusInOut& BusInOut::operator= (int v) {
borlanic 0:02dd72d1d465 107 // Underlying write is thread safe
borlanic 0:02dd72d1d465 108 write(v);
borlanic 0:02dd72d1d465 109 return *this;
borlanic 0:02dd72d1d465 110 }
borlanic 0:02dd72d1d465 111
borlanic 0:02dd72d1d465 112 BusInOut& BusInOut::operator= (BusInOut& rhs) {
borlanic 0:02dd72d1d465 113 // Underlying read is thread safe
borlanic 0:02dd72d1d465 114 write(rhs.read());
borlanic 0:02dd72d1d465 115 return *this;
borlanic 0:02dd72d1d465 116 }
borlanic 0:02dd72d1d465 117
borlanic 0:02dd72d1d465 118 DigitalInOut& BusInOut::operator[] (int index) {
borlanic 0:02dd72d1d465 119 // No lock needed since _pin is not modified outside the constructor
borlanic 0:02dd72d1d465 120 MBED_ASSERT(index >= 0 && index <= 16);
borlanic 0:02dd72d1d465 121 MBED_ASSERT(_pin[index]);
borlanic 0:02dd72d1d465 122 return *_pin[index];
borlanic 0:02dd72d1d465 123 }
borlanic 0:02dd72d1d465 124
borlanic 0:02dd72d1d465 125 BusInOut::operator int() {
borlanic 0:02dd72d1d465 126 // Underlying read is thread safe
borlanic 0:02dd72d1d465 127 return read();
borlanic 0:02dd72d1d465 128 }
borlanic 0:02dd72d1d465 129
borlanic 0:02dd72d1d465 130 void BusInOut::lock() {
borlanic 0:02dd72d1d465 131 _mutex.lock();
borlanic 0:02dd72d1d465 132 }
borlanic 0:02dd72d1d465 133
borlanic 0:02dd72d1d465 134 void BusInOut::unlock() {
borlanic 0:02dd72d1d465 135 _mutex.unlock();
borlanic 0:02dd72d1d465 136 }
borlanic 0:02dd72d1d465 137
borlanic 0:02dd72d1d465 138 } // namespace mbed