Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 00:01:50 2018 +0000
Revision:
0:6ad07c9019fd
Codigo de tales para todos los pasculaes

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