mbed-os5 only for TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Committer:
kenjiArai
Date:
Tue Dec 31 06:02:27 2019 +0000
Revision:
1:9db0e321a9f4
Parent:
drivers/BusInOut.cpp@0:5b88d5760320
updated based on mbed-os5.15.0

Who changed what in which revision?

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