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 "BusOut.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 BusOut::BusOut(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 DigitalOut(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 BusOut::BusOut(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 DigitalOut(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 BusOut::~BusOut() {
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 BusOut::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 BusOut::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 BusOut& BusOut::operator= (int v) {
bryantaylor 0:eafc3fd41f75 77 // Underlying write is thread safe
bryantaylor 0:eafc3fd41f75 78 write(v);
bryantaylor 0:eafc3fd41f75 79 return *this;
bryantaylor 0:eafc3fd41f75 80 }
bryantaylor 0:eafc3fd41f75 81
bryantaylor 0:eafc3fd41f75 82 BusOut& BusOut::operator= (BusOut& rhs) {
bryantaylor 0:eafc3fd41f75 83 // Underlying write is thread safe
bryantaylor 0:eafc3fd41f75 84 write(rhs.read());
bryantaylor 0:eafc3fd41f75 85 return *this;
bryantaylor 0:eafc3fd41f75 86 }
bryantaylor 0:eafc3fd41f75 87
bryantaylor 0:eafc3fd41f75 88 DigitalOut& BusOut::operator[] (int index) {
bryantaylor 0:eafc3fd41f75 89 // No lock needed since _pin is not modified outside the constructor
bryantaylor 0:eafc3fd41f75 90 MBED_ASSERT(index >= 0 && index <= 16);
bryantaylor 0:eafc3fd41f75 91 MBED_ASSERT(_pin[index]);
bryantaylor 0:eafc3fd41f75 92 return *_pin[index];
bryantaylor 0:eafc3fd41f75 93 }
bryantaylor 0:eafc3fd41f75 94
bryantaylor 0:eafc3fd41f75 95 BusOut::operator int() {
bryantaylor 0:eafc3fd41f75 96 // Underlying read is thread safe
bryantaylor 0:eafc3fd41f75 97 return read();
bryantaylor 0:eafc3fd41f75 98 }
bryantaylor 0:eafc3fd41f75 99
bryantaylor 0:eafc3fd41f75 100 void BusOut::lock() {
bryantaylor 0:eafc3fd41f75 101 _mutex.lock();
bryantaylor 0:eafc3fd41f75 102 }
bryantaylor 0:eafc3fd41f75 103
bryantaylor 0:eafc3fd41f75 104 void BusOut::unlock() {
bryantaylor 0:eafc3fd41f75 105 _mutex.unlock();
bryantaylor 0:eafc3fd41f75 106 }
bryantaylor 0:eafc3fd41f75 107
bryantaylor 0:eafc3fd41f75 108 } // namespace mbed