CDY version that shares functionality with Counter
Dependencies: SDFileSystem_HelloWorld mbed FATFileSystem
mbed/drivers/DigitalInOut.h@3:c547dba5d39b, 2018-11-05 (annotated)
- 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?
User | Revision | Line number | New 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_DIGITALINOUT_H |
Charles David Young |
0:aa13e1c335cd | 17 | #define MBED_DIGITALINOUT_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 | #include "hal/gpio_api.h" |
Charles David Young |
0:aa13e1c335cd | 22 | #include "platform/critical.h" |
Charles David Young |
0:aa13e1c335cd | 23 | |
Charles David Young |
0:aa13e1c335cd | 24 | namespace mbed { |
Charles David Young |
0:aa13e1c335cd | 25 | /** \addtogroup drivers */ |
Charles David Young |
0:aa13e1c335cd | 26 | /** @{*/ |
Charles David Young |
0:aa13e1c335cd | 27 | |
Charles David Young |
0:aa13e1c335cd | 28 | /** A digital input/output, used for setting or reading a bi-directional pin |
Charles David Young |
0:aa13e1c335cd | 29 | * |
Charles David Young |
0:aa13e1c335cd | 30 | * @Note Synchronization level: Interrupt safe |
Charles David Young |
0:aa13e1c335cd | 31 | */ |
Charles David Young |
0:aa13e1c335cd | 32 | class DigitalInOut { |
Charles David Young |
0:aa13e1c335cd | 33 | |
Charles David Young |
0:aa13e1c335cd | 34 | public: |
Charles David Young |
0:aa13e1c335cd | 35 | /** Create a DigitalInOut connected to the specified pin |
Charles David Young |
0:aa13e1c335cd | 36 | * |
Charles David Young |
0:aa13e1c335cd | 37 | * @param pin DigitalInOut pin to connect to |
Charles David Young |
0:aa13e1c335cd | 38 | */ |
Charles David Young |
0:aa13e1c335cd | 39 | DigitalInOut(PinName pin) : gpio() { |
Charles David Young |
0:aa13e1c335cd | 40 | // No lock needed in the constructor |
Charles David Young |
0:aa13e1c335cd | 41 | gpio_init_in(&gpio, pin); |
Charles David Young |
0:aa13e1c335cd | 42 | } |
Charles David Young |
0:aa13e1c335cd | 43 | |
Charles David Young |
0:aa13e1c335cd | 44 | /** Create a DigitalInOut connected to the specified pin |
Charles David Young |
0:aa13e1c335cd | 45 | * |
Charles David Young |
0:aa13e1c335cd | 46 | * @param pin DigitalInOut pin to connect to |
Charles David Young |
0:aa13e1c335cd | 47 | * @param direction the initial direction of the pin |
Charles David Young |
0:aa13e1c335cd | 48 | * @param mode the initial mode of the pin |
Charles David Young |
0:aa13e1c335cd | 49 | * @param value the initial value of the pin if is an output |
Charles David Young |
0:aa13e1c335cd | 50 | */ |
Charles David Young |
0:aa13e1c335cd | 51 | DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() { |
Charles David Young |
0:aa13e1c335cd | 52 | // No lock needed in the constructor |
Charles David Young |
0:aa13e1c335cd | 53 | gpio_init_inout(&gpio, pin, direction, mode, value); |
Charles David Young |
0:aa13e1c335cd | 54 | } |
Charles David Young |
0:aa13e1c335cd | 55 | |
Charles David Young |
0:aa13e1c335cd | 56 | /** Set the output, specified as 0 or 1 (int) |
Charles David Young |
0:aa13e1c335cd | 57 | * |
Charles David Young |
0:aa13e1c335cd | 58 | * @param value An integer specifying the pin output value, |
Charles David Young |
0:aa13e1c335cd | 59 | * 0 for logical 0, 1 (or any other non-zero value) for logical 1 |
Charles David Young |
0:aa13e1c335cd | 60 | */ |
Charles David Young |
0:aa13e1c335cd | 61 | void write(int value) { |
Charles David Young |
0:aa13e1c335cd | 62 | // Thread safe / atomic HAL call |
Charles David Young |
0:aa13e1c335cd | 63 | gpio_write(&gpio, value); |
Charles David Young |
0:aa13e1c335cd | 64 | } |
Charles David Young |
0:aa13e1c335cd | 65 | |
Charles David Young |
0:aa13e1c335cd | 66 | /** Return the output setting, represented as 0 or 1 (int) |
Charles David Young |
0:aa13e1c335cd | 67 | * |
Charles David Young |
0:aa13e1c335cd | 68 | * @returns |
Charles David Young |
0:aa13e1c335cd | 69 | * an integer representing the output setting of the pin if it is an output, |
Charles David Young |
0:aa13e1c335cd | 70 | * or read the input if set as an input |
Charles David Young |
0:aa13e1c335cd | 71 | */ |
Charles David Young |
0:aa13e1c335cd | 72 | int read() { |
Charles David Young |
0:aa13e1c335cd | 73 | // Thread safe / atomic HAL call |
Charles David Young |
0:aa13e1c335cd | 74 | return gpio_read(&gpio); |
Charles David Young |
0:aa13e1c335cd | 75 | } |
Charles David Young |
0:aa13e1c335cd | 76 | |
Charles David Young |
0:aa13e1c335cd | 77 | /** Set as an output |
Charles David Young |
0:aa13e1c335cd | 78 | */ |
Charles David Young |
0:aa13e1c335cd | 79 | void output() { |
Charles David Young |
0:aa13e1c335cd | 80 | core_util_critical_section_enter(); |
Charles David Young |
0:aa13e1c335cd | 81 | gpio_dir(&gpio, PIN_OUTPUT); |
Charles David Young |
0:aa13e1c335cd | 82 | core_util_critical_section_exit(); |
Charles David Young |
0:aa13e1c335cd | 83 | } |
Charles David Young |
0:aa13e1c335cd | 84 | |
Charles David Young |
0:aa13e1c335cd | 85 | /** Set as an input |
Charles David Young |
0:aa13e1c335cd | 86 | */ |
Charles David Young |
0:aa13e1c335cd | 87 | void input() { |
Charles David Young |
0:aa13e1c335cd | 88 | core_util_critical_section_enter(); |
Charles David Young |
0:aa13e1c335cd | 89 | gpio_dir(&gpio, PIN_INPUT); |
Charles David Young |
0:aa13e1c335cd | 90 | core_util_critical_section_exit(); |
Charles David Young |
0:aa13e1c335cd | 91 | } |
Charles David Young |
0:aa13e1c335cd | 92 | |
Charles David Young |
0:aa13e1c335cd | 93 | /** Set the input pin mode |
Charles David Young |
0:aa13e1c335cd | 94 | * |
Charles David Young |
0:aa13e1c335cd | 95 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
Charles David Young |
0:aa13e1c335cd | 96 | */ |
Charles David Young |
0:aa13e1c335cd | 97 | void mode(PinMode pull) { |
Charles David Young |
0:aa13e1c335cd | 98 | core_util_critical_section_enter(); |
Charles David Young |
0:aa13e1c335cd | 99 | gpio_mode(&gpio, pull); |
Charles David Young |
0:aa13e1c335cd | 100 | core_util_critical_section_exit(); |
Charles David Young |
0:aa13e1c335cd | 101 | } |
Charles David Young |
0:aa13e1c335cd | 102 | |
Charles David Young |
0:aa13e1c335cd | 103 | /** Return the output setting, represented as 0 or 1 (int) |
Charles David Young |
0:aa13e1c335cd | 104 | * |
Charles David Young |
0:aa13e1c335cd | 105 | * @returns |
Charles David Young |
0:aa13e1c335cd | 106 | * Non zero value if pin is connected to uc GPIO |
Charles David Young |
0:aa13e1c335cd | 107 | * 0 if gpio object was initialized with NC |
Charles David Young |
0:aa13e1c335cd | 108 | */ |
Charles David Young |
0:aa13e1c335cd | 109 | int is_connected() { |
Charles David Young |
0:aa13e1c335cd | 110 | // Thread safe / atomic HAL call |
Charles David Young |
0:aa13e1c335cd | 111 | return gpio_is_connected(&gpio); |
Charles David Young |
0:aa13e1c335cd | 112 | } |
Charles David Young |
0:aa13e1c335cd | 113 | |
Charles David Young |
0:aa13e1c335cd | 114 | /** A shorthand for write() |
Charles David Young |
0:aa13e1c335cd | 115 | */ |
Charles David Young |
0:aa13e1c335cd | 116 | DigitalInOut& operator= (int value) { |
Charles David Young |
0:aa13e1c335cd | 117 | // Underlying write is thread safe |
Charles David Young |
0:aa13e1c335cd | 118 | write(value); |
Charles David Young |
0:aa13e1c335cd | 119 | return *this; |
Charles David Young |
0:aa13e1c335cd | 120 | } |
Charles David Young |
0:aa13e1c335cd | 121 | |
Charles David Young |
0:aa13e1c335cd | 122 | DigitalInOut& operator= (DigitalInOut& rhs) { |
Charles David Young |
0:aa13e1c335cd | 123 | core_util_critical_section_enter(); |
Charles David Young |
0:aa13e1c335cd | 124 | write(rhs.read()); |
Charles David Young |
0:aa13e1c335cd | 125 | core_util_critical_section_exit(); |
Charles David Young |
0:aa13e1c335cd | 126 | return *this; |
Charles David Young |
0:aa13e1c335cd | 127 | } |
Charles David Young |
0:aa13e1c335cd | 128 | |
Charles David Young |
0:aa13e1c335cd | 129 | /** A shorthand for read() |
Charles David Young |
0:aa13e1c335cd | 130 | */ |
Charles David Young |
0:aa13e1c335cd | 131 | operator int() { |
Charles David Young |
0:aa13e1c335cd | 132 | // Underlying call is thread safe |
Charles David Young |
0:aa13e1c335cd | 133 | return read(); |
Charles David Young |
0:aa13e1c335cd | 134 | } |
Charles David Young |
0:aa13e1c335cd | 135 | |
Charles David Young |
0:aa13e1c335cd | 136 | protected: |
Charles David Young |
0:aa13e1c335cd | 137 | gpio_t gpio; |
Charles David Young |
0:aa13e1c335cd | 138 | }; |
Charles David Young |
0:aa13e1c335cd | 139 | |
Charles David Young |
0:aa13e1c335cd | 140 | } // namespace mbed |
Charles David Young |
0:aa13e1c335cd | 141 | |
Charles David Young |
0:aa13e1c335cd | 142 | #endif |
Charles David Young |
0:aa13e1c335cd | 143 | |
Charles David Young |
0:aa13e1c335cd | 144 | /** @}*/ |