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