Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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