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/BusInOut.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 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) {
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 DigitalInOut(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 BusInOut::BusInOut(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 DigitalInOut(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 BusInOut::~BusInOut() {
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 BusInOut::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 BusInOut::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 void BusInOut::output() {
marcozecchini 0:9fca2b23d0ba 77 lock();
marcozecchini 0:9fca2b23d0ba 78 for (int i=0; i<16; i++) {
marcozecchini 0:9fca2b23d0ba 79 if (_pin[i] != 0) {
marcozecchini 0:9fca2b23d0ba 80 _pin[i]->output();
marcozecchini 0:9fca2b23d0ba 81 }
marcozecchini 0:9fca2b23d0ba 82 }
marcozecchini 0:9fca2b23d0ba 83 unlock();
marcozecchini 0:9fca2b23d0ba 84 }
marcozecchini 0:9fca2b23d0ba 85
marcozecchini 0:9fca2b23d0ba 86 void BusInOut::input() {
marcozecchini 0:9fca2b23d0ba 87 lock();
marcozecchini 0:9fca2b23d0ba 88 for (int i=0; i<16; i++) {
marcozecchini 0:9fca2b23d0ba 89 if (_pin[i] != 0) {
marcozecchini 0:9fca2b23d0ba 90 _pin[i]->input();
marcozecchini 0:9fca2b23d0ba 91 }
marcozecchini 0:9fca2b23d0ba 92 }
marcozecchini 0:9fca2b23d0ba 93 unlock();
marcozecchini 0:9fca2b23d0ba 94 }
marcozecchini 0:9fca2b23d0ba 95
marcozecchini 0:9fca2b23d0ba 96 void BusInOut::mode(PinMode pull) {
marcozecchini 0:9fca2b23d0ba 97 lock();
marcozecchini 0:9fca2b23d0ba 98 for (int i=0; i<16; i++) {
marcozecchini 0:9fca2b23d0ba 99 if (_pin[i] != 0) {
marcozecchini 0:9fca2b23d0ba 100 _pin[i]->mode(pull);
marcozecchini 0:9fca2b23d0ba 101 }
marcozecchini 0:9fca2b23d0ba 102 }
marcozecchini 0:9fca2b23d0ba 103 unlock();
marcozecchini 0:9fca2b23d0ba 104 }
marcozecchini 0:9fca2b23d0ba 105
marcozecchini 0:9fca2b23d0ba 106 BusInOut& BusInOut::operator= (int v) {
marcozecchini 0:9fca2b23d0ba 107 // Underlying write is thread safe
marcozecchini 0:9fca2b23d0ba 108 write(v);
marcozecchini 0:9fca2b23d0ba 109 return *this;
marcozecchini 0:9fca2b23d0ba 110 }
marcozecchini 0:9fca2b23d0ba 111
marcozecchini 0:9fca2b23d0ba 112 BusInOut& BusInOut::operator= (BusInOut& rhs) {
marcozecchini 0:9fca2b23d0ba 113 // Underlying read is thread safe
marcozecchini 0:9fca2b23d0ba 114 write(rhs.read());
marcozecchini 0:9fca2b23d0ba 115 return *this;
marcozecchini 0:9fca2b23d0ba 116 }
marcozecchini 0:9fca2b23d0ba 117
marcozecchini 0:9fca2b23d0ba 118 DigitalInOut& BusInOut::operator[] (int index) {
marcozecchini 0:9fca2b23d0ba 119 // No lock needed since _pin is not modified outside the constructor
marcozecchini 0:9fca2b23d0ba 120 MBED_ASSERT(index >= 0 && index <= 16);
marcozecchini 0:9fca2b23d0ba 121 MBED_ASSERT(_pin[index]);
marcozecchini 0:9fca2b23d0ba 122 return *_pin[index];
marcozecchini 0:9fca2b23d0ba 123 }
marcozecchini 0:9fca2b23d0ba 124
marcozecchini 0:9fca2b23d0ba 125 BusInOut::operator int() {
marcozecchini 0:9fca2b23d0ba 126 // Underlying read is thread safe
marcozecchini 0:9fca2b23d0ba 127 return read();
marcozecchini 0:9fca2b23d0ba 128 }
marcozecchini 0:9fca2b23d0ba 129
marcozecchini 0:9fca2b23d0ba 130 void BusInOut::lock() {
marcozecchini 0:9fca2b23d0ba 131 _mutex.lock();
marcozecchini 0:9fca2b23d0ba 132 }
marcozecchini 0:9fca2b23d0ba 133
marcozecchini 0:9fca2b23d0ba 134 void BusInOut::unlock() {
marcozecchini 0:9fca2b23d0ba 135 _mutex.unlock();
marcozecchini 0:9fca2b23d0ba 136 }
marcozecchini 0:9fca2b23d0ba 137
marcozecchini 0:9fca2b23d0ba 138 } // namespace mbed