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