mbed library sources

Committer:
ebrus
Date:
Wed Jul 27 18:35:32 2016 +0000
Revision:
0:0a673c671a56
4

Who changed what in which revision?

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