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