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