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 PortInOut.h Source File

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