takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

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