,,
Fork of Application by
mbed-dev/drivers/BusInOut.h@15:2a20c3d2616e, 2018-02-15 (annotated)
- Committer:
- Zaitsev
- Date:
- Thu Feb 15 14:29:23 2018 +0000
- Revision:
- 15:2a20c3d2616e
- Parent:
- 10:41552d038a69
j
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zaitsev | 10:41552d038a69 | 1 | /* mbed Microcontroller Library |
Zaitsev | 10:41552d038a69 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Zaitsev | 10:41552d038a69 | 3 | * |
Zaitsev | 10:41552d038a69 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Zaitsev | 10:41552d038a69 | 5 | * you may not use this file except in compliance with the License. |
Zaitsev | 10:41552d038a69 | 6 | * You may obtain a copy of the License at |
Zaitsev | 10:41552d038a69 | 7 | * |
Zaitsev | 10:41552d038a69 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Zaitsev | 10:41552d038a69 | 9 | * |
Zaitsev | 10:41552d038a69 | 10 | * Unless required by applicable law or agreed to in writing, software |
Zaitsev | 10:41552d038a69 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Zaitsev | 10:41552d038a69 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Zaitsev | 10:41552d038a69 | 13 | * See the License for the specific language governing permissions and |
Zaitsev | 10:41552d038a69 | 14 | * limitations under the License. |
Zaitsev | 10:41552d038a69 | 15 | */ |
Zaitsev | 10:41552d038a69 | 16 | #ifndef MBED_BUSINOUT_H |
Zaitsev | 10:41552d038a69 | 17 | #define MBED_BUSINOUT_H |
Zaitsev | 10:41552d038a69 | 18 | |
Zaitsev | 10:41552d038a69 | 19 | #include "drivers/DigitalInOut.h" |
Zaitsev | 10:41552d038a69 | 20 | #include "platform/PlatformMutex.h" |
Zaitsev | 10:41552d038a69 | 21 | |
Zaitsev | 10:41552d038a69 | 22 | namespace mbed { |
Zaitsev | 10:41552d038a69 | 23 | /** \addtogroup drivers */ |
Zaitsev | 10:41552d038a69 | 24 | /** @{*/ |
Zaitsev | 10:41552d038a69 | 25 | |
Zaitsev | 10:41552d038a69 | 26 | /** A digital input output bus, used for setting the state of a collection of pins |
Zaitsev | 10:41552d038a69 | 27 | * |
Zaitsev | 10:41552d038a69 | 28 | * @Note Synchronization level: Thread safe |
Zaitsev | 10:41552d038a69 | 29 | */ |
Zaitsev | 10:41552d038a69 | 30 | class BusInOut { |
Zaitsev | 10:41552d038a69 | 31 | |
Zaitsev | 10:41552d038a69 | 32 | public: |
Zaitsev | 10:41552d038a69 | 33 | |
Zaitsev | 10:41552d038a69 | 34 | /** Create an BusInOut, connected to the specified pins |
Zaitsev | 10:41552d038a69 | 35 | * |
Zaitsev | 10:41552d038a69 | 36 | * @param p<n> DigitalInOut pin to connect to bus bit p<n> (p5-p30, NC) |
Zaitsev | 10:41552d038a69 | 37 | * |
Zaitsev | 10:41552d038a69 | 38 | * @note |
Zaitsev | 10:41552d038a69 | 39 | * It is only required to specify as many pin variables as is required |
Zaitsev | 10:41552d038a69 | 40 | * for the bus; the rest will default to NC (not connected) |
Zaitsev | 10:41552d038a69 | 41 | */ |
Zaitsev | 10:41552d038a69 | 42 | BusInOut(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC, |
Zaitsev | 10:41552d038a69 | 43 | PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC, |
Zaitsev | 10:41552d038a69 | 44 | PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC, |
Zaitsev | 10:41552d038a69 | 45 | PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC); |
Zaitsev | 10:41552d038a69 | 46 | |
Zaitsev | 10:41552d038a69 | 47 | BusInOut(PinName pins[16]); |
Zaitsev | 10:41552d038a69 | 48 | |
Zaitsev | 10:41552d038a69 | 49 | virtual ~BusInOut(); |
Zaitsev | 10:41552d038a69 | 50 | |
Zaitsev | 10:41552d038a69 | 51 | /* Group: Access Methods */ |
Zaitsev | 10:41552d038a69 | 52 | |
Zaitsev | 10:41552d038a69 | 53 | /** Write the value to the output bus |
Zaitsev | 10:41552d038a69 | 54 | * |
Zaitsev | 10:41552d038a69 | 55 | * @param value An integer specifying a bit to write for every corresponding DigitalInOut pin |
Zaitsev | 10:41552d038a69 | 56 | */ |
Zaitsev | 10:41552d038a69 | 57 | void write(int value); |
Zaitsev | 10:41552d038a69 | 58 | |
Zaitsev | 10:41552d038a69 | 59 | /** Read the value currently output on the bus |
Zaitsev | 10:41552d038a69 | 60 | * |
Zaitsev | 10:41552d038a69 | 61 | * @returns |
Zaitsev | 10:41552d038a69 | 62 | * An integer with each bit corresponding to associated DigitalInOut pin setting |
Zaitsev | 10:41552d038a69 | 63 | */ |
Zaitsev | 10:41552d038a69 | 64 | int read(); |
Zaitsev | 10:41552d038a69 | 65 | |
Zaitsev | 10:41552d038a69 | 66 | /** Set as an output |
Zaitsev | 10:41552d038a69 | 67 | */ |
Zaitsev | 10:41552d038a69 | 68 | void output(); |
Zaitsev | 10:41552d038a69 | 69 | |
Zaitsev | 10:41552d038a69 | 70 | /** Set as an input |
Zaitsev | 10:41552d038a69 | 71 | */ |
Zaitsev | 10:41552d038a69 | 72 | void input(); |
Zaitsev | 10:41552d038a69 | 73 | |
Zaitsev | 10:41552d038a69 | 74 | /** Set the input pin mode |
Zaitsev | 10:41552d038a69 | 75 | * |
Zaitsev | 10:41552d038a69 | 76 | * @param mode PullUp, PullDown, PullNone |
Zaitsev | 10:41552d038a69 | 77 | */ |
Zaitsev | 10:41552d038a69 | 78 | void mode(PinMode pull); |
Zaitsev | 10:41552d038a69 | 79 | |
Zaitsev | 10:41552d038a69 | 80 | /** Binary mask of bus pins connected to actual pins (not NC pins) |
Zaitsev | 10:41552d038a69 | 81 | * If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1 |
Zaitsev | 10:41552d038a69 | 82 | * |
Zaitsev | 10:41552d038a69 | 83 | * @returns |
Zaitsev | 10:41552d038a69 | 84 | * Binary mask of connected pins |
Zaitsev | 10:41552d038a69 | 85 | */ |
Zaitsev | 10:41552d038a69 | 86 | int mask() { |
Zaitsev | 10:41552d038a69 | 87 | // No lock needed since _nc_mask is not modified outside the constructor |
Zaitsev | 10:41552d038a69 | 88 | return _nc_mask; |
Zaitsev | 10:41552d038a69 | 89 | } |
Zaitsev | 10:41552d038a69 | 90 | |
Zaitsev | 10:41552d038a69 | 91 | /** A shorthand for write() |
Zaitsev | 10:41552d038a69 | 92 | */ |
Zaitsev | 10:41552d038a69 | 93 | BusInOut& operator= (int v); |
Zaitsev | 10:41552d038a69 | 94 | BusInOut& operator= (BusInOut& rhs); |
Zaitsev | 10:41552d038a69 | 95 | |
Zaitsev | 10:41552d038a69 | 96 | /** Access to particular bit in random-iterator fashion |
Zaitsev | 10:41552d038a69 | 97 | */ |
Zaitsev | 10:41552d038a69 | 98 | DigitalInOut& operator[] (int index); |
Zaitsev | 10:41552d038a69 | 99 | |
Zaitsev | 10:41552d038a69 | 100 | /** A shorthand for read() |
Zaitsev | 10:41552d038a69 | 101 | */ |
Zaitsev | 10:41552d038a69 | 102 | operator int(); |
Zaitsev | 10:41552d038a69 | 103 | |
Zaitsev | 10:41552d038a69 | 104 | protected: |
Zaitsev | 10:41552d038a69 | 105 | virtual void lock(); |
Zaitsev | 10:41552d038a69 | 106 | virtual void unlock(); |
Zaitsev | 10:41552d038a69 | 107 | DigitalInOut* _pin[16]; |
Zaitsev | 10:41552d038a69 | 108 | |
Zaitsev | 10:41552d038a69 | 109 | /** Mask of bus's NC pins |
Zaitsev | 10:41552d038a69 | 110 | * If bit[n] is set to 1 - pin is connected |
Zaitsev | 10:41552d038a69 | 111 | * if bit[n] is cleared - pin is not connected (NC) |
Zaitsev | 10:41552d038a69 | 112 | */ |
Zaitsev | 10:41552d038a69 | 113 | int _nc_mask; |
Zaitsev | 10:41552d038a69 | 114 | |
Zaitsev | 10:41552d038a69 | 115 | PlatformMutex _mutex; |
Zaitsev | 10:41552d038a69 | 116 | |
Zaitsev | 10:41552d038a69 | 117 | /* disallow copy constructor and assignment operators */ |
Zaitsev | 10:41552d038a69 | 118 | private: |
Zaitsev | 10:41552d038a69 | 119 | BusInOut(const BusInOut&); |
Zaitsev | 10:41552d038a69 | 120 | BusInOut & operator = (const BusInOut&); |
Zaitsev | 10:41552d038a69 | 121 | }; |
Zaitsev | 10:41552d038a69 | 122 | |
Zaitsev | 10:41552d038a69 | 123 | } // namespace mbed |
Zaitsev | 10:41552d038a69 | 124 | |
Zaitsev | 10:41552d038a69 | 125 | #endif |
Zaitsev | 10:41552d038a69 | 126 | |
Zaitsev | 10:41552d038a69 | 127 | /** @}*/ |