1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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