001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ganlikun 0:13413ea9a877 1 /* mbed Microcontroller Library
ganlikun 0:13413ea9a877 2 * Copyright (c) 2006-2013 ARM Limited
ganlikun 0:13413ea9a877 3 *
ganlikun 0:13413ea9a877 4 * Licensed under the Apache License, Version 2.0 (the "License");
ganlikun 0:13413ea9a877 5 * you may not use this file except in compliance with the License.
ganlikun 0:13413ea9a877 6 * You may obtain a copy of the License at
ganlikun 0:13413ea9a877 7 *
ganlikun 0:13413ea9a877 8 * http://www.apache.org/licenses/LICENSE-2.0
ganlikun 0:13413ea9a877 9 *
ganlikun 0:13413ea9a877 10 * Unless required by applicable law or agreed to in writing, software
ganlikun 0:13413ea9a877 11 * distributed under the License is distributed on an "AS IS" BASIS,
ganlikun 0:13413ea9a877 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ganlikun 0:13413ea9a877 13 * See the License for the specific language governing permissions and
ganlikun 0:13413ea9a877 14 * limitations under the License.
ganlikun 0:13413ea9a877 15 */
ganlikun 0:13413ea9a877 16 #ifndef MBED_PORTOUT_H
ganlikun 0:13413ea9a877 17 #define MBED_PORTOUT_H
ganlikun 0:13413ea9a877 18
ganlikun 0:13413ea9a877 19 #include "platform/platform.h"
ganlikun 0:13413ea9a877 20
ganlikun 0:13413ea9a877 21 #if defined (DEVICE_PORTOUT) || defined(DOXYGEN_ONLY)
ganlikun 0:13413ea9a877 22
ganlikun 0:13413ea9a877 23 #include "hal/port_api.h"
ganlikun 0:13413ea9a877 24 #include "platform/mbed_critical.h"
ganlikun 0:13413ea9a877 25
ganlikun 0:13413ea9a877 26 namespace mbed {
ganlikun 0:13413ea9a877 27 /** \addtogroup drivers */
ganlikun 0:13413ea9a877 28 /** A multiple pin digital out
ganlikun 0:13413ea9a877 29 *
ganlikun 0:13413ea9a877 30 * @note Synchronization level: Interrupt safe
ganlikun 0:13413ea9a877 31 *
ganlikun 0:13413ea9a877 32 * Example:
ganlikun 0:13413ea9a877 33 * @code
ganlikun 0:13413ea9a877 34 * // Toggle all four LEDs
ganlikun 0:13413ea9a877 35 *
ganlikun 0:13413ea9a877 36 * #include "mbed.h"
ganlikun 0:13413ea9a877 37 *
ganlikun 0:13413ea9a877 38 * // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23
ganlikun 0:13413ea9a877 39 * #define LED_MASK 0x00B40000
ganlikun 0:13413ea9a877 40 *
ganlikun 0:13413ea9a877 41 * PortOut ledport(Port1, LED_MASK);
ganlikun 0:13413ea9a877 42 *
ganlikun 0:13413ea9a877 43 * int main() {
ganlikun 0:13413ea9a877 44 * while(1) {
ganlikun 0:13413ea9a877 45 * ledport = LED_MASK;
ganlikun 0:13413ea9a877 46 * wait(1);
ganlikun 0:13413ea9a877 47 * ledport = 0;
ganlikun 0:13413ea9a877 48 * wait(1);
ganlikun 0:13413ea9a877 49 * }
ganlikun 0:13413ea9a877 50 * }
ganlikun 0:13413ea9a877 51 * @endcode
ganlikun 0:13413ea9a877 52 * @ingroup drivers
ganlikun 0:13413ea9a877 53 */
ganlikun 0:13413ea9a877 54 class PortOut {
ganlikun 0:13413ea9a877 55 public:
ganlikun 0:13413ea9a877 56
ganlikun 0:13413ea9a877 57 /** Create an PortOut, connected to the specified port
ganlikun 0:13413ea9a877 58 *
ganlikun 0:13413ea9a877 59 * @param port Port to connect to (Port0-Port5)
ganlikun 0:13413ea9a877 60 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
ganlikun 0:13413ea9a877 61 */
ganlikun 0:13413ea9a877 62 PortOut(PortName port, int mask = 0xFFFFFFFF) {
ganlikun 0:13413ea9a877 63 core_util_critical_section_enter();
ganlikun 0:13413ea9a877 64 port_init(&_port, port, mask, PIN_OUTPUT);
ganlikun 0:13413ea9a877 65 core_util_critical_section_exit();
ganlikun 0:13413ea9a877 66 }
ganlikun 0:13413ea9a877 67
ganlikun 0:13413ea9a877 68 /** Write the value to the output port
ganlikun 0:13413ea9a877 69 *
ganlikun 0:13413ea9a877 70 * @param value An integer specifying a bit to write for every corresponding PortOut pin
ganlikun 0:13413ea9a877 71 */
ganlikun 0:13413ea9a877 72 void write(int value) {
ganlikun 0:13413ea9a877 73 port_write(&_port, value);
ganlikun 0:13413ea9a877 74 }
ganlikun 0:13413ea9a877 75
ganlikun 0:13413ea9a877 76 /** Read the value currently output on the port
ganlikun 0:13413ea9a877 77 *
ganlikun 0:13413ea9a877 78 * @returns
ganlikun 0:13413ea9a877 79 * An integer with each bit corresponding to associated PortOut pin setting
ganlikun 0:13413ea9a877 80 */
ganlikun 0:13413ea9a877 81 int read() {
ganlikun 0:13413ea9a877 82 return port_read(&_port);
ganlikun 0:13413ea9a877 83 }
ganlikun 0:13413ea9a877 84
ganlikun 0:13413ea9a877 85 /** A shorthand for write()
ganlikun 0:13413ea9a877 86 * \sa PortOut::write()
ganlikun 0:13413ea9a877 87 */
ganlikun 0:13413ea9a877 88 PortOut& operator= (int value) {
ganlikun 0:13413ea9a877 89 write(value);
ganlikun 0:13413ea9a877 90 return *this;
ganlikun 0:13413ea9a877 91 }
ganlikun 0:13413ea9a877 92
ganlikun 0:13413ea9a877 93 /** A shorthand for read()
ganlikun 0:13413ea9a877 94 * \sa PortOut::read()
ganlikun 0:13413ea9a877 95 */
ganlikun 0:13413ea9a877 96 PortOut& operator= (PortOut& rhs) {
ganlikun 0:13413ea9a877 97 write(rhs.read());
ganlikun 0:13413ea9a877 98 return *this;
ganlikun 0:13413ea9a877 99 }
ganlikun 0:13413ea9a877 100
ganlikun 0:13413ea9a877 101 /** A shorthand for read()
ganlikun 0:13413ea9a877 102 * \sa PortOut::read()
ganlikun 0:13413ea9a877 103 */
ganlikun 0:13413ea9a877 104 operator int() {
ganlikun 0:13413ea9a877 105 return read();
ganlikun 0:13413ea9a877 106 }
ganlikun 0:13413ea9a877 107
ganlikun 0:13413ea9a877 108 private:
ganlikun 0:13413ea9a877 109 port_t _port;
ganlikun 0:13413ea9a877 110 };
ganlikun 0:13413ea9a877 111
ganlikun 0:13413ea9a877 112 } // namespace mbed
ganlikun 0:13413ea9a877 113
ganlikun 0:13413ea9a877 114 #endif
ganlikun 0:13413ea9a877 115
ganlikun 0:13413ea9a877 116 #endif