Rtos API example

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         // No lock needed in the constructor
00041         gpio_init_in(&gpio, pin);
00042     }
00043 
00044     /** Create a DigitalInOut connected to the specified pin
00045      *
00046      *  @param pin DigitalInOut pin to connect to
00047      *  @param direction the initial direction of the pin
00048      *  @param mode the initial mode of the pin
00049      *  @param value the initial value of the pin if is an output
00050      */
00051     DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() {
00052         // No lock needed in the constructor
00053         gpio_init_inout(&gpio, pin, direction, mode, value);
00054     }
00055 
00056     /** Set the output, specified as 0 or 1 (int)
00057      *
00058      *  @param value An integer specifying the pin output value,
00059      *      0 for logical 0, 1 (or any other non-zero value) for logical 1
00060      */
00061     void write(int value) {
00062         // Thread safe / atomic HAL call
00063         gpio_write(&gpio, value);
00064     }
00065 
00066     /** Return the output setting, represented as 0 or 1 (int)
00067      *
00068      *  @returns
00069      *    an integer representing the output setting of the pin if it is an output,
00070      *    or read the input if set as an input
00071      */
00072     int read() {
00073         // Thread safe / atomic HAL call
00074         return gpio_read(&gpio);
00075     }
00076 
00077     /** Set as an output
00078      */
00079     void output() {
00080         core_util_critical_section_enter();
00081         gpio_dir(&gpio, PIN_OUTPUT);
00082         core_util_critical_section_exit();
00083     }
00084 
00085     /** Set as an input
00086      */
00087     void input() {
00088         core_util_critical_section_enter();
00089         gpio_dir(&gpio, PIN_INPUT);
00090         core_util_critical_section_exit();
00091     }
00092 
00093     /** Set the input pin mode
00094      *
00095      *  @param pull PullUp, PullDown, PullNone, OpenDrain
00096      */
00097     void mode(PinMode pull) {
00098         core_util_critical_section_enter();
00099         gpio_mode(&gpio, pull);
00100         core_util_critical_section_exit();
00101     }
00102 
00103     /** Return the output setting, represented as 0 or 1 (int)
00104      *
00105      *  @returns
00106      *    Non zero value if pin is connected to uc GPIO
00107      *    0 if gpio object was initialized with NC
00108      */
00109     int is_connected() {
00110         // Thread safe / atomic HAL call
00111         return gpio_is_connected(&gpio);
00112     }
00113 
00114     /** A shorthand for write()
00115      * \sa DigitalInOut::write()
00116      */
00117     DigitalInOut& operator= (int value) {
00118         // Underlying write is thread safe
00119         write(value);
00120         return *this;
00121     }
00122 
00123     /** A shorthand for write()
00124      * \sa DigitalInOut::write()
00125      */
00126     DigitalInOut& operator= (DigitalInOut& rhs) {
00127         core_util_critical_section_enter();
00128         write(rhs.read());
00129         core_util_critical_section_exit();
00130         return *this;
00131     }
00132 
00133     /** A shorthand for read()
00134      * \sa DigitalInOut::read()
00135      */
00136     operator int() {
00137         // Underlying call is thread safe
00138         return read();
00139     }
00140 
00141 protected:
00142     gpio_t gpio;
00143 };
00144 
00145 } // namespace mbed
00146 
00147 #endif