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