mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

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