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