Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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