mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2006-2013 ARM Limited
AnnaBridge 189:f392fc9709a3 3 * SPDX-License-Identifier: Apache-2.0
<> 149:156823d33999 4 *
<> 149:156823d33999 5 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 6 * you may not use this file except in compliance with the License.
<> 149:156823d33999 7 * You may obtain a copy of the License at
<> 149:156823d33999 8 *
<> 149:156823d33999 9 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 10 *
<> 149:156823d33999 11 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 12 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 14 * See the License for the specific language governing permissions and
<> 149:156823d33999 15 * limitations under the License.
<> 149:156823d33999 16 */
<> 149:156823d33999 17 #ifndef MBED_DIGITALINOUT_H
<> 149:156823d33999 18 #define MBED_DIGITALINOUT_H
<> 149:156823d33999 19
<> 149:156823d33999 20 #include "platform/platform.h"
<> 149:156823d33999 21
<> 149:156823d33999 22 #include "hal/gpio_api.h"
<> 160:d5399cc887bb 23 #include "platform/mbed_critical.h"
<> 149:156823d33999 24
<> 149:156823d33999 25 namespace mbed {
<> 149:156823d33999 26 /** \addtogroup drivers */
<> 149:156823d33999 27
<> 149:156823d33999 28 /** A digital input/output, used for setting or reading a bi-directional pin
<> 149:156823d33999 29 *
AnnaBridge 167:e84263d55307 30 * @note Synchronization level: Interrupt safe
AnnaBridge 167:e84263d55307 31 * @ingroup drivers
<> 149:156823d33999 32 */
<> 149:156823d33999 33 class DigitalInOut {
<> 149:156823d33999 34
<> 149:156823d33999 35 public:
<> 149:156823d33999 36 /** Create a DigitalInOut connected to the specified pin
<> 149:156823d33999 37 *
<> 149:156823d33999 38 * @param pin DigitalInOut pin to connect to
<> 149:156823d33999 39 */
AnnaBridge 187:0387e8f68319 40 DigitalInOut(PinName pin) : gpio()
AnnaBridge 187:0387e8f68319 41 {
<> 149:156823d33999 42 // No lock needed in the constructor
<> 149:156823d33999 43 gpio_init_in(&gpio, pin);
<> 149:156823d33999 44 }
<> 149:156823d33999 45
<> 149:156823d33999 46 /** Create a DigitalInOut connected to the specified pin
<> 149:156823d33999 47 *
<> 149:156823d33999 48 * @param pin DigitalInOut pin to connect to
<> 149:156823d33999 49 * @param direction the initial direction of the pin
<> 149:156823d33999 50 * @param mode the initial mode of the pin
<> 149:156823d33999 51 * @param value the initial value of the pin if is an output
<> 149:156823d33999 52 */
AnnaBridge 187:0387e8f68319 53 DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
AnnaBridge 187:0387e8f68319 54 {
<> 149:156823d33999 55 // No lock needed in the constructor
<> 149:156823d33999 56 gpio_init_inout(&gpio, pin, direction, mode, value);
<> 149:156823d33999 57 }
<> 149:156823d33999 58
<> 149:156823d33999 59 /** Set the output, specified as 0 or 1 (int)
<> 149:156823d33999 60 *
<> 149:156823d33999 61 * @param value An integer specifying the pin output value,
<> 149:156823d33999 62 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
<> 149:156823d33999 63 */
AnnaBridge 187:0387e8f68319 64 void write(int value)
AnnaBridge 187:0387e8f68319 65 {
<> 149:156823d33999 66 // Thread safe / atomic HAL call
<> 149:156823d33999 67 gpio_write(&gpio, value);
<> 149:156823d33999 68 }
<> 149:156823d33999 69
<> 149:156823d33999 70 /** Return the output setting, represented as 0 or 1 (int)
<> 149:156823d33999 71 *
<> 149:156823d33999 72 * @returns
<> 149:156823d33999 73 * an integer representing the output setting of the pin if it is an output,
<> 149:156823d33999 74 * or read the input if set as an input
<> 149:156823d33999 75 */
AnnaBridge 187:0387e8f68319 76 int read()
AnnaBridge 187:0387e8f68319 77 {
<> 149:156823d33999 78 // Thread safe / atomic HAL call
<> 149:156823d33999 79 return gpio_read(&gpio);
<> 149:156823d33999 80 }
<> 149:156823d33999 81
<> 149:156823d33999 82 /** Set as an output
<> 149:156823d33999 83 */
AnnaBridge 187:0387e8f68319 84 void output()
AnnaBridge 187:0387e8f68319 85 {
<> 149:156823d33999 86 core_util_critical_section_enter();
<> 149:156823d33999 87 gpio_dir(&gpio, PIN_OUTPUT);
<> 149:156823d33999 88 core_util_critical_section_exit();
<> 149:156823d33999 89 }
<> 149:156823d33999 90
<> 149:156823d33999 91 /** Set as an input
<> 149:156823d33999 92 */
AnnaBridge 187:0387e8f68319 93 void input()
AnnaBridge 187:0387e8f68319 94 {
<> 149:156823d33999 95 core_util_critical_section_enter();
<> 149:156823d33999 96 gpio_dir(&gpio, PIN_INPUT);
<> 149:156823d33999 97 core_util_critical_section_exit();
<> 149:156823d33999 98 }
<> 149:156823d33999 99
<> 149:156823d33999 100 /** Set the input pin mode
<> 149:156823d33999 101 *
AnnaBridge 167:e84263d55307 102 * @param pull PullUp, PullDown, PullNone, OpenDrain
<> 149:156823d33999 103 */
AnnaBridge 187:0387e8f68319 104 void mode(PinMode pull)
AnnaBridge 187:0387e8f68319 105 {
<> 149:156823d33999 106 core_util_critical_section_enter();
<> 149:156823d33999 107 gpio_mode(&gpio, pull);
<> 149:156823d33999 108 core_util_critical_section_exit();
<> 149:156823d33999 109 }
<> 149:156823d33999 110
<> 149:156823d33999 111 /** Return the output setting, represented as 0 or 1 (int)
<> 149:156823d33999 112 *
<> 149:156823d33999 113 * @returns
<> 149:156823d33999 114 * Non zero value if pin is connected to uc GPIO
<> 149:156823d33999 115 * 0 if gpio object was initialized with NC
<> 149:156823d33999 116 */
AnnaBridge 187:0387e8f68319 117 int is_connected()
AnnaBridge 187:0387e8f68319 118 {
<> 149:156823d33999 119 // Thread safe / atomic HAL call
<> 149:156823d33999 120 return gpio_is_connected(&gpio);
<> 149:156823d33999 121 }
<> 149:156823d33999 122
<> 149:156823d33999 123 /** A shorthand for write()
AnnaBridge 167:e84263d55307 124 * \sa DigitalInOut::write()
AnnaBridge 188:bcfe06ba3d64 125 * @code
AnnaBridge 188:bcfe06ba3d64 126 * DigitalInOut inout(PIN);
AnnaBridge 188:bcfe06ba3d64 127 * DigitalIn button(BUTTON1);
AnnaBridge 188:bcfe06ba3d64 128 * inout.output();
AnnaBridge 188:bcfe06ba3d64 129 *
AnnaBridge 188:bcfe06ba3d64 130 * inout = button; // Equivalent to inout.write(button.read())
AnnaBridge 188:bcfe06ba3d64 131 * @endcode
<> 149:156823d33999 132 */
AnnaBridge 187:0387e8f68319 133 DigitalInOut &operator= (int value)
AnnaBridge 187:0387e8f68319 134 {
<> 149:156823d33999 135 // Underlying write is thread safe
<> 149:156823d33999 136 write(value);
<> 149:156823d33999 137 return *this;
<> 149:156823d33999 138 }
<> 149:156823d33999 139
AnnaBridge 188:bcfe06ba3d64 140 /**A shorthand for write() using the assignment operator which copies the
AnnaBridge 188:bcfe06ba3d64 141 * state from the DigitalInOut argument.
AnnaBridge 167:e84263d55307 142 * \sa DigitalInOut::write()
AnnaBridge 167:e84263d55307 143 */
AnnaBridge 187:0387e8f68319 144 DigitalInOut &operator= (DigitalInOut &rhs)
AnnaBridge 187:0387e8f68319 145 {
<> 149:156823d33999 146 core_util_critical_section_enter();
<> 149:156823d33999 147 write(rhs.read());
<> 149:156823d33999 148 core_util_critical_section_exit();
<> 149:156823d33999 149 return *this;
<> 149:156823d33999 150 }
<> 149:156823d33999 151
<> 149:156823d33999 152 /** A shorthand for read()
AnnaBridge 167:e84263d55307 153 * \sa DigitalInOut::read()
AnnaBridge 188:bcfe06ba3d64 154 * @code
AnnaBridge 188:bcfe06ba3d64 155 * DigitalInOut inout(PIN);
AnnaBridge 188:bcfe06ba3d64 156 * DigitalOut led(LED1);
AnnaBridge 188:bcfe06ba3d64 157 *
AnnaBridge 188:bcfe06ba3d64 158 * inout.input();
AnnaBridge 188:bcfe06ba3d64 159 * led = inout; // Equivalent to led.write(inout.read())
AnnaBridge 188:bcfe06ba3d64 160 * @endcode
<> 149:156823d33999 161 */
AnnaBridge 187:0387e8f68319 162 operator int()
AnnaBridge 187:0387e8f68319 163 {
<> 149:156823d33999 164 // Underlying call is thread safe
<> 149:156823d33999 165 return read();
<> 149:156823d33999 166 }
<> 149:156823d33999 167
<> 149:156823d33999 168 protected:
AnnaBridge 189:f392fc9709a3 169 #if !defined(DOXYGEN_ONLY)
<> 149:156823d33999 170 gpio_t gpio;
AnnaBridge 189:f392fc9709a3 171 #endif //!defined(DOXYGEN_ONLY)
<> 149:156823d33999 172 };
<> 149:156823d33999 173
<> 149:156823d33999 174 } // namespace mbed
<> 149:156823d33999 175
<> 149:156823d33999 176 #endif