CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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