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/BusOut.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 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) {
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 DigitalOut(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 BusOut::BusOut(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 DigitalOut(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 BusOut::~BusOut() {
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 void BusOut::write(int value) {
dkato 0:f782d9c66c49 55 lock();
dkato 0:f782d9c66c49 56 for (int i=0; i<16; i++) {
dkato 0:f782d9c66c49 57 if (_pin[i] != 0) {
dkato 0:f782d9c66c49 58 _pin[i]->write((value >> i) & 1);
dkato 0:f782d9c66c49 59 }
dkato 0:f782d9c66c49 60 }
dkato 0:f782d9c66c49 61 unlock();
dkato 0:f782d9c66c49 62 }
dkato 0:f782d9c66c49 63
dkato 0:f782d9c66c49 64 int BusOut::read() {
dkato 0:f782d9c66c49 65 lock();
dkato 0:f782d9c66c49 66 int v = 0;
dkato 0:f782d9c66c49 67 for (int i=0; i<16; i++) {
dkato 0:f782d9c66c49 68 if (_pin[i] != 0) {
dkato 0:f782d9c66c49 69 v |= _pin[i]->read() << i;
dkato 0:f782d9c66c49 70 }
dkato 0:f782d9c66c49 71 }
dkato 0:f782d9c66c49 72 unlock();
dkato 0:f782d9c66c49 73 return v;
dkato 0:f782d9c66c49 74 }
dkato 0:f782d9c66c49 75
dkato 0:f782d9c66c49 76 BusOut& BusOut::operator= (int v) {
dkato 0:f782d9c66c49 77 // Underlying write is thread safe
dkato 0:f782d9c66c49 78 write(v);
dkato 0:f782d9c66c49 79 return *this;
dkato 0:f782d9c66c49 80 }
dkato 0:f782d9c66c49 81
dkato 0:f782d9c66c49 82 BusOut& BusOut::operator= (BusOut& rhs) {
dkato 0:f782d9c66c49 83 // Underlying write is thread safe
dkato 0:f782d9c66c49 84 write(rhs.read());
dkato 0:f782d9c66c49 85 return *this;
dkato 0:f782d9c66c49 86 }
dkato 0:f782d9c66c49 87
dkato 0:f782d9c66c49 88 DigitalOut& BusOut::operator[] (int index) {
dkato 0:f782d9c66c49 89 // No lock needed since _pin is not modified outside the constructor
dkato 0:f782d9c66c49 90 MBED_ASSERT(index >= 0 && index <= 16);
dkato 0:f782d9c66c49 91 MBED_ASSERT(_pin[index]);
dkato 0:f782d9c66c49 92 return *_pin[index];
dkato 0:f782d9c66c49 93 }
dkato 0:f782d9c66c49 94
dkato 0:f782d9c66c49 95 BusOut::operator int() {
dkato 0:f782d9c66c49 96 // Underlying read is thread safe
dkato 0:f782d9c66c49 97 return read();
dkato 0:f782d9c66c49 98 }
dkato 0:f782d9c66c49 99
dkato 0:f782d9c66c49 100 void BusOut::lock() {
dkato 0:f782d9c66c49 101 _mutex.lock();
dkato 0:f782d9c66c49 102 }
dkato 0:f782d9c66c49 103
dkato 0:f782d9c66c49 104 void BusOut::unlock() {
dkato 0:f782d9c66c49 105 _mutex.unlock();
dkato 0:f782d9c66c49 106 }
dkato 0:f782d9c66c49 107
dkato 0:f782d9c66c49 108 } // namespace mbed