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