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/BusIn.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 BusIn::BusIn(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 DigitalIn(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 BusIn::BusIn(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 DigitalIn(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 BusIn::~BusIn() {
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 int BusIn::read() {
borlanic 0:380207fcb5c1 55 int v = 0;
borlanic 0:380207fcb5c1 56 lock();
borlanic 0:380207fcb5c1 57 for (int i=0; i<16; i++) {
borlanic 0:380207fcb5c1 58 if (_pin[i] != 0) {
borlanic 0:380207fcb5c1 59 v |= _pin[i]->read() << i;
borlanic 0:380207fcb5c1 60 }
borlanic 0:380207fcb5c1 61 }
borlanic 0:380207fcb5c1 62 unlock();
borlanic 0:380207fcb5c1 63 return v;
borlanic 0:380207fcb5c1 64 }
borlanic 0:380207fcb5c1 65
borlanic 0:380207fcb5c1 66 void BusIn::mode(PinMode pull) {
borlanic 0:380207fcb5c1 67 lock();
borlanic 0:380207fcb5c1 68 for (int i=0; i<16; i++) {
borlanic 0:380207fcb5c1 69 if (_pin[i] != 0) {
borlanic 0:380207fcb5c1 70 _pin[i]->mode(pull);
borlanic 0:380207fcb5c1 71 }
borlanic 0:380207fcb5c1 72 }
borlanic 0:380207fcb5c1 73 unlock();
borlanic 0:380207fcb5c1 74 }
borlanic 0:380207fcb5c1 75
borlanic 0:380207fcb5c1 76 void BusIn::lock() {
borlanic 0:380207fcb5c1 77 _mutex.lock();
borlanic 0:380207fcb5c1 78 }
borlanic 0:380207fcb5c1 79
borlanic 0:380207fcb5c1 80 void BusIn::unlock() {
borlanic 0:380207fcb5c1 81 _mutex.unlock();
borlanic 0:380207fcb5c1 82 }
borlanic 0:380207fcb5c1 83
borlanic 0:380207fcb5c1 84 BusIn::operator int() {
borlanic 0:380207fcb5c1 85 // Underlying read is thread safe
borlanic 0:380207fcb5c1 86 return read();
borlanic 0:380207fcb5c1 87 }
borlanic 0:380207fcb5c1 88
borlanic 0:380207fcb5c1 89 DigitalIn& BusIn::operator[] (int index) {
borlanic 0:380207fcb5c1 90 // No lock needed since _pin is not modified outside the constructor
borlanic 0:380207fcb5c1 91 MBED_ASSERT(index >= 0 && index <= 16);
borlanic 0:380207fcb5c1 92 MBED_ASSERT(_pin[index]);
borlanic 0:380207fcb5c1 93 return *_pin[index];
borlanic 0:380207fcb5c1 94 }
borlanic 0:380207fcb5c1 95
borlanic 0:380207fcb5c1 96 } // namespace mbed