mbed-os
Fork of mbed-os by
drivers/PortIn.h@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2013 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | #ifndef MBED_PORTIN_H |
elessair | 0:f269e3021894 | 17 | #define MBED_PORTIN_H |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #include "platform/platform.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #if DEVICE_PORTIN |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | #include "hal/port_api.h" |
elessair | 0:f269e3021894 | 24 | #include "platform/critical.h" |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | namespace mbed { |
elessair | 0:f269e3021894 | 27 | /** \addtogroup drivers */ |
elessair | 0:f269e3021894 | 28 | /** @{*/ |
elessair | 0:f269e3021894 | 29 | |
elessair | 0:f269e3021894 | 30 | /** A multiple pin digital input |
elessair | 0:f269e3021894 | 31 | * |
elessair | 0:f269e3021894 | 32 | * @Note Synchronization level: Interrupt safe |
elessair | 0:f269e3021894 | 33 | * |
elessair | 0:f269e3021894 | 34 | * Example: |
elessair | 0:f269e3021894 | 35 | * @code |
elessair | 0:f269e3021894 | 36 | * // Switch on an LED if any of mbed pins 21-26 is high |
elessair | 0:f269e3021894 | 37 | * |
elessair | 0:f269e3021894 | 38 | * #include "mbed.h" |
elessair | 0:f269e3021894 | 39 | * |
elessair | 0:f269e3021894 | 40 | * PortIn p(Port2, 0x0000003F); // p21-p26 |
elessair | 0:f269e3021894 | 41 | * DigitalOut ind(LED4); |
elessair | 0:f269e3021894 | 42 | * |
elessair | 0:f269e3021894 | 43 | * int main() { |
elessair | 0:f269e3021894 | 44 | * while(1) { |
elessair | 0:f269e3021894 | 45 | * int pins = p.read(); |
elessair | 0:f269e3021894 | 46 | * if(pins) { |
elessair | 0:f269e3021894 | 47 | * ind = 1; |
elessair | 0:f269e3021894 | 48 | * } else { |
elessair | 0:f269e3021894 | 49 | * ind = 0; |
elessair | 0:f269e3021894 | 50 | * } |
elessair | 0:f269e3021894 | 51 | * } |
elessair | 0:f269e3021894 | 52 | * } |
elessair | 0:f269e3021894 | 53 | * @endcode |
elessair | 0:f269e3021894 | 54 | */ |
elessair | 0:f269e3021894 | 55 | class PortIn { |
elessair | 0:f269e3021894 | 56 | public: |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | /** Create an PortIn, connected to the specified port |
elessair | 0:f269e3021894 | 59 | * |
elessair | 0:f269e3021894 | 60 | * @param port Port to connect to (Port0-Port5) |
elessair | 0:f269e3021894 | 61 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
elessair | 0:f269e3021894 | 62 | */ |
elessair | 0:f269e3021894 | 63 | PortIn(PortName port, int mask = 0xFFFFFFFF) { |
elessair | 0:f269e3021894 | 64 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 65 | port_init(&_port, port, mask, PIN_INPUT); |
elessair | 0:f269e3021894 | 66 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 67 | } |
elessair | 0:f269e3021894 | 68 | |
elessair | 0:f269e3021894 | 69 | /** Read the value currently output on the port |
elessair | 0:f269e3021894 | 70 | * |
elessair | 0:f269e3021894 | 71 | * @returns |
elessair | 0:f269e3021894 | 72 | * An integer with each bit corresponding to associated port pin setting |
elessair | 0:f269e3021894 | 73 | */ |
elessair | 0:f269e3021894 | 74 | int read() { |
elessair | 0:f269e3021894 | 75 | return port_read(&_port); |
elessair | 0:f269e3021894 | 76 | } |
elessair | 0:f269e3021894 | 77 | |
elessair | 0:f269e3021894 | 78 | /** Set the input pin mode |
elessair | 0:f269e3021894 | 79 | * |
elessair | 0:f269e3021894 | 80 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
elessair | 0:f269e3021894 | 81 | */ |
elessair | 0:f269e3021894 | 82 | void mode(PinMode mode) { |
elessair | 0:f269e3021894 | 83 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 84 | port_mode(&_port, mode); |
elessair | 0:f269e3021894 | 85 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 86 | } |
elessair | 0:f269e3021894 | 87 | |
elessair | 0:f269e3021894 | 88 | /** A shorthand for read() |
elessair | 0:f269e3021894 | 89 | */ |
elessair | 0:f269e3021894 | 90 | operator int() { |
elessair | 0:f269e3021894 | 91 | return read(); |
elessair | 0:f269e3021894 | 92 | } |
elessair | 0:f269e3021894 | 93 | |
elessair | 0:f269e3021894 | 94 | private: |
elessair | 0:f269e3021894 | 95 | port_t _port; |
elessair | 0:f269e3021894 | 96 | }; |
elessair | 0:f269e3021894 | 97 | |
elessair | 0:f269e3021894 | 98 | } // namespace mbed |
elessair | 0:f269e3021894 | 99 | |
elessair | 0:f269e3021894 | 100 | #endif |
elessair | 0:f269e3021894 | 101 | |
elessair | 0:f269e3021894 | 102 | #endif |
elessair | 0:f269e3021894 | 103 | |
elessair | 0:f269e3021894 | 104 | /** @}*/ |