1232
drivers/BusIn.h@0:06036f8bee2d, 2022-10-24 (annotated)
- Committer:
- ganlikun
- Date:
- Mon Oct 24 15:19:39 2022 +0000
- Revision:
- 0:06036f8bee2d
11
Who changed what in which revision?
User | Revision | Line number | New 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_BUSIN_H |
ganlikun | 0:06036f8bee2d | 17 | #define MBED_BUSIN_H |
ganlikun | 0:06036f8bee2d | 18 | |
ganlikun | 0:06036f8bee2d | 19 | #include "platform/platform.h" |
ganlikun | 0:06036f8bee2d | 20 | #include "drivers/DigitalIn.h" |
ganlikun | 0:06036f8bee2d | 21 | #include "platform/PlatformMutex.h" |
ganlikun | 0:06036f8bee2d | 22 | #include "platform/NonCopyable.h" |
ganlikun | 0:06036f8bee2d | 23 | |
ganlikun | 0:06036f8bee2d | 24 | namespace mbed { |
ganlikun | 0:06036f8bee2d | 25 | /** \addtogroup drivers */ |
ganlikun | 0:06036f8bee2d | 26 | |
ganlikun | 0:06036f8bee2d | 27 | /** A digital input bus, used for reading the state of a collection of pins |
ganlikun | 0:06036f8bee2d | 28 | * |
ganlikun | 0:06036f8bee2d | 29 | * @note Synchronization level: Thread safe |
ganlikun | 0:06036f8bee2d | 30 | * @ingroup drivers |
ganlikun | 0:06036f8bee2d | 31 | */ |
ganlikun | 0:06036f8bee2d | 32 | class BusIn : private NonCopyable<BusIn> { |
ganlikun | 0:06036f8bee2d | 33 | |
ganlikun | 0:06036f8bee2d | 34 | public: |
ganlikun | 0:06036f8bee2d | 35 | /* Group: Configuration Methods */ |
ganlikun | 0:06036f8bee2d | 36 | |
ganlikun | 0:06036f8bee2d | 37 | /** Create an BusIn, connected to the specified pins |
ganlikun | 0:06036f8bee2d | 38 | * |
ganlikun | 0:06036f8bee2d | 39 | * @param p0 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 40 | * @param p1 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 41 | * @param p2 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 42 | * @param p3 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 43 | * @param p4 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 44 | * @param p5 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 45 | * @param p6 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 46 | * @param p7 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 47 | * @param p8 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 48 | * @param p9 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 49 | * @param p10 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 50 | * @param p11 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 51 | * @param p12 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 52 | * @param p13 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 53 | * @param p14 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 54 | * @param p15 DigitalIn pin to connect to bus bit |
ganlikun | 0:06036f8bee2d | 55 | * |
ganlikun | 0:06036f8bee2d | 56 | * @note |
ganlikun | 0:06036f8bee2d | 57 | * It is only required to specify as many pin variables as is required |
ganlikun | 0:06036f8bee2d | 58 | * for the bus; the rest will default to NC (not connected) |
ganlikun | 0:06036f8bee2d | 59 | */ |
ganlikun | 0:06036f8bee2d | 60 | BusIn(PinName p0, PinName p1 = NC, PinName p2 = NC, PinName p3 = NC, |
ganlikun | 0:06036f8bee2d | 61 | PinName p4 = NC, PinName p5 = NC, PinName p6 = NC, PinName p7 = NC, |
ganlikun | 0:06036f8bee2d | 62 | PinName p8 = NC, PinName p9 = NC, PinName p10 = NC, PinName p11 = NC, |
ganlikun | 0:06036f8bee2d | 63 | PinName p12 = NC, PinName p13 = NC, PinName p14 = NC, PinName p15 = NC); |
ganlikun | 0:06036f8bee2d | 64 | |
ganlikun | 0:06036f8bee2d | 65 | |
ganlikun | 0:06036f8bee2d | 66 | /** Create an BusIn, connected to the specified pins |
ganlikun | 0:06036f8bee2d | 67 | * |
ganlikun | 0:06036f8bee2d | 68 | * @param pins An array of pins to connect to bus bit |
ganlikun | 0:06036f8bee2d | 69 | */ |
ganlikun | 0:06036f8bee2d | 70 | BusIn(PinName pins[16]); |
ganlikun | 0:06036f8bee2d | 71 | |
ganlikun | 0:06036f8bee2d | 72 | virtual ~BusIn(); |
ganlikun | 0:06036f8bee2d | 73 | |
ganlikun | 0:06036f8bee2d | 74 | /** Read the value of the input bus |
ganlikun | 0:06036f8bee2d | 75 | * |
ganlikun | 0:06036f8bee2d | 76 | * @returns |
ganlikun | 0:06036f8bee2d | 77 | * An integer with each bit corresponding to the value read from the associated DigitalIn pin |
ganlikun | 0:06036f8bee2d | 78 | */ |
ganlikun | 0:06036f8bee2d | 79 | int read(); |
ganlikun | 0:06036f8bee2d | 80 | |
ganlikun | 0:06036f8bee2d | 81 | /** Set the input pin mode |
ganlikun | 0:06036f8bee2d | 82 | * |
ganlikun | 0:06036f8bee2d | 83 | * @param pull PullUp, PullDown, PullNone |
ganlikun | 0:06036f8bee2d | 84 | */ |
ganlikun | 0:06036f8bee2d | 85 | void mode(PinMode pull); |
ganlikun | 0:06036f8bee2d | 86 | |
ganlikun | 0:06036f8bee2d | 87 | /** Binary mask of bus pins connected to actual pins (not NC pins) |
ganlikun | 0:06036f8bee2d | 88 | * 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 | 89 | * |
ganlikun | 0:06036f8bee2d | 90 | * @returns |
ganlikun | 0:06036f8bee2d | 91 | * Binary mask of connected pins |
ganlikun | 0:06036f8bee2d | 92 | */ |
ganlikun | 0:06036f8bee2d | 93 | int mask() { |
ganlikun | 0:06036f8bee2d | 94 | // No lock needed since _nc_mask is not modified outside the constructor |
ganlikun | 0:06036f8bee2d | 95 | return _nc_mask; |
ganlikun | 0:06036f8bee2d | 96 | } |
ganlikun | 0:06036f8bee2d | 97 | |
ganlikun | 0:06036f8bee2d | 98 | /** A shorthand for read() |
ganlikun | 0:06036f8bee2d | 99 | * \sa DigitalIn::read() |
ganlikun | 0:06036f8bee2d | 100 | */ |
ganlikun | 0:06036f8bee2d | 101 | operator int(); |
ganlikun | 0:06036f8bee2d | 102 | |
ganlikun | 0:06036f8bee2d | 103 | /** Access to particular bit in random-iterator fashion |
ganlikun | 0:06036f8bee2d | 104 | * @param index Position of bit |
ganlikun | 0:06036f8bee2d | 105 | */ |
ganlikun | 0:06036f8bee2d | 106 | DigitalIn & operator[] (int index); |
ganlikun | 0:06036f8bee2d | 107 | |
ganlikun | 0:06036f8bee2d | 108 | protected: |
ganlikun | 0:06036f8bee2d | 109 | DigitalIn* _pin[16]; |
ganlikun | 0:06036f8bee2d | 110 | |
ganlikun | 0:06036f8bee2d | 111 | /* Mask of bus's NC pins |
ganlikun | 0:06036f8bee2d | 112 | * If bit[n] is set to 1 - pin is connected |
ganlikun | 0:06036f8bee2d | 113 | * if bit[n] is cleared - pin is not connected (NC) |
ganlikun | 0:06036f8bee2d | 114 | */ |
ganlikun | 0:06036f8bee2d | 115 | int _nc_mask; |
ganlikun | 0:06036f8bee2d | 116 | |
ganlikun | 0:06036f8bee2d | 117 | PlatformMutex _mutex; |
ganlikun | 0:06036f8bee2d | 118 | |
ganlikun | 0:06036f8bee2d | 119 | private: |
ganlikun | 0:06036f8bee2d | 120 | virtual void lock(); |
ganlikun | 0:06036f8bee2d | 121 | virtual void unlock(); |
ganlikun | 0:06036f8bee2d | 122 | }; |
ganlikun | 0:06036f8bee2d | 123 | |
ganlikun | 0:06036f8bee2d | 124 | } // namespace mbed |
ganlikun | 0:06036f8bee2d | 125 | |
ganlikun | 0:06036f8bee2d | 126 | #endif |
ganlikun | 0:06036f8bee2d | 127 | |
ganlikun | 0:06036f8bee2d | 128 |