Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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