This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

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