initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

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