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