mbed-dev-f303

Committer:
abe5b02d-a2d4-4fe9-818e-c4e57c809ea4
Date:
Tue Jun 14 09:21:18 2022 +0000
Revision:
0:bdf663c61a82
lib

Who changed what in which revision?

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