1232

Committer:
ganlikun
Date:
Mon Oct 24 15:19:39 2022 +0000
Revision:
0:06036f8bee2d
11

Who changed what in which revision?

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