00

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /* mbed Microcontroller Library
ganlikun 0:13413ea9a877 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:13413ea9a877 3 *
ganlikun 0:13413ea9a877 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:13413ea9a877 5 * you may not use this file except in compliance with the License.
ganlikun 0:13413ea9a877 6 * You may obtain a copy of the License at
ganlikun 0:13413ea9a877 7 *
ganlikun 0:13413ea9a877 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:13413ea9a877 9 *
ganlikun 0:13413ea9a877 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:13413ea9a877 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:13413ea9a877 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:13413ea9a877 13 * See the License for the specific language governing permissions and
ganlikun 0:13413ea9a877 14 * limitations under the License.
ganlikun 0:13413ea9a877 15 */
ganlikun 0:13413ea9a877 16 #ifndef MBED_DIGITALINOUT_H
ganlikun 0:13413ea9a877 17 #define MBED_DIGITALINOUT_H
ganlikun 0:13413ea9a877 18
ganlikun 0:13413ea9a877 19 #include "platform/platform.h"
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 #include "hal/gpio_api.h"
ganlikun 0:13413ea9a877 22 #include "platform/mbed_critical.h"
ganlikun 0:13413ea9a877 23
ganlikun 0:13413ea9a877 24 namespace mbed {
ganlikun 0:13413ea9a877 25 /** \addtogroup drivers */
ganlikun 0:13413ea9a877 26
ganlikun 0:13413ea9a877 27 /** A digital input/output, used for setting or reading a bi-directional pin
ganlikun 0:13413ea9a877 28 *
ganlikun 0:13413ea9a877 29 * @note Synchronization level: Interrupt safe
ganlikun 0:13413ea9a877 30 * @ingroup drivers
ganlikun 0:13413ea9a877 31 */
ganlikun 0:13413ea9a877 32 class DigitalInOut {
ganlikun 0:13413ea9a877 33
ganlikun 0:13413ea9a877 34 public:
ganlikun 0:13413ea9a877 35 /** Create a DigitalInOut connected to the specified pin
ganlikun 0:13413ea9a877 36 *
ganlikun 0:13413ea9a877 37 * @param pin DigitalInOut pin to connect to
ganlikun 0:13413ea9a877 38 */
ganlikun 0:13413ea9a877 39 DigitalInOut(PinName pin) : gpio() {
ganlikun 0:13413ea9a877 40 // No lock needed in the constructor
ganlikun 0:13413ea9a877 41 gpio_init_in(&gpio, pin);
ganlikun 0:13413ea9a877 42 }
ganlikun 0:13413ea9a877 43
ganlikun 0:13413ea9a877 44 /** Create a DigitalInOut connected to the specified pin
ganlikun 0:13413ea9a877 45 *
ganlikun 0:13413ea9a877 46 * @param pin DigitalInOut pin to connect to
ganlikun 0:13413ea9a877 47 * @param direction the initial direction of the pin
ganlikun 0:13413ea9a877 48 * @param mode the initial mode of the pin
ganlikun 0:13413ea9a877 49 * @param value the initial value of the pin if is an output
ganlikun 0:13413ea9a877 50 */
ganlikun 0:13413ea9a877 51 DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() {
ganlikun 0:13413ea9a877 52 // No lock needed in the constructor
ganlikun 0:13413ea9a877 53 gpio_init_inout(&gpio, pin, direction, mode, value);
ganlikun 0:13413ea9a877 54 }
ganlikun 0:13413ea9a877 55
ganlikun 0:13413ea9a877 56 /** Set the output, specified as 0 or 1 (int)
ganlikun 0:13413ea9a877 57 *
ganlikun 0:13413ea9a877 58 * @param value An integer specifying the pin output value,
ganlikun 0:13413ea9a877 59 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
ganlikun 0:13413ea9a877 60 */
ganlikun 0:13413ea9a877 61 void write(int value) {
ganlikun 0:13413ea9a877 62 // Thread safe / atomic HAL call
ganlikun 0:13413ea9a877 63 gpio_write(&gpio, value);
ganlikun 0:13413ea9a877 64 }
ganlikun 0:13413ea9a877 65
ganlikun 0:13413ea9a877 66 /** Return the output setting, represented as 0 or 1 (int)
ganlikun 0:13413ea9a877 67 *
ganlikun 0:13413ea9a877 68 * @returns
ganlikun 0:13413ea9a877 69 * an integer representing the output setting of the pin if it is an output,
ganlikun 0:13413ea9a877 70 * or read the input if set as an input
ganlikun 0:13413ea9a877 71 */
ganlikun 0:13413ea9a877 72 int read() {
ganlikun 0:13413ea9a877 73 // Thread safe / atomic HAL call
ganlikun 0:13413ea9a877 74 return gpio_read(&gpio);
ganlikun 0:13413ea9a877 75 }
ganlikun 0:13413ea9a877 76
ganlikun 0:13413ea9a877 77 /** Set as an output
ganlikun 0:13413ea9a877 78 */
ganlikun 0:13413ea9a877 79 void output() {
ganlikun 0:13413ea9a877 80 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 81 gpio_dir(&gpio, PIN_OUTPUT);
ganlikun 0:13413ea9a877 82 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 83 }
ganlikun 0:13413ea9a877 84
ganlikun 0:13413ea9a877 85 /** Set as an input
ganlikun 0:13413ea9a877 86 */
ganlikun 0:13413ea9a877 87 void input() {
ganlikun 0:13413ea9a877 88 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 89 gpio_dir(&gpio, PIN_INPUT);
ganlikun 0:13413ea9a877 90 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 91 }
ganlikun 0:13413ea9a877 92
ganlikun 0:13413ea9a877 93 /** Set the input pin mode
ganlikun 0:13413ea9a877 94 *
ganlikun 0:13413ea9a877 95 * @param pull PullUp, PullDown, PullNone, OpenDrain
ganlikun 0:13413ea9a877 96 */
ganlikun 0:13413ea9a877 97 void mode(PinMode pull) {
ganlikun 0:13413ea9a877 98 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 99 gpio_mode(&gpio, pull);
ganlikun 0:13413ea9a877 100 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 101 }
ganlikun 0:13413ea9a877 102
ganlikun 0:13413ea9a877 103 /** Return the output setting, represented as 0 or 1 (int)
ganlikun 0:13413ea9a877 104 *
ganlikun 0:13413ea9a877 105 * @returns
ganlikun 0:13413ea9a877 106 * Non zero value if pin is connected to uc GPIO
ganlikun 0:13413ea9a877 107 * 0 if gpio object was initialized with NC
ganlikun 0:13413ea9a877 108 */
ganlikun 0:13413ea9a877 109 int is_connected() {
ganlikun 0:13413ea9a877 110 // Thread safe / atomic HAL call
ganlikun 0:13413ea9a877 111 return gpio_is_connected(&gpio);
ganlikun 0:13413ea9a877 112 }
ganlikun 0:13413ea9a877 113
ganlikun 0:13413ea9a877 114 /** A shorthand for write()
ganlikun 0:13413ea9a877 115 * \sa DigitalInOut::write()
ganlikun 0:13413ea9a877 116 */
ganlikun 0:13413ea9a877 117 DigitalInOut& operator= (int value) {
ganlikun 0:13413ea9a877 118 // Underlying write is thread safe
ganlikun 0:13413ea9a877 119 write(value);
ganlikun 0:13413ea9a877 120 return *this;
ganlikun 0:13413ea9a877 121 }
ganlikun 0:13413ea9a877 122
ganlikun 0:13413ea9a877 123 /** A shorthand for write()
ganlikun 0:13413ea9a877 124 * \sa DigitalInOut::write()
ganlikun 0:13413ea9a877 125 */
ganlikun 0:13413ea9a877 126 DigitalInOut& operator= (DigitalInOut& rhs) {
ganlikun 0:13413ea9a877 127 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 128 write(rhs.read());
ganlikun 0:13413ea9a877 129 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 130 return *this;
ganlikun 0:13413ea9a877 131 }
ganlikun 0:13413ea9a877 132
ganlikun 0:13413ea9a877 133 /** A shorthand for read()
ganlikun 0:13413ea9a877 134 * \sa DigitalInOut::read()
ganlikun 0:13413ea9a877 135 */
ganlikun 0:13413ea9a877 136 operator int() {
ganlikun 0:13413ea9a877 137 // Underlying call is thread safe
ganlikun 0:13413ea9a877 138 return read();
ganlikun 0:13413ea9a877 139 }
ganlikun 0:13413ea9a877 140
ganlikun 0:13413ea9a877 141 protected:
ganlikun 0:13413ea9a877 142 gpio_t gpio;
ganlikun 0:13413ea9a877 143 };
ganlikun 0:13413ea9a877 144
ganlikun 0:13413ea9a877 145 } // namespace mbed
ganlikun 0:13413ea9a877 146
ganlikun 0:13413ea9a877 147 #endif
ganlikun 0:13413ea9a877 148
ganlikun 0:13413ea9a877 149