Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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