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/BusIn.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 BusIn::BusIn(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 DigitalIn(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 BusIn::BusIn(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 DigitalIn(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 BusIn::~BusIn() {
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 int BusIn::read() {
marcozecchini 0:9fca2b23d0ba 55 int v = 0;
marcozecchini 0:9fca2b23d0ba 56 lock();
marcozecchini 0:9fca2b23d0ba 57 for (int i=0; i<16; i++) {
marcozecchini 0:9fca2b23d0ba 58 if (_pin[i] != 0) {
marcozecchini 0:9fca2b23d0ba 59 v |= _pin[i]->read() << i;
marcozecchini 0:9fca2b23d0ba 60 }
marcozecchini 0:9fca2b23d0ba 61 }
marcozecchini 0:9fca2b23d0ba 62 unlock();
marcozecchini 0:9fca2b23d0ba 63 return v;
marcozecchini 0:9fca2b23d0ba 64 }
marcozecchini 0:9fca2b23d0ba 65
marcozecchini 0:9fca2b23d0ba 66 void BusIn::mode(PinMode pull) {
marcozecchini 0:9fca2b23d0ba 67 lock();
marcozecchini 0:9fca2b23d0ba 68 for (int i=0; i<16; i++) {
marcozecchini 0:9fca2b23d0ba 69 if (_pin[i] != 0) {
marcozecchini 0:9fca2b23d0ba 70 _pin[i]->mode(pull);
marcozecchini 0:9fca2b23d0ba 71 }
marcozecchini 0:9fca2b23d0ba 72 }
marcozecchini 0:9fca2b23d0ba 73 unlock();
marcozecchini 0:9fca2b23d0ba 74 }
marcozecchini 0:9fca2b23d0ba 75
marcozecchini 0:9fca2b23d0ba 76 void BusIn::lock() {
marcozecchini 0:9fca2b23d0ba 77 _mutex.lock();
marcozecchini 0:9fca2b23d0ba 78 }
marcozecchini 0:9fca2b23d0ba 79
marcozecchini 0:9fca2b23d0ba 80 void BusIn::unlock() {
marcozecchini 0:9fca2b23d0ba 81 _mutex.unlock();
marcozecchini 0:9fca2b23d0ba 82 }
marcozecchini 0:9fca2b23d0ba 83
marcozecchini 0:9fca2b23d0ba 84 BusIn::operator int() {
marcozecchini 0:9fca2b23d0ba 85 // Underlying read is thread safe
marcozecchini 0:9fca2b23d0ba 86 return read();
marcozecchini 0:9fca2b23d0ba 87 }
marcozecchini 0:9fca2b23d0ba 88
marcozecchini 0:9fca2b23d0ba 89 DigitalIn& BusIn::operator[] (int index) {
marcozecchini 0:9fca2b23d0ba 90 // No lock needed since _pin is not modified outside the constructor
marcozecchini 0:9fca2b23d0ba 91 MBED_ASSERT(index >= 0 && index <= 16);
marcozecchini 0:9fca2b23d0ba 92 MBED_ASSERT(_pin[index]);
marcozecchini 0:9fca2b23d0ba 93 return *_pin[index];
marcozecchini 0:9fca2b23d0ba 94 }
marcozecchini 0:9fca2b23d0ba 95
marcozecchini 0:9fca2b23d0ba 96 } // namespace mbed