mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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