mbed library sources. Supersedes mbed-src.
Dependents: Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more
drivers/DigitalInOut.h@187:0387e8f68319, 2018-09-06 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Sep 06 13:40:20 2018 +0100
- Revision:
- 187:0387e8f68319
- Parent:
- 167:e84263d55307
- Child:
- 188:bcfe06ba3d64
mbed-dev library. Release version 163
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_DIGITALINOUT_H |
<> | 149:156823d33999 | 17 | #define MBED_DIGITALINOUT_H |
<> | 149:156823d33999 | 18 | |
<> | 149:156823d33999 | 19 | #include "platform/platform.h" |
<> | 149:156823d33999 | 20 | |
<> | 149:156823d33999 | 21 | #include "hal/gpio_api.h" |
<> | 160:d5399cc887bb | 22 | #include "platform/mbed_critical.h" |
<> | 149:156823d33999 | 23 | |
<> | 149:156823d33999 | 24 | namespace mbed { |
<> | 149:156823d33999 | 25 | /** \addtogroup drivers */ |
<> | 149:156823d33999 | 26 | |
<> | 149:156823d33999 | 27 | /** A digital input/output, used for setting or reading a bi-directional pin |
<> | 149:156823d33999 | 28 | * |
AnnaBridge | 167:e84263d55307 | 29 | * @note Synchronization level: Interrupt safe |
AnnaBridge | 167:e84263d55307 | 30 | * @ingroup drivers |
<> | 149:156823d33999 | 31 | */ |
<> | 149:156823d33999 | 32 | class DigitalInOut { |
<> | 149:156823d33999 | 33 | |
<> | 149:156823d33999 | 34 | public: |
<> | 149:156823d33999 | 35 | /** Create a DigitalInOut connected to the specified pin |
<> | 149:156823d33999 | 36 | * |
<> | 149:156823d33999 | 37 | * @param pin DigitalInOut pin to connect to |
<> | 149:156823d33999 | 38 | */ |
AnnaBridge | 187:0387e8f68319 | 39 | DigitalInOut(PinName pin) : gpio() |
AnnaBridge | 187:0387e8f68319 | 40 | { |
<> | 149:156823d33999 | 41 | // No lock needed in the constructor |
<> | 149:156823d33999 | 42 | gpio_init_in(&gpio, pin); |
<> | 149:156823d33999 | 43 | } |
<> | 149:156823d33999 | 44 | |
<> | 149:156823d33999 | 45 | /** Create a DigitalInOut connected to the specified pin |
<> | 149:156823d33999 | 46 | * |
<> | 149:156823d33999 | 47 | * @param pin DigitalInOut pin to connect to |
<> | 149:156823d33999 | 48 | * @param direction the initial direction of the pin |
<> | 149:156823d33999 | 49 | * @param mode the initial mode of the pin |
<> | 149:156823d33999 | 50 | * @param value the initial value of the pin if is an output |
<> | 149:156823d33999 | 51 | */ |
AnnaBridge | 187:0387e8f68319 | 52 | DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() |
AnnaBridge | 187:0387e8f68319 | 53 | { |
<> | 149:156823d33999 | 54 | // No lock needed in the constructor |
<> | 149:156823d33999 | 55 | gpio_init_inout(&gpio, pin, direction, mode, value); |
<> | 149:156823d33999 | 56 | } |
<> | 149:156823d33999 | 57 | |
<> | 149:156823d33999 | 58 | /** Set the output, specified as 0 or 1 (int) |
<> | 149:156823d33999 | 59 | * |
<> | 149:156823d33999 | 60 | * @param value An integer specifying the pin output value, |
<> | 149:156823d33999 | 61 | * 0 for logical 0, 1 (or any other non-zero value) for logical 1 |
<> | 149:156823d33999 | 62 | */ |
AnnaBridge | 187:0387e8f68319 | 63 | void write(int value) |
AnnaBridge | 187:0387e8f68319 | 64 | { |
<> | 149:156823d33999 | 65 | // Thread safe / atomic HAL call |
<> | 149:156823d33999 | 66 | gpio_write(&gpio, value); |
<> | 149:156823d33999 | 67 | } |
<> | 149:156823d33999 | 68 | |
<> | 149:156823d33999 | 69 | /** Return the output setting, represented as 0 or 1 (int) |
<> | 149:156823d33999 | 70 | * |
<> | 149:156823d33999 | 71 | * @returns |
<> | 149:156823d33999 | 72 | * an integer representing the output setting of the pin if it is an output, |
<> | 149:156823d33999 | 73 | * or read the input if set as an input |
<> | 149:156823d33999 | 74 | */ |
AnnaBridge | 187:0387e8f68319 | 75 | int read() |
AnnaBridge | 187:0387e8f68319 | 76 | { |
<> | 149:156823d33999 | 77 | // Thread safe / atomic HAL call |
<> | 149:156823d33999 | 78 | return gpio_read(&gpio); |
<> | 149:156823d33999 | 79 | } |
<> | 149:156823d33999 | 80 | |
<> | 149:156823d33999 | 81 | /** Set as an output |
<> | 149:156823d33999 | 82 | */ |
AnnaBridge | 187:0387e8f68319 | 83 | void output() |
AnnaBridge | 187:0387e8f68319 | 84 | { |
<> | 149:156823d33999 | 85 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 86 | gpio_dir(&gpio, PIN_OUTPUT); |
<> | 149:156823d33999 | 87 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 88 | } |
<> | 149:156823d33999 | 89 | |
<> | 149:156823d33999 | 90 | /** Set as an input |
<> | 149:156823d33999 | 91 | */ |
AnnaBridge | 187:0387e8f68319 | 92 | void input() |
AnnaBridge | 187:0387e8f68319 | 93 | { |
<> | 149:156823d33999 | 94 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 95 | gpio_dir(&gpio, PIN_INPUT); |
<> | 149:156823d33999 | 96 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 97 | } |
<> | 149:156823d33999 | 98 | |
<> | 149:156823d33999 | 99 | /** Set the input pin mode |
<> | 149:156823d33999 | 100 | * |
AnnaBridge | 167:e84263d55307 | 101 | * @param pull PullUp, PullDown, PullNone, OpenDrain |
<> | 149:156823d33999 | 102 | */ |
AnnaBridge | 187:0387e8f68319 | 103 | void mode(PinMode pull) |
AnnaBridge | 187:0387e8f68319 | 104 | { |
<> | 149:156823d33999 | 105 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 106 | gpio_mode(&gpio, pull); |
<> | 149:156823d33999 | 107 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 108 | } |
<> | 149:156823d33999 | 109 | |
<> | 149:156823d33999 | 110 | /** Return the output setting, represented as 0 or 1 (int) |
<> | 149:156823d33999 | 111 | * |
<> | 149:156823d33999 | 112 | * @returns |
<> | 149:156823d33999 | 113 | * Non zero value if pin is connected to uc GPIO |
<> | 149:156823d33999 | 114 | * 0 if gpio object was initialized with NC |
<> | 149:156823d33999 | 115 | */ |
AnnaBridge | 187:0387e8f68319 | 116 | int is_connected() |
AnnaBridge | 187:0387e8f68319 | 117 | { |
<> | 149:156823d33999 | 118 | // Thread safe / atomic HAL call |
<> | 149:156823d33999 | 119 | return gpio_is_connected(&gpio); |
<> | 149:156823d33999 | 120 | } |
<> | 149:156823d33999 | 121 | |
<> | 149:156823d33999 | 122 | /** A shorthand for write() |
AnnaBridge | 167:e84263d55307 | 123 | * \sa DigitalInOut::write() |
<> | 149:156823d33999 | 124 | */ |
AnnaBridge | 187:0387e8f68319 | 125 | DigitalInOut &operator= (int value) |
AnnaBridge | 187:0387e8f68319 | 126 | { |
<> | 149:156823d33999 | 127 | // Underlying write is thread safe |
<> | 149:156823d33999 | 128 | write(value); |
<> | 149:156823d33999 | 129 | return *this; |
<> | 149:156823d33999 | 130 | } |
<> | 149:156823d33999 | 131 | |
AnnaBridge | 167:e84263d55307 | 132 | /** A shorthand for write() |
AnnaBridge | 167:e84263d55307 | 133 | * \sa DigitalInOut::write() |
AnnaBridge | 167:e84263d55307 | 134 | */ |
AnnaBridge | 187:0387e8f68319 | 135 | DigitalInOut &operator= (DigitalInOut &rhs) |
AnnaBridge | 187:0387e8f68319 | 136 | { |
<> | 149:156823d33999 | 137 | core_util_critical_section_enter(); |
<> | 149:156823d33999 | 138 | write(rhs.read()); |
<> | 149:156823d33999 | 139 | core_util_critical_section_exit(); |
<> | 149:156823d33999 | 140 | return *this; |
<> | 149:156823d33999 | 141 | } |
<> | 149:156823d33999 | 142 | |
<> | 149:156823d33999 | 143 | /** A shorthand for read() |
AnnaBridge | 167:e84263d55307 | 144 | * \sa DigitalInOut::read() |
<> | 149:156823d33999 | 145 | */ |
AnnaBridge | 187:0387e8f68319 | 146 | operator int() |
AnnaBridge | 187:0387e8f68319 | 147 | { |
<> | 149:156823d33999 | 148 | // Underlying call is thread safe |
<> | 149:156823d33999 | 149 | return read(); |
<> | 149:156823d33999 | 150 | } |
<> | 149:156823d33999 | 151 | |
<> | 149:156823d33999 | 152 | protected: |
<> | 149:156823d33999 | 153 | gpio_t gpio; |
<> | 149:156823d33999 | 154 | }; |
<> | 149:156823d33999 | 155 | |
<> | 149:156823d33999 | 156 | } // namespace mbed |
<> | 149:156823d33999 | 157 | |
<> | 149:156823d33999 | 158 | #endif |