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