Rtos API example

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