Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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