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

PortOut.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_PORTOUT_H
00018 #define MBED_PORTOUT_H
00019 
00020 #include "platform/platform.h"
00021 
00022 #if DEVICE_PORTOUT || defined(DOXYGEN_ONLY)
00023 
00024 #include "hal/port_api.h"
00025 #include "platform/mbed_critical.h"
00026 
00027 namespace mbed {
00028 /** \addtogroup drivers */
00029 /** A multiple pin digital output
00030  *
00031  * @note Synchronization level: Interrupt safe
00032  *
00033  * Example:
00034  * @code
00035  * // Toggle all four LEDs
00036  *
00037  * #include "mbed.h"
00038  *
00039  * // LED1 = P1.18  LED2 = P1.20  LED3 = P1.21  LED4 = P1.23
00040  * #define LED_MASK 0x00B40000
00041  *
00042  * PortOut ledport(Port1, LED_MASK);
00043  *
00044  * int main() {
00045  *     while(1) {
00046  *         ledport = LED_MASK;
00047  *         wait(1);
00048  *         ledport = 0;
00049  *         wait(1);
00050  *     }
00051  * }
00052  * @endcode
00053  * @ingroup drivers
00054  */
00055 class PortOut {
00056 public:
00057 
00058     /** Create a PortOut, connected to the specified port
00059      *
00060      *  @param port Port to connect to (as defined in target's PortNames.h)
00061      *  @param mask Bitmask defines which port pins are an output (0 - ignore, 1 - include)
00062      */
00063     PortOut(PortName port, int mask = 0xFFFFFFFF)
00064     {
00065         core_util_critical_section_enter();
00066         port_init(&_port, port, mask, PIN_OUTPUT);
00067         core_util_critical_section_exit();
00068     }
00069 
00070     /** Write the value to the output port
00071      *
00072      *  @param value An integer specifying a bit to write for every corresponding PortOut pin
00073      */
00074     void write(int value)
00075     {
00076         port_write(&_port, value);
00077     }
00078 
00079     /** Read the value currently output on the port
00080      *
00081      *  @returns
00082      *    An integer with each bit corresponding to associated pin value
00083      */
00084     int read()
00085     {
00086         return port_read(&_port);
00087     }
00088 
00089     /** A shorthand for write()
00090      * \sa PortOut::write()
00091      */
00092     PortOut &operator= (int value)
00093     {
00094         write(value);
00095         return *this;
00096     }
00097 
00098     /** A shorthand for read()
00099      * \sa PortOut::read()
00100      */
00101     PortOut &operator= (PortOut &rhs)
00102     {
00103         write(rhs.read());
00104         return *this;
00105     }
00106 
00107     /** A shorthand for read()
00108      * \sa PortOut::read()
00109      */
00110     operator int()
00111     {
00112         return read();
00113     }
00114 
00115 private:
00116     port_t _port;
00117 };
00118 
00119 } // namespace mbed
00120 
00121 #endif
00122 
00123 #endif