mbed library sources. Supersedes mbed-src.

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DigitalInOut.h Source File

DigitalInOut.h

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #ifndef MBED_DIGITALINOUT_H
00018 #define MBED_DIGITALINOUT_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #include "hal/gpio_api.h"
00023 #include "platform/mbed_critical.h"
00024 
00025 namespace mbed {
00026 /** \addtogroup drivers */
00027 
00028 /** A digital input/output, used for setting or reading a bi-directional pin
00029  *
00030  * @note Synchronization level: Interrupt safe
00031  * @ingroup drivers
00032  */
00033 class DigitalInOut {
00034 
00035 public:
00036     /** Create a DigitalInOut connected to the specified pin
00037      *
00038      *  @param pin DigitalInOut pin to connect to
00039      */
00040     DigitalInOut(PinName pin) : gpio()
00041     {
00042         // No lock needed in the constructor
00043         gpio_init_in(&gpio, pin);
00044     }
00045 
00046     /** Create a DigitalInOut connected to the specified pin
00047      *
00048      *  @param pin DigitalInOut pin to connect to
00049      *  @param direction the initial direction of the pin
00050      *  @param mode the initial mode of the pin
00051      *  @param value the initial value of the pin if is an output
00052      */
00053     DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio()
00054     {
00055         // No lock needed in the constructor
00056         gpio_init_inout(&gpio, pin, direction, mode, value);
00057     }
00058 
00059     /** Set the output, specified as 0 or 1 (int)
00060      *
00061      *  @param value An integer specifying the pin output value,
00062      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
00063      */
00064     void write(int value)
00065     {
00066         // Thread safe / atomic HAL call
00067         gpio_write(&gpio, value);
00068     }
00069 
00070     /** Return the output setting, represented as 0 or 1 (int)
00071      *
00072      *  @returns
00073      *    an integer representing the output setting of the pin if it is an output,
00074      *    or read the input if set as an input
00075      */
00076     int read()
00077     {
00078         // Thread safe / atomic HAL call
00079         return gpio_read(&gpio);
00080     }
00081 
00082     /** Set as an output
00083      */
00084     void output()
00085     {
00086         core_util_critical_section_enter();
00087         gpio_dir(&gpio, PIN_OUTPUT);
00088         core_util_critical_section_exit();
00089     }
00090 
00091     /** Set as an input
00092      */
00093     void input()
00094     {
00095         core_util_critical_section_enter();
00096         gpio_dir(&gpio, PIN_INPUT);
00097         core_util_critical_section_exit();
00098     }
00099 
00100     /** Set the input pin mode
00101      *
00102      *  @param pull PullUp, PullDown, PullNone, OpenDrain
00103      */
00104     void mode(PinMode pull)
00105     {
00106         core_util_critical_section_enter();
00107         gpio_mode(&gpio, pull);
00108         core_util_critical_section_exit();
00109     }
00110 
00111     /** Return the output setting, represented as 0 or 1 (int)
00112      *
00113      *  @returns
00114      *    Non zero value if pin is connected to uc GPIO
00115      *    0 if gpio object was initialized with NC
00116      */
00117     int is_connected()
00118     {
00119         // Thread safe / atomic HAL call
00120         return gpio_is_connected(&gpio);
00121     }
00122 
00123     /** A shorthand for write()
00124      * \sa DigitalInOut::write()
00125      * @code
00126      *      DigitalInOut  inout(PIN);
00127      *      DigitalIn     button(BUTTON1);
00128      *      inout.output();
00129      *
00130      *      inout = button;     // Equivalent to inout.write(button.read())
00131      * @endcode
00132      */
00133     DigitalInOut &operator= (int value)
00134     {
00135         // Underlying write is thread safe
00136         write(value);
00137         return *this;
00138     }
00139 
00140     /**A shorthand for write() using the assignment operator which copies the
00141      * state from the DigitalInOut argument.
00142      * \sa DigitalInOut::write()
00143      */
00144     DigitalInOut &operator= (DigitalInOut &rhs)
00145     {
00146         core_util_critical_section_enter();
00147         write(rhs.read());
00148         core_util_critical_section_exit();
00149         return *this;
00150     }
00151 
00152     /** A shorthand for read()
00153      * \sa DigitalInOut::read()
00154      * @code
00155      *      DigitalInOut inout(PIN);
00156      *      DigitalOut led(LED1);
00157      *
00158      *      inout.input();
00159      *      led = inout;   // Equivalent to led.write(inout.read())
00160      * @endcode
00161      */
00162     operator int()
00163     {
00164         // Underlying call is thread safe
00165         return read();
00166     }
00167 
00168 protected:
00169 #if !defined(DOXYGEN_ONLY)
00170     gpio_t gpio;
00171 #endif //!defined(DOXYGEN_ONLY)
00172 };
00173 
00174 } // namespace mbed
00175 
00176 #endif