inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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