Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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