Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

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 "BusInOut.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 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) {
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 DigitalInOut(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 BusInOut::BusInOut(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 DigitalInOut(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 BusInOut::~BusInOut() {
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 BusInOut::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 BusInOut::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 void BusInOut::output() {
nexpaq 0:6c56fb4bc5f0 77 lock();
nexpaq 0:6c56fb4bc5f0 78 for (int i=0; i<16; i++) {
nexpaq 0:6c56fb4bc5f0 79 if (_pin[i] != 0) {
nexpaq 0:6c56fb4bc5f0 80 _pin[i]->output();
nexpaq 0:6c56fb4bc5f0 81 }
nexpaq 0:6c56fb4bc5f0 82 }
nexpaq 0:6c56fb4bc5f0 83 unlock();
nexpaq 0:6c56fb4bc5f0 84 }
nexpaq 0:6c56fb4bc5f0 85
nexpaq 0:6c56fb4bc5f0 86 void BusInOut::input() {
nexpaq 0:6c56fb4bc5f0 87 lock();
nexpaq 0:6c56fb4bc5f0 88 for (int i=0; i<16; i++) {
nexpaq 0:6c56fb4bc5f0 89 if (_pin[i] != 0) {
nexpaq 0:6c56fb4bc5f0 90 _pin[i]->input();
nexpaq 0:6c56fb4bc5f0 91 }
nexpaq 0:6c56fb4bc5f0 92 }
nexpaq 0:6c56fb4bc5f0 93 unlock();
nexpaq 0:6c56fb4bc5f0 94 }
nexpaq 0:6c56fb4bc5f0 95
nexpaq 0:6c56fb4bc5f0 96 void BusInOut::mode(PinMode pull) {
nexpaq 0:6c56fb4bc5f0 97 lock();
nexpaq 0:6c56fb4bc5f0 98 for (int i=0; i<16; i++) {
nexpaq 0:6c56fb4bc5f0 99 if (_pin[i] != 0) {
nexpaq 0:6c56fb4bc5f0 100 _pin[i]->mode(pull);
nexpaq 0:6c56fb4bc5f0 101 }
nexpaq 0:6c56fb4bc5f0 102 }
nexpaq 0:6c56fb4bc5f0 103 unlock();
nexpaq 0:6c56fb4bc5f0 104 }
nexpaq 0:6c56fb4bc5f0 105
nexpaq 0:6c56fb4bc5f0 106 BusInOut& BusInOut::operator= (int v) {
nexpaq 0:6c56fb4bc5f0 107 // Underlying write is thread safe
nexpaq 0:6c56fb4bc5f0 108 write(v);
nexpaq 0:6c56fb4bc5f0 109 return *this;
nexpaq 0:6c56fb4bc5f0 110 }
nexpaq 0:6c56fb4bc5f0 111
nexpaq 0:6c56fb4bc5f0 112 BusInOut& BusInOut::operator= (BusInOut& rhs) {
nexpaq 0:6c56fb4bc5f0 113 // Underlying read is thread safe
nexpaq 0:6c56fb4bc5f0 114 write(rhs.read());
nexpaq 0:6c56fb4bc5f0 115 return *this;
nexpaq 0:6c56fb4bc5f0 116 }
nexpaq 0:6c56fb4bc5f0 117
nexpaq 0:6c56fb4bc5f0 118 DigitalInOut& BusInOut::operator[] (int index) {
nexpaq 0:6c56fb4bc5f0 119 // No lock needed since _pin is not modified outside the constructor
nexpaq 0:6c56fb4bc5f0 120 MBED_ASSERT(index >= 0 && index <= 16);
nexpaq 0:6c56fb4bc5f0 121 MBED_ASSERT(_pin[index]);
nexpaq 0:6c56fb4bc5f0 122 return *_pin[index];
nexpaq 0:6c56fb4bc5f0 123 }
nexpaq 0:6c56fb4bc5f0 124
nexpaq 0:6c56fb4bc5f0 125 BusInOut::operator int() {
nexpaq 0:6c56fb4bc5f0 126 // Underlying read is thread safe
nexpaq 0:6c56fb4bc5f0 127 return read();
nexpaq 0:6c56fb4bc5f0 128 }
nexpaq 0:6c56fb4bc5f0 129
nexpaq 0:6c56fb4bc5f0 130 void BusInOut::lock() {
nexpaq 0:6c56fb4bc5f0 131 _mutex.lock();
nexpaq 0:6c56fb4bc5f0 132 }
nexpaq 0:6c56fb4bc5f0 133
nexpaq 0:6c56fb4bc5f0 134 void BusInOut::unlock() {
nexpaq 0:6c56fb4bc5f0 135 _mutex.unlock();
nexpaq 0:6c56fb4bc5f0 136 }
nexpaq 0:6c56fb4bc5f0 137
nexpaq 0:6c56fb4bc5f0 138 } // namespace mbed