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