Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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