mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

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