dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/hal/common/BusInOut.cpp@9:6bb35cef007d, 2016-10-22 (annotated)
- Committer:
- cyberjoey
- Date:
- Sat Oct 22 01:31:58 2016 +0000
- Revision:
- 9:6bb35cef007d
- Parent:
- 1:55a6170b404f
WORKING
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | /* mbed Microcontroller Library |
nexpaq | 1:55a6170b404f | 2 | * Copyright (c) 2006-2013 ARM Limited |
nexpaq | 1:55a6170b404f | 3 | * |
nexpaq | 1:55a6170b404f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
nexpaq | 1:55a6170b404f | 5 | * you may not use this file except in compliance with the License. |
nexpaq | 1:55a6170b404f | 6 | * You may obtain a copy of the License at |
nexpaq | 1:55a6170b404f | 7 | * |
nexpaq | 1:55a6170b404f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
nexpaq | 1:55a6170b404f | 9 | * |
nexpaq | 1:55a6170b404f | 10 | * Unless required by applicable law or agreed to in writing, software |
nexpaq | 1:55a6170b404f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
nexpaq | 1:55a6170b404f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
nexpaq | 1:55a6170b404f | 13 | * See the License for the specific language governing permissions and |
nexpaq | 1:55a6170b404f | 14 | * limitations under the License. |
nexpaq | 1:55a6170b404f | 15 | */ |
nexpaq | 1:55a6170b404f | 16 | #include "BusInOut.h" |
nexpaq | 1:55a6170b404f | 17 | #include "mbed_assert.h" |
nexpaq | 1:55a6170b404f | 18 | |
nexpaq | 1:55a6170b404f | 19 | namespace mbed { |
nexpaq | 1:55a6170b404f | 20 | |
nexpaq | 1:55a6170b404f | 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) { |
nexpaq | 1:55a6170b404f | 22 | PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15}; |
nexpaq | 1:55a6170b404f | 23 | |
nexpaq | 1:55a6170b404f | 24 | // No lock needed in the constructor |
nexpaq | 1:55a6170b404f | 25 | _nc_mask = 0; |
nexpaq | 1:55a6170b404f | 26 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 27 | _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0; |
nexpaq | 1:55a6170b404f | 28 | if (pins[i] != NC) { |
nexpaq | 1:55a6170b404f | 29 | _nc_mask |= (1 << i); |
nexpaq | 1:55a6170b404f | 30 | } |
nexpaq | 1:55a6170b404f | 31 | } |
nexpaq | 1:55a6170b404f | 32 | } |
nexpaq | 1:55a6170b404f | 33 | |
nexpaq | 1:55a6170b404f | 34 | BusInOut::BusInOut(PinName pins[16]) { |
nexpaq | 1:55a6170b404f | 35 | // No lock needed in the constructor |
nexpaq | 1:55a6170b404f | 36 | _nc_mask = 0; |
nexpaq | 1:55a6170b404f | 37 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 38 | _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0; |
nexpaq | 1:55a6170b404f | 39 | if (pins[i] != NC) { |
nexpaq | 1:55a6170b404f | 40 | _nc_mask |= (1 << i); |
nexpaq | 1:55a6170b404f | 41 | } |
nexpaq | 1:55a6170b404f | 42 | } |
nexpaq | 1:55a6170b404f | 43 | } |
nexpaq | 1:55a6170b404f | 44 | |
nexpaq | 1:55a6170b404f | 45 | BusInOut::~BusInOut() { |
nexpaq | 1:55a6170b404f | 46 | // No lock needed in the destructor |
nexpaq | 1:55a6170b404f | 47 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 48 | if (_pin[i] != 0) { |
nexpaq | 1:55a6170b404f | 49 | delete _pin[i]; |
nexpaq | 1:55a6170b404f | 50 | } |
nexpaq | 1:55a6170b404f | 51 | } |
nexpaq | 1:55a6170b404f | 52 | } |
nexpaq | 1:55a6170b404f | 53 | |
nexpaq | 1:55a6170b404f | 54 | void BusInOut::write(int value) { |
nexpaq | 1:55a6170b404f | 55 | lock(); |
nexpaq | 1:55a6170b404f | 56 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 57 | if (_pin[i] != 0) { |
nexpaq | 1:55a6170b404f | 58 | _pin[i]->write((value >> i) & 1); |
nexpaq | 1:55a6170b404f | 59 | } |
nexpaq | 1:55a6170b404f | 60 | } |
nexpaq | 1:55a6170b404f | 61 | unlock(); |
nexpaq | 1:55a6170b404f | 62 | } |
nexpaq | 1:55a6170b404f | 63 | |
nexpaq | 1:55a6170b404f | 64 | int BusInOut::read() { |
nexpaq | 1:55a6170b404f | 65 | lock(); |
nexpaq | 1:55a6170b404f | 66 | int v = 0; |
nexpaq | 1:55a6170b404f | 67 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 68 | if (_pin[i] != 0) { |
nexpaq | 1:55a6170b404f | 69 | v |= _pin[i]->read() << i; |
nexpaq | 1:55a6170b404f | 70 | } |
nexpaq | 1:55a6170b404f | 71 | } |
nexpaq | 1:55a6170b404f | 72 | unlock(); |
nexpaq | 1:55a6170b404f | 73 | return v; |
nexpaq | 1:55a6170b404f | 74 | } |
nexpaq | 1:55a6170b404f | 75 | |
nexpaq | 1:55a6170b404f | 76 | void BusInOut::output() { |
nexpaq | 1:55a6170b404f | 77 | lock(); |
nexpaq | 1:55a6170b404f | 78 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 79 | if (_pin[i] != 0) { |
nexpaq | 1:55a6170b404f | 80 | _pin[i]->output(); |
nexpaq | 1:55a6170b404f | 81 | } |
nexpaq | 1:55a6170b404f | 82 | } |
nexpaq | 1:55a6170b404f | 83 | unlock(); |
nexpaq | 1:55a6170b404f | 84 | } |
nexpaq | 1:55a6170b404f | 85 | |
nexpaq | 1:55a6170b404f | 86 | void BusInOut::input() { |
nexpaq | 1:55a6170b404f | 87 | lock(); |
nexpaq | 1:55a6170b404f | 88 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 89 | if (_pin[i] != 0) { |
nexpaq | 1:55a6170b404f | 90 | _pin[i]->input(); |
nexpaq | 1:55a6170b404f | 91 | } |
nexpaq | 1:55a6170b404f | 92 | } |
nexpaq | 1:55a6170b404f | 93 | unlock(); |
nexpaq | 1:55a6170b404f | 94 | } |
nexpaq | 1:55a6170b404f | 95 | |
nexpaq | 1:55a6170b404f | 96 | void BusInOut::mode(PinMode pull) { |
nexpaq | 1:55a6170b404f | 97 | lock(); |
nexpaq | 1:55a6170b404f | 98 | for (int i=0; i<16; i++) { |
nexpaq | 1:55a6170b404f | 99 | if (_pin[i] != 0) { |
nexpaq | 1:55a6170b404f | 100 | _pin[i]->mode(pull); |
nexpaq | 1:55a6170b404f | 101 | } |
nexpaq | 1:55a6170b404f | 102 | } |
nexpaq | 1:55a6170b404f | 103 | unlock(); |
nexpaq | 1:55a6170b404f | 104 | } |
nexpaq | 1:55a6170b404f | 105 | |
nexpaq | 1:55a6170b404f | 106 | BusInOut& BusInOut::operator= (int v) { |
nexpaq | 1:55a6170b404f | 107 | // Underlying write is thread safe |
nexpaq | 1:55a6170b404f | 108 | write(v); |
nexpaq | 1:55a6170b404f | 109 | return *this; |
nexpaq | 1:55a6170b404f | 110 | } |
nexpaq | 1:55a6170b404f | 111 | |
nexpaq | 1:55a6170b404f | 112 | BusInOut& BusInOut::operator= (BusInOut& rhs) { |
nexpaq | 1:55a6170b404f | 113 | // Underlying read is thread safe |
nexpaq | 1:55a6170b404f | 114 | write(rhs.read()); |
nexpaq | 1:55a6170b404f | 115 | return *this; |
nexpaq | 1:55a6170b404f | 116 | } |
nexpaq | 1:55a6170b404f | 117 | |
nexpaq | 1:55a6170b404f | 118 | DigitalInOut& BusInOut::operator[] (int index) { |
nexpaq | 1:55a6170b404f | 119 | // No lock needed since _pin is not modified outside the constructor |
nexpaq | 1:55a6170b404f | 120 | MBED_ASSERT(index >= 0 && index <= 16); |
nexpaq | 1:55a6170b404f | 121 | MBED_ASSERT(_pin[index]); |
nexpaq | 1:55a6170b404f | 122 | return *_pin[index]; |
nexpaq | 1:55a6170b404f | 123 | } |
nexpaq | 1:55a6170b404f | 124 | |
nexpaq | 1:55a6170b404f | 125 | BusInOut::operator int() { |
nexpaq | 1:55a6170b404f | 126 | // Underlying read is thread safe |
nexpaq | 1:55a6170b404f | 127 | return read(); |
nexpaq | 1:55a6170b404f | 128 | } |
nexpaq | 1:55a6170b404f | 129 | |
nexpaq | 1:55a6170b404f | 130 | void BusInOut::lock() { |
nexpaq | 1:55a6170b404f | 131 | _mutex.lock(); |
nexpaq | 1:55a6170b404f | 132 | } |
nexpaq | 1:55a6170b404f | 133 | |
nexpaq | 1:55a6170b404f | 134 | void BusInOut::unlock() { |
nexpaq | 1:55a6170b404f | 135 | _mutex.unlock(); |
nexpaq | 1:55a6170b404f | 136 | } |
nexpaq | 1:55a6170b404f | 137 | |
nexpaq | 1:55a6170b404f | 138 | } // namespace mbed |