TUKS MCU Introductory course / TUKS-COURSE-2-LED
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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