Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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