mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
cmsis/BUILD/mbed/drivers/PortInOut.h@189:f392fc9709a3, 2019-02-20 (annotated)
- Committer:
- AnnaBridge
- Date:
- Wed Feb 20 22:31:08 2019 +0000
- Revision:
- 189:f392fc9709a3
mbed library release version 165
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 189:f392fc9709a3 | 1 | /* mbed Microcontroller Library |
AnnaBridge | 189:f392fc9709a3 | 2 | * Copyright (c) 2006-2013 ARM Limited |
AnnaBridge | 189:f392fc9709a3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
AnnaBridge | 189:f392fc9709a3 | 4 | * |
AnnaBridge | 189:f392fc9709a3 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 189:f392fc9709a3 | 6 | * you may not use this file except in compliance with the License. |
AnnaBridge | 189:f392fc9709a3 | 7 | * You may obtain a copy of the License at |
AnnaBridge | 189:f392fc9709a3 | 8 | * |
AnnaBridge | 189:f392fc9709a3 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 189:f392fc9709a3 | 10 | * |
AnnaBridge | 189:f392fc9709a3 | 11 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 189:f392fc9709a3 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 189:f392fc9709a3 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 189:f392fc9709a3 | 14 | * See the License for the specific language governing permissions and |
AnnaBridge | 189:f392fc9709a3 | 15 | * limitations under the License. |
AnnaBridge | 189:f392fc9709a3 | 16 | */ |
AnnaBridge | 189:f392fc9709a3 | 17 | #ifndef MBED_PORTINOUT_H |
AnnaBridge | 189:f392fc9709a3 | 18 | #define MBED_PORTINOUT_H |
AnnaBridge | 189:f392fc9709a3 | 19 | |
AnnaBridge | 189:f392fc9709a3 | 20 | #include "platform/platform.h" |
AnnaBridge | 189:f392fc9709a3 | 21 | |
AnnaBridge | 189:f392fc9709a3 | 22 | #if DEVICE_PORTINOUT || defined(DOXYGEN_ONLY) |
AnnaBridge | 189:f392fc9709a3 | 23 | |
AnnaBridge | 189:f392fc9709a3 | 24 | #include "hal/port_api.h" |
AnnaBridge | 189:f392fc9709a3 | 25 | #include "platform/mbed_critical.h" |
AnnaBridge | 189:f392fc9709a3 | 26 | |
AnnaBridge | 189:f392fc9709a3 | 27 | namespace mbed { |
AnnaBridge | 189:f392fc9709a3 | 28 | /** \addtogroup drivers */ |
AnnaBridge | 189:f392fc9709a3 | 29 | |
AnnaBridge | 189:f392fc9709a3 | 30 | /** A multiple pin digital in/out used to set/read multiple bi-directional pins |
AnnaBridge | 189:f392fc9709a3 | 31 | * |
AnnaBridge | 189:f392fc9709a3 | 32 | * @note Synchronization level: Interrupt safe |
AnnaBridge | 189:f392fc9709a3 | 33 | * @ingroup drivers |
AnnaBridge | 189:f392fc9709a3 | 34 | */ |
AnnaBridge | 189:f392fc9709a3 | 35 | class PortInOut { |
AnnaBridge | 189:f392fc9709a3 | 36 | public: |
AnnaBridge | 189:f392fc9709a3 | 37 | |
AnnaBridge | 189:f392fc9709a3 | 38 | /** Create an PortInOut, connected to the specified port |
AnnaBridge | 189:f392fc9709a3 | 39 | * |
AnnaBridge | 189:f392fc9709a3 | 40 | * @param port Port to connect to (Port0-Port5) |
AnnaBridge | 189:f392fc9709a3 | 41 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
AnnaBridge | 189:f392fc9709a3 | 42 | */ |
AnnaBridge | 189:f392fc9709a3 | 43 | PortInOut(PortName port, int mask = 0xFFFFFFFF) |
AnnaBridge | 189:f392fc9709a3 | 44 | { |
AnnaBridge | 189:f392fc9709a3 | 45 | core_util_critical_section_enter(); |
AnnaBridge | 189:f392fc9709a3 | 46 | port_init(&_port, port, mask, PIN_INPUT); |
AnnaBridge | 189:f392fc9709a3 | 47 | core_util_critical_section_exit(); |
AnnaBridge | 189:f392fc9709a3 | 48 | } |
AnnaBridge | 189:f392fc9709a3 | 49 | |
AnnaBridge | 189:f392fc9709a3 | 50 | /** Write the value to the output port |
AnnaBridge | 189:f392fc9709a3 | 51 | * |
AnnaBridge | 189:f392fc9709a3 | 52 | * @param value An integer specifying a bit to write for every corresponding port pin |
AnnaBridge | 189:f392fc9709a3 | 53 | */ |
AnnaBridge | 189:f392fc9709a3 | 54 | void write(int value) |
AnnaBridge | 189:f392fc9709a3 | 55 | { |
AnnaBridge | 189:f392fc9709a3 | 56 | port_write(&_port, value); |
AnnaBridge | 189:f392fc9709a3 | 57 | } |
AnnaBridge | 189:f392fc9709a3 | 58 | |
AnnaBridge | 189:f392fc9709a3 | 59 | /** Read the value currently output on the port |
AnnaBridge | 189:f392fc9709a3 | 60 | * |
AnnaBridge | 189:f392fc9709a3 | 61 | * @returns |
AnnaBridge | 189:f392fc9709a3 | 62 | * An integer with each bit corresponding to associated port pin setting |
AnnaBridge | 189:f392fc9709a3 | 63 | */ |
AnnaBridge | 189:f392fc9709a3 | 64 | int read() |
AnnaBridge | 189:f392fc9709a3 | 65 | { |
AnnaBridge | 189:f392fc9709a3 | 66 | return port_read(&_port); |
AnnaBridge | 189:f392fc9709a3 | 67 | } |
AnnaBridge | 189:f392fc9709a3 | 68 | |
AnnaBridge | 189:f392fc9709a3 | 69 | /** Set as an output |
AnnaBridge | 189:f392fc9709a3 | 70 | */ |
AnnaBridge | 189:f392fc9709a3 | 71 | void output() |
AnnaBridge | 189:f392fc9709a3 | 72 | { |
AnnaBridge | 189:f392fc9709a3 | 73 | core_util_critical_section_enter(); |
AnnaBridge | 189:f392fc9709a3 | 74 | port_dir(&_port, PIN_OUTPUT); |
AnnaBridge | 189:f392fc9709a3 | 75 | core_util_critical_section_exit(); |
AnnaBridge | 189:f392fc9709a3 | 76 | } |
AnnaBridge | 189:f392fc9709a3 | 77 | |
AnnaBridge | 189:f392fc9709a3 | 78 | /** Set as an input |
AnnaBridge | 189:f392fc9709a3 | 79 | */ |
AnnaBridge | 189:f392fc9709a3 | 80 | void input() |
AnnaBridge | 189:f392fc9709a3 | 81 | { |
AnnaBridge | 189:f392fc9709a3 | 82 | core_util_critical_section_enter(); |
AnnaBridge | 189:f392fc9709a3 | 83 | port_dir(&_port, PIN_INPUT); |
AnnaBridge | 189:f392fc9709a3 | 84 | core_util_critical_section_exit(); |
AnnaBridge | 189:f392fc9709a3 | 85 | } |
AnnaBridge | 189:f392fc9709a3 | 86 | |
AnnaBridge | 189:f392fc9709a3 | 87 | /** Set the input pin mode |
AnnaBridge | 189:f392fc9709a3 | 88 | * |
AnnaBridge | 189:f392fc9709a3 | 89 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
AnnaBridge | 189:f392fc9709a3 | 90 | */ |
AnnaBridge | 189:f392fc9709a3 | 91 | void mode(PinMode mode) |
AnnaBridge | 189:f392fc9709a3 | 92 | { |
AnnaBridge | 189:f392fc9709a3 | 93 | core_util_critical_section_enter(); |
AnnaBridge | 189:f392fc9709a3 | 94 | port_mode(&_port, mode); |
AnnaBridge | 189:f392fc9709a3 | 95 | core_util_critical_section_exit(); |
AnnaBridge | 189:f392fc9709a3 | 96 | } |
AnnaBridge | 189:f392fc9709a3 | 97 | |
AnnaBridge | 189:f392fc9709a3 | 98 | /** A shorthand for write() |
AnnaBridge | 189:f392fc9709a3 | 99 | * \sa PortInOut::write() |
AnnaBridge | 189:f392fc9709a3 | 100 | */ |
AnnaBridge | 189:f392fc9709a3 | 101 | PortInOut &operator= (int value) |
AnnaBridge | 189:f392fc9709a3 | 102 | { |
AnnaBridge | 189:f392fc9709a3 | 103 | write(value); |
AnnaBridge | 189:f392fc9709a3 | 104 | return *this; |
AnnaBridge | 189:f392fc9709a3 | 105 | } |
AnnaBridge | 189:f392fc9709a3 | 106 | |
AnnaBridge | 189:f392fc9709a3 | 107 | /** A shorthand for write() |
AnnaBridge | 189:f392fc9709a3 | 108 | * \sa PortInOut::write() |
AnnaBridge | 189:f392fc9709a3 | 109 | */ |
AnnaBridge | 189:f392fc9709a3 | 110 | PortInOut &operator= (PortInOut &rhs) |
AnnaBridge | 189:f392fc9709a3 | 111 | { |
AnnaBridge | 189:f392fc9709a3 | 112 | write(rhs.read()); |
AnnaBridge | 189:f392fc9709a3 | 113 | return *this; |
AnnaBridge | 189:f392fc9709a3 | 114 | } |
AnnaBridge | 189:f392fc9709a3 | 115 | |
AnnaBridge | 189:f392fc9709a3 | 116 | /** A shorthand for read() |
AnnaBridge | 189:f392fc9709a3 | 117 | * \sa PortInOut::read() |
AnnaBridge | 189:f392fc9709a3 | 118 | */ |
AnnaBridge | 189:f392fc9709a3 | 119 | operator int() |
AnnaBridge | 189:f392fc9709a3 | 120 | { |
AnnaBridge | 189:f392fc9709a3 | 121 | return read(); |
AnnaBridge | 189:f392fc9709a3 | 122 | } |
AnnaBridge | 189:f392fc9709a3 | 123 | |
AnnaBridge | 189:f392fc9709a3 | 124 | private: |
AnnaBridge | 189:f392fc9709a3 | 125 | port_t _port; |
AnnaBridge | 189:f392fc9709a3 | 126 | }; |
AnnaBridge | 189:f392fc9709a3 | 127 | |
AnnaBridge | 189:f392fc9709a3 | 128 | } // namespace mbed |
AnnaBridge | 189:f392fc9709a3 | 129 | |
AnnaBridge | 189:f392fc9709a3 | 130 | #endif |
AnnaBridge | 189:f392fc9709a3 | 131 | |
AnnaBridge | 189:f392fc9709a3 | 132 | #endif |