mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
187:0387e8f68319
mbed library release version 165

Who changed what in which revision?

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