Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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