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/BusIn.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 BusIn::BusIn(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 DigitalIn(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 BusIn::BusIn(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 DigitalIn(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 BusIn::~BusIn() {
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 int BusIn::read() {
NYX 0:85b3fd62ea1a 55 int v = 0;
NYX 0:85b3fd62ea1a 56 lock();
NYX 0:85b3fd62ea1a 57 for (int i=0; i<16; i++) {
NYX 0:85b3fd62ea1a 58 if (_pin[i] != 0) {
NYX 0:85b3fd62ea1a 59 v |= _pin[i]->read() << i;
NYX 0:85b3fd62ea1a 60 }
NYX 0:85b3fd62ea1a 61 }
NYX 0:85b3fd62ea1a 62 unlock();
NYX 0:85b3fd62ea1a 63 return v;
NYX 0:85b3fd62ea1a 64 }
NYX 0:85b3fd62ea1a 65
NYX 0:85b3fd62ea1a 66 void BusIn::mode(PinMode pull) {
NYX 0:85b3fd62ea1a 67 lock();
NYX 0:85b3fd62ea1a 68 for (int i=0; i<16; i++) {
NYX 0:85b3fd62ea1a 69 if (_pin[i] != 0) {
NYX 0:85b3fd62ea1a 70 _pin[i]->mode(pull);
NYX 0:85b3fd62ea1a 71 }
NYX 0:85b3fd62ea1a 72 }
NYX 0:85b3fd62ea1a 73 unlock();
NYX 0:85b3fd62ea1a 74 }
NYX 0:85b3fd62ea1a 75
NYX 0:85b3fd62ea1a 76 void BusIn::lock() {
NYX 0:85b3fd62ea1a 77 _mutex.lock();
NYX 0:85b3fd62ea1a 78 }
NYX 0:85b3fd62ea1a 79
NYX 0:85b3fd62ea1a 80 void BusIn::unlock() {
NYX 0:85b3fd62ea1a 81 _mutex.unlock();
NYX 0:85b3fd62ea1a 82 }
NYX 0:85b3fd62ea1a 83
NYX 0:85b3fd62ea1a 84 BusIn::operator int() {
NYX 0:85b3fd62ea1a 85 // Underlying read is thread safe
NYX 0:85b3fd62ea1a 86 return read();
NYX 0:85b3fd62ea1a 87 }
NYX 0:85b3fd62ea1a 88
NYX 0:85b3fd62ea1a 89 DigitalIn& BusIn::operator[] (int index) {
NYX 0:85b3fd62ea1a 90 // No lock needed since _pin is not modified outside the constructor
NYX 0:85b3fd62ea1a 91 MBED_ASSERT(index >= 0 && index <= 16);
NYX 0:85b3fd62ea1a 92 MBED_ASSERT(_pin[index]);
NYX 0:85b3fd62ea1a 93 return *_pin[index];
NYX 0:85b3fd62ea1a 94 }
NYX 0:85b3fd62ea1a 95
NYX 0:85b3fd62ea1a 96 } // namespace mbed