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