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