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_PORTINOUT_H
ganlikun 0:06036f8bee2d 17 #define MBED_PORTINOUT_H
ganlikun 0:06036f8bee2d 18
ganlikun 0:06036f8bee2d 19 #include "platform/platform.h"
ganlikun 0:06036f8bee2d 20
ganlikun 0:06036f8bee2d 21 #if defined (DEVICE_PORTINOUT) || defined(DOXYGEN_ONLY)
ganlikun 0:06036f8bee2d 22
ganlikun 0:06036f8bee2d 23 #include "hal/port_api.h"
ganlikun 0:06036f8bee2d 24 #include "platform/mbed_critical.h"
ganlikun 0:06036f8bee2d 25
ganlikun 0:06036f8bee2d 26 namespace mbed {
ganlikun 0:06036f8bee2d 27 /** \addtogroup drivers */
ganlikun 0:06036f8bee2d 28
ganlikun 0:06036f8bee2d 29 /** A multiple pin digital in/out used to set/read multiple bi-directional pins
ganlikun 0:06036f8bee2d 30 *
ganlikun 0:06036f8bee2d 31 * @note Synchronization level: Interrupt safe
ganlikun 0:06036f8bee2d 32 * @ingroup drivers
ganlikun 0:06036f8bee2d 33 */
ganlikun 0:06036f8bee2d 34 class PortInOut {
ganlikun 0:06036f8bee2d 35 public:
ganlikun 0:06036f8bee2d 36
ganlikun 0:06036f8bee2d 37 /** Create an PortInOut, connected to the specified port
ganlikun 0:06036f8bee2d 38 *
ganlikun 0:06036f8bee2d 39 * @param port Port to connect to (Port0-Port5)
ganlikun 0:06036f8bee2d 40 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
ganlikun 0:06036f8bee2d 41 */
ganlikun 0:06036f8bee2d 42 PortInOut(PortName port, int mask = 0xFFFFFFFF) {
ganlikun 0:06036f8bee2d 43 core_util_critical_section_enter();
ganlikun 0:06036f8bee2d 44 port_init(&_port, port, mask, PIN_INPUT);
ganlikun 0:06036f8bee2d 45 core_util_critical_section_exit();
ganlikun 0:06036f8bee2d 46 }
ganlikun 0:06036f8bee2d 47
ganlikun 0:06036f8bee2d 48 /** Write the value to the output port
ganlikun 0:06036f8bee2d 49 *
ganlikun 0:06036f8bee2d 50 * @param value An integer specifying a bit to write for every corresponding port pin
ganlikun 0:06036f8bee2d 51 */
ganlikun 0:06036f8bee2d 52 void write(int value) {
ganlikun 0:06036f8bee2d 53 port_write(&_port, value);
ganlikun 0:06036f8bee2d 54 }
ganlikun 0:06036f8bee2d 55
ganlikun 0:06036f8bee2d 56 /** Read the value currently output on the port
ganlikun 0:06036f8bee2d 57 *
ganlikun 0:06036f8bee2d 58 * @returns
ganlikun 0:06036f8bee2d 59 * An integer with each bit corresponding to associated port pin setting
ganlikun 0:06036f8bee2d 60 */
ganlikun 0:06036f8bee2d 61 int read() {
ganlikun 0:06036f8bee2d 62 return port_read(&_port);
ganlikun 0:06036f8bee2d 63 }
ganlikun 0:06036f8bee2d 64
ganlikun 0:06036f8bee2d 65 /** Set as an output
ganlikun 0:06036f8bee2d 66 */
ganlikun 0:06036f8bee2d 67 void output() {
ganlikun 0:06036f8bee2d 68 core_util_critical_section_enter();
ganlikun 0:06036f8bee2d 69 port_dir(&_port, PIN_OUTPUT);
ganlikun 0:06036f8bee2d 70 core_util_critical_section_exit();
ganlikun 0:06036f8bee2d 71 }
ganlikun 0:06036f8bee2d 72
ganlikun 0:06036f8bee2d 73 /** Set as an input
ganlikun 0:06036f8bee2d 74 */
ganlikun 0:06036f8bee2d 75 void input() {
ganlikun 0:06036f8bee2d 76 core_util_critical_section_enter();
ganlikun 0:06036f8bee2d 77 port_dir(&_port, PIN_INPUT);
ganlikun 0:06036f8bee2d 78 core_util_critical_section_exit();
ganlikun 0:06036f8bee2d 79 }
ganlikun 0:06036f8bee2d 80
ganlikun 0:06036f8bee2d 81 /** Set the input pin mode
ganlikun 0:06036f8bee2d 82 *
ganlikun 0:06036f8bee2d 83 * @param mode PullUp, PullDown, PullNone, OpenDrain
ganlikun 0:06036f8bee2d 84 */
ganlikun 0:06036f8bee2d 85 void mode(PinMode mode) {
ganlikun 0:06036f8bee2d 86 core_util_critical_section_enter();
ganlikun 0:06036f8bee2d 87 port_mode(&_port, mode);
ganlikun 0:06036f8bee2d 88 core_util_critical_section_exit();
ganlikun 0:06036f8bee2d 89 }
ganlikun 0:06036f8bee2d 90
ganlikun 0:06036f8bee2d 91 /** A shorthand for write()
ganlikun 0:06036f8bee2d 92 * \sa PortInOut::write()
ganlikun 0:06036f8bee2d 93 */
ganlikun 0:06036f8bee2d 94 PortInOut& operator= (int value) {
ganlikun 0:06036f8bee2d 95 write(value);
ganlikun 0:06036f8bee2d 96 return *this;
ganlikun 0:06036f8bee2d 97 }
ganlikun 0:06036f8bee2d 98
ganlikun 0:06036f8bee2d 99 /** A shorthand for write()
ganlikun 0:06036f8bee2d 100 * \sa PortInOut::write()
ganlikun 0:06036f8bee2d 101 */
ganlikun 0:06036f8bee2d 102 PortInOut& operator= (PortInOut& rhs) {
ganlikun 0:06036f8bee2d 103 write(rhs.read());
ganlikun 0:06036f8bee2d 104 return *this;
ganlikun 0:06036f8bee2d 105 }
ganlikun 0:06036f8bee2d 106
ganlikun 0:06036f8bee2d 107 /** A shorthand for read()
ganlikun 0:06036f8bee2d 108 * \sa PortInOut::read()
ganlikun 0:06036f8bee2d 109 */
ganlikun 0:06036f8bee2d 110 operator int() {
ganlikun 0:06036f8bee2d 111 return read();
ganlikun 0:06036f8bee2d 112 }
ganlikun 0:06036f8bee2d 113
ganlikun 0:06036f8bee2d 114 private:
ganlikun 0:06036f8bee2d 115 port_t _port;
ganlikun 0:06036f8bee2d 116 };
ganlikun 0:06036f8bee2d 117
ganlikun 0:06036f8bee2d 118 } // namespace mbed
ganlikun 0:06036f8bee2d 119
ganlikun 0:06036f8bee2d 120 #endif
ganlikun 0:06036f8bee2d 121
ganlikun 0:06036f8bee2d 122 #endif