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