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