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