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 "BusOut.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 BusOut::BusOut(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 DigitalOut(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 BusOut::BusOut(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 DigitalOut(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 BusOut::~BusOut() {
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 BusOut::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 BusOut::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 BusOut& BusOut::operator= (int v) {
apluscw 187:92cbb9eec47b 77 // Underlying write is thread safe
apluscw 187:92cbb9eec47b 78 write(v);
apluscw 187:92cbb9eec47b 79 return *this;
apluscw 187:92cbb9eec47b 80 }
apluscw 187:92cbb9eec47b 81
apluscw 187:92cbb9eec47b 82 BusOut& BusOut::operator= (BusOut& rhs) {
apluscw 187:92cbb9eec47b 83 // Underlying write is thread safe
apluscw 187:92cbb9eec47b 84 write(rhs.read());
apluscw 187:92cbb9eec47b 85 return *this;
apluscw 187:92cbb9eec47b 86 }
apluscw 187:92cbb9eec47b 87
apluscw 187:92cbb9eec47b 88 DigitalOut& BusOut::operator[] (int index) {
apluscw 187:92cbb9eec47b 89 // No lock needed since _pin is not modified outside the constructor
apluscw 187:92cbb9eec47b 90 MBED_ASSERT(index >= 0 && index <= 16);
apluscw 187:92cbb9eec47b 91 MBED_ASSERT(_pin[index]);
apluscw 187:92cbb9eec47b 92 return *_pin[index];
apluscw 187:92cbb9eec47b 93 }
apluscw 187:92cbb9eec47b 94
apluscw 187:92cbb9eec47b 95 BusOut::operator int() {
apluscw 187:92cbb9eec47b 96 // Underlying read is thread safe
apluscw 187:92cbb9eec47b 97 return read();
apluscw 187:92cbb9eec47b 98 }
apluscw 187:92cbb9eec47b 99
apluscw 187:92cbb9eec47b 100 void BusOut::lock() {
apluscw 187:92cbb9eec47b 101 _mutex.lock();
apluscw 187:92cbb9eec47b 102 }
apluscw 187:92cbb9eec47b 103
apluscw 187:92cbb9eec47b 104 void BusOut::unlock() {
apluscw 187:92cbb9eec47b 105 _mutex.unlock();
apluscw 187:92cbb9eec47b 106 }
apluscw 187:92cbb9eec47b 107
apluscw 187:92cbb9eec47b 108 } // namespace mbed