Backup 1

Committer:
borlanic
Date:
Tue Apr 24 11:45:18 2018 +0000
Revision:
0:02dd72d1d465
BaBoRo_test2 - backup 1

Who changed what in which revision?

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