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