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