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