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