mbed library sources. Supersedes mbed-src.
Dependents: Hobbyking_Cheetah_Compact Hobbyking_Cheetah_Compact_DRV8323_14bit Hobbyking_Cheetah_Compact_DRV8323_V51_201907 HKC_MiniCheetah ... more
Fork of mbed-dev by
drivers/BusInOut.cpp@181:36facd806e4a, 2018-07-30 (annotated)
- Committer:
- benkatz
- Date:
- Mon Jul 30 20:31:44 2018 +0000
- Revision:
- 181:36facd806e4a
- Parent:
- 149:156823d33999
going on the robot. fixed a dumb bug in float_to_uint
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/BusInOut.h" |
<> | 149:156823d33999 | 17 | #include "platform/mbed_assert.h" |
<> | 149:156823d33999 | 18 | |
<> | 149:156823d33999 | 19 | namespace mbed { |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | BusInOut::BusInOut(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 DigitalInOut(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 | BusInOut::BusInOut(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 DigitalInOut(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 | BusInOut::~BusInOut() { |
<> | 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 BusInOut::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 BusInOut::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 | void BusInOut::output() { |
<> | 149:156823d33999 | 77 | lock(); |
<> | 149:156823d33999 | 78 | for (int i=0; i<16; i++) { |
<> | 149:156823d33999 | 79 | if (_pin[i] != 0) { |
<> | 149:156823d33999 | 80 | _pin[i]->output(); |
<> | 149:156823d33999 | 81 | } |
<> | 149:156823d33999 | 82 | } |
<> | 149:156823d33999 | 83 | unlock(); |
<> | 149:156823d33999 | 84 | } |
<> | 149:156823d33999 | 85 | |
<> | 149:156823d33999 | 86 | void BusInOut::input() { |
<> | 149:156823d33999 | 87 | lock(); |
<> | 149:156823d33999 | 88 | for (int i=0; i<16; i++) { |
<> | 149:156823d33999 | 89 | if (_pin[i] != 0) { |
<> | 149:156823d33999 | 90 | _pin[i]->input(); |
<> | 149:156823d33999 | 91 | } |
<> | 149:156823d33999 | 92 | } |
<> | 149:156823d33999 | 93 | unlock(); |
<> | 149:156823d33999 | 94 | } |
<> | 149:156823d33999 | 95 | |
<> | 149:156823d33999 | 96 | void BusInOut::mode(PinMode pull) { |
<> | 149:156823d33999 | 97 | lock(); |
<> | 149:156823d33999 | 98 | for (int i=0; i<16; i++) { |
<> | 149:156823d33999 | 99 | if (_pin[i] != 0) { |
<> | 149:156823d33999 | 100 | _pin[i]->mode(pull); |
<> | 149:156823d33999 | 101 | } |
<> | 149:156823d33999 | 102 | } |
<> | 149:156823d33999 | 103 | unlock(); |
<> | 149:156823d33999 | 104 | } |
<> | 149:156823d33999 | 105 | |
<> | 149:156823d33999 | 106 | BusInOut& BusInOut::operator= (int v) { |
<> | 149:156823d33999 | 107 | // Underlying write is thread safe |
<> | 149:156823d33999 | 108 | write(v); |
<> | 149:156823d33999 | 109 | return *this; |
<> | 149:156823d33999 | 110 | } |
<> | 149:156823d33999 | 111 | |
<> | 149:156823d33999 | 112 | BusInOut& BusInOut::operator= (BusInOut& rhs) { |
<> | 149:156823d33999 | 113 | // Underlying read is thread safe |
<> | 149:156823d33999 | 114 | write(rhs.read()); |
<> | 149:156823d33999 | 115 | return *this; |
<> | 149:156823d33999 | 116 | } |
<> | 149:156823d33999 | 117 | |
<> | 149:156823d33999 | 118 | DigitalInOut& BusInOut::operator[] (int index) { |
<> | 149:156823d33999 | 119 | // No lock needed since _pin is not modified outside the constructor |
<> | 149:156823d33999 | 120 | MBED_ASSERT(index >= 0 && index <= 16); |
<> | 149:156823d33999 | 121 | MBED_ASSERT(_pin[index]); |
<> | 149:156823d33999 | 122 | return *_pin[index]; |
<> | 149:156823d33999 | 123 | } |
<> | 149:156823d33999 | 124 | |
<> | 149:156823d33999 | 125 | BusInOut::operator int() { |
<> | 149:156823d33999 | 126 | // Underlying read is thread safe |
<> | 149:156823d33999 | 127 | return read(); |
<> | 149:156823d33999 | 128 | } |
<> | 149:156823d33999 | 129 | |
<> | 149:156823d33999 | 130 | void BusInOut::lock() { |
<> | 149:156823d33999 | 131 | _mutex.lock(); |
<> | 149:156823d33999 | 132 | } |
<> | 149:156823d33999 | 133 | |
<> | 149:156823d33999 | 134 | void BusInOut::unlock() { |
<> | 149:156823d33999 | 135 | _mutex.unlock(); |
<> | 149:156823d33999 | 136 | } |
<> | 149:156823d33999 | 137 | |
<> | 149:156823d33999 | 138 | } // namespace mbed |