,,

Fork of Application by Daniel Sygut

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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