This fork captures the mbed lib v125 for ease of integration into older projects.
Fork of mbed-dev by
api/PortInOut.h@187:92cbb9eec47b, 2018-07-20 (annotated)
- Committer:
- apluscw
- Date:
- Fri Jul 20 21:24:42 2018 +0000
- Revision:
- 187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apluscw | 187:92cbb9eec47b | 1 | /* mbed Microcontroller Library |
apluscw | 187:92cbb9eec47b | 2 | * Copyright (c) 2006-2013 ARM Limited |
apluscw | 187:92cbb9eec47b | 3 | * |
apluscw | 187:92cbb9eec47b | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
apluscw | 187:92cbb9eec47b | 5 | * you may not use this file except in compliance with the License. |
apluscw | 187:92cbb9eec47b | 6 | * You may obtain a copy of the License at |
apluscw | 187:92cbb9eec47b | 7 | * |
apluscw | 187:92cbb9eec47b | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
apluscw | 187:92cbb9eec47b | 9 | * |
apluscw | 187:92cbb9eec47b | 10 | * Unless required by applicable law or agreed to in writing, software |
apluscw | 187:92cbb9eec47b | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
apluscw | 187:92cbb9eec47b | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
apluscw | 187:92cbb9eec47b | 13 | * See the License for the specific language governing permissions and |
apluscw | 187:92cbb9eec47b | 14 | * limitations under the License. |
apluscw | 187:92cbb9eec47b | 15 | */ |
apluscw | 187:92cbb9eec47b | 16 | #ifndef MBED_PORTINOUT_H |
apluscw | 187:92cbb9eec47b | 17 | #define MBED_PORTINOUT_H |
apluscw | 187:92cbb9eec47b | 18 | |
apluscw | 187:92cbb9eec47b | 19 | #include "platform.h" |
apluscw | 187:92cbb9eec47b | 20 | |
apluscw | 187:92cbb9eec47b | 21 | #if DEVICE_PORTINOUT |
apluscw | 187:92cbb9eec47b | 22 | |
apluscw | 187:92cbb9eec47b | 23 | #include "port_api.h" |
apluscw | 187:92cbb9eec47b | 24 | #include "critical.h" |
apluscw | 187:92cbb9eec47b | 25 | |
apluscw | 187:92cbb9eec47b | 26 | namespace mbed { |
apluscw | 187:92cbb9eec47b | 27 | |
apluscw | 187:92cbb9eec47b | 28 | /** A multiple pin digital in/out used to set/read multiple bi-directional pins |
apluscw | 187:92cbb9eec47b | 29 | * |
apluscw | 187:92cbb9eec47b | 30 | * @Note Synchronization level: Interrupt safe |
apluscw | 187:92cbb9eec47b | 31 | */ |
apluscw | 187:92cbb9eec47b | 32 | class PortInOut { |
apluscw | 187:92cbb9eec47b | 33 | public: |
apluscw | 187:92cbb9eec47b | 34 | |
apluscw | 187:92cbb9eec47b | 35 | /** Create an PortInOut, connected to the specified port |
apluscw | 187:92cbb9eec47b | 36 | * |
apluscw | 187:92cbb9eec47b | 37 | * @param port Port to connect to (Port0-Port5) |
apluscw | 187:92cbb9eec47b | 38 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
apluscw | 187:92cbb9eec47b | 39 | */ |
apluscw | 187:92cbb9eec47b | 40 | PortInOut(PortName port, int mask = 0xFFFFFFFF) { |
apluscw | 187:92cbb9eec47b | 41 | core_util_critical_section_enter(); |
apluscw | 187:92cbb9eec47b | 42 | port_init(&_port, port, mask, PIN_INPUT); |
apluscw | 187:92cbb9eec47b | 43 | core_util_critical_section_exit(); |
apluscw | 187:92cbb9eec47b | 44 | } |
apluscw | 187:92cbb9eec47b | 45 | |
apluscw | 187:92cbb9eec47b | 46 | /** Write the value to the output port |
apluscw | 187:92cbb9eec47b | 47 | * |
apluscw | 187:92cbb9eec47b | 48 | * @param value An integer specifying a bit to write for every corresponding port pin |
apluscw | 187:92cbb9eec47b | 49 | */ |
apluscw | 187:92cbb9eec47b | 50 | void write(int value) { |
apluscw | 187:92cbb9eec47b | 51 | port_write(&_port, value); |
apluscw | 187:92cbb9eec47b | 52 | } |
apluscw | 187:92cbb9eec47b | 53 | |
apluscw | 187:92cbb9eec47b | 54 | /** Read the value currently output on the port |
apluscw | 187:92cbb9eec47b | 55 | * |
apluscw | 187:92cbb9eec47b | 56 | * @returns |
apluscw | 187:92cbb9eec47b | 57 | * An integer with each bit corresponding to associated port pin setting |
apluscw | 187:92cbb9eec47b | 58 | */ |
apluscw | 187:92cbb9eec47b | 59 | int read() { |
apluscw | 187:92cbb9eec47b | 60 | return port_read(&_port); |
apluscw | 187:92cbb9eec47b | 61 | } |
apluscw | 187:92cbb9eec47b | 62 | |
apluscw | 187:92cbb9eec47b | 63 | /** Set as an output |
apluscw | 187:92cbb9eec47b | 64 | */ |
apluscw | 187:92cbb9eec47b | 65 | void output() { |
apluscw | 187:92cbb9eec47b | 66 | core_util_critical_section_enter(); |
apluscw | 187:92cbb9eec47b | 67 | port_dir(&_port, PIN_OUTPUT); |
apluscw | 187:92cbb9eec47b | 68 | core_util_critical_section_exit(); |
apluscw | 187:92cbb9eec47b | 69 | } |
apluscw | 187:92cbb9eec47b | 70 | |
apluscw | 187:92cbb9eec47b | 71 | /** Set as an input |
apluscw | 187:92cbb9eec47b | 72 | */ |
apluscw | 187:92cbb9eec47b | 73 | void input() { |
apluscw | 187:92cbb9eec47b | 74 | core_util_critical_section_enter(); |
apluscw | 187:92cbb9eec47b | 75 | port_dir(&_port, PIN_INPUT); |
apluscw | 187:92cbb9eec47b | 76 | core_util_critical_section_exit(); |
apluscw | 187:92cbb9eec47b | 77 | } |
apluscw | 187:92cbb9eec47b | 78 | |
apluscw | 187:92cbb9eec47b | 79 | /** Set the input pin mode |
apluscw | 187:92cbb9eec47b | 80 | * |
apluscw | 187:92cbb9eec47b | 81 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
apluscw | 187:92cbb9eec47b | 82 | */ |
apluscw | 187:92cbb9eec47b | 83 | void mode(PinMode mode) { |
apluscw | 187:92cbb9eec47b | 84 | core_util_critical_section_enter(); |
apluscw | 187:92cbb9eec47b | 85 | port_mode(&_port, mode); |
apluscw | 187:92cbb9eec47b | 86 | core_util_critical_section_exit(); |
apluscw | 187:92cbb9eec47b | 87 | } |
apluscw | 187:92cbb9eec47b | 88 | |
apluscw | 187:92cbb9eec47b | 89 | /** A shorthand for write() |
apluscw | 187:92cbb9eec47b | 90 | */ |
apluscw | 187:92cbb9eec47b | 91 | PortInOut& operator= (int value) { |
apluscw | 187:92cbb9eec47b | 92 | write(value); |
apluscw | 187:92cbb9eec47b | 93 | return *this; |
apluscw | 187:92cbb9eec47b | 94 | } |
apluscw | 187:92cbb9eec47b | 95 | |
apluscw | 187:92cbb9eec47b | 96 | PortInOut& operator= (PortInOut& rhs) { |
apluscw | 187:92cbb9eec47b | 97 | write(rhs.read()); |
apluscw | 187:92cbb9eec47b | 98 | return *this; |
apluscw | 187:92cbb9eec47b | 99 | } |
apluscw | 187:92cbb9eec47b | 100 | |
apluscw | 187:92cbb9eec47b | 101 | /** A shorthand for read() |
apluscw | 187:92cbb9eec47b | 102 | */ |
apluscw | 187:92cbb9eec47b | 103 | operator int() { |
apluscw | 187:92cbb9eec47b | 104 | return read(); |
apluscw | 187:92cbb9eec47b | 105 | } |
apluscw | 187:92cbb9eec47b | 106 | |
apluscw | 187:92cbb9eec47b | 107 | private: |
apluscw | 187:92cbb9eec47b | 108 | port_t _port; |
apluscw | 187:92cbb9eec47b | 109 | }; |
apluscw | 187:92cbb9eec47b | 110 | |
apluscw | 187:92cbb9eec47b | 111 | } // namespace mbed |
apluscw | 187:92cbb9eec47b | 112 | |
apluscw | 187:92cbb9eec47b | 113 | #endif |
apluscw | 187:92cbb9eec47b | 114 | |
apluscw | 187:92cbb9eec47b | 115 | #endif |