takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers PortInOut.h Source File

PortInOut.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_PORTINOUT_H
00017 #define MBED_PORTINOUT_H
00018 
00019 #include "platform/platform.h"
00020 
00021 #if defined (DEVICE_PORTINOUT) || defined(DOXYGEN_ONLY)
00022 
00023 #include "hal/port_api.h"
00024 #include "platform/mbed_critical.h"
00025 
00026 namespace mbed {
00027 /** \addtogroup drivers */
00028 
00029 /** A multiple pin digital in/out used to set/read multiple bi-directional pins
00030  *
00031  * @note Synchronization level: Interrupt safe
00032  * @ingroup drivers
00033  */
00034 class PortInOut {
00035 public:
00036 
00037     /** Create an PortInOut, connected to the specified port
00038      *
00039      *  @param port Port to connect to (Port0-Port5)
00040      *  @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
00041      */
00042     PortInOut(PortName port, int mask = 0xFFFFFFFF)
00043     {
00044         core_util_critical_section_enter();
00045         port_init(&_port, port, mask, PIN_INPUT);
00046         core_util_critical_section_exit();
00047     }
00048 
00049     /** Write the value to the output port
00050      *
00051      *  @param value An integer specifying a bit to write for every corresponding port pin
00052      */
00053     void write(int value)
00054     {
00055         port_write(&_port, value);
00056     }
00057 
00058     /** Read the value currently output on the port
00059      *
00060      *  @returns
00061      *    An integer with each bit corresponding to associated port pin setting
00062      */
00063     int read()
00064     {
00065         return port_read(&_port);
00066     }
00067 
00068     /** Set as an output
00069      */
00070     void output()
00071     {
00072         core_util_critical_section_enter();
00073         port_dir(&_port, PIN_OUTPUT);
00074         core_util_critical_section_exit();
00075     }
00076 
00077     /** Set as an input
00078      */
00079     void input()
00080     {
00081         core_util_critical_section_enter();
00082         port_dir(&_port, PIN_INPUT);
00083         core_util_critical_section_exit();
00084     }
00085 
00086     /** Set the input pin mode
00087      *
00088      *  @param mode PullUp, PullDown, PullNone, OpenDrain
00089      */
00090     void mode(PinMode mode)
00091     {
00092         core_util_critical_section_enter();
00093         port_mode(&_port, mode);
00094         core_util_critical_section_exit();
00095     }
00096 
00097     /** A shorthand for write()
00098      * \sa PortInOut::write()
00099      */
00100     PortInOut &operator= (int value)
00101     {
00102         write(value);
00103         return *this;
00104     }
00105 
00106     /** A shorthand for write()
00107      * \sa PortInOut::write()
00108      */
00109     PortInOut &operator= (PortInOut &rhs)
00110     {
00111         write(rhs.read());
00112         return *this;
00113     }
00114 
00115     /** A shorthand for read()
00116      * \sa PortInOut::read()
00117      */
00118     operator int()
00119     {
00120         return read();
00121     }
00122 
00123 private:
00124     port_t _port;
00125 };
00126 
00127 } // namespace mbed
00128 
00129 #endif
00130 
00131 #endif