USB Serial application
Fork of USBSerial_HelloWorld by
mbed-dev/drivers/PortInOut.h@11:b3f2a8bdac4d, 2017-12-16 (annotated)
- Committer:
- Zaitsev
- Date:
- Sat Dec 16 10:26:48 2017 +0000
- Revision:
- 11:b3f2a8bdac4d
- Parent:
- 10:41552d038a69
A copy for D.S;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Zaitsev | 10:41552d038a69 | 1 | /* mbed Microcontroller Library |
Zaitsev | 10:41552d038a69 | 2 | * Copyright (c) 2006-2013 ARM Limited |
Zaitsev | 10:41552d038a69 | 3 | * |
Zaitsev | 10:41552d038a69 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Zaitsev | 10:41552d038a69 | 5 | * you may not use this file except in compliance with the License. |
Zaitsev | 10:41552d038a69 | 6 | * You may obtain a copy of the License at |
Zaitsev | 10:41552d038a69 | 7 | * |
Zaitsev | 10:41552d038a69 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Zaitsev | 10:41552d038a69 | 9 | * |
Zaitsev | 10:41552d038a69 | 10 | * Unless required by applicable law or agreed to in writing, software |
Zaitsev | 10:41552d038a69 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Zaitsev | 10:41552d038a69 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Zaitsev | 10:41552d038a69 | 13 | * See the License for the specific language governing permissions and |
Zaitsev | 10:41552d038a69 | 14 | * limitations under the License. |
Zaitsev | 10:41552d038a69 | 15 | */ |
Zaitsev | 10:41552d038a69 | 16 | #ifndef MBED_PORTINOUT_H |
Zaitsev | 10:41552d038a69 | 17 | #define MBED_PORTINOUT_H |
Zaitsev | 10:41552d038a69 | 18 | |
Zaitsev | 10:41552d038a69 | 19 | #include "platform/platform.h" |
Zaitsev | 10:41552d038a69 | 20 | |
Zaitsev | 10:41552d038a69 | 21 | #if DEVICE_PORTINOUT |
Zaitsev | 10:41552d038a69 | 22 | |
Zaitsev | 10:41552d038a69 | 23 | #include "hal/port_api.h" |
Zaitsev | 10:41552d038a69 | 24 | #include "platform/critical.h" |
Zaitsev | 10:41552d038a69 | 25 | |
Zaitsev | 10:41552d038a69 | 26 | namespace mbed { |
Zaitsev | 10:41552d038a69 | 27 | /** \addtogroup drivers */ |
Zaitsev | 10:41552d038a69 | 28 | /** @{*/ |
Zaitsev | 10:41552d038a69 | 29 | |
Zaitsev | 10:41552d038a69 | 30 | /** A multiple pin digital in/out used to set/read multiple bi-directional pins |
Zaitsev | 10:41552d038a69 | 31 | * |
Zaitsev | 10:41552d038a69 | 32 | * @Note Synchronization level: Interrupt safe |
Zaitsev | 10:41552d038a69 | 33 | */ |
Zaitsev | 10:41552d038a69 | 34 | class PortInOut { |
Zaitsev | 10:41552d038a69 | 35 | public: |
Zaitsev | 10:41552d038a69 | 36 | |
Zaitsev | 10:41552d038a69 | 37 | /** Create an PortInOut, connected to the specified port |
Zaitsev | 10:41552d038a69 | 38 | * |
Zaitsev | 10:41552d038a69 | 39 | * @param port Port to connect to (Port0-Port5) |
Zaitsev | 10:41552d038a69 | 40 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
Zaitsev | 10:41552d038a69 | 41 | */ |
Zaitsev | 10:41552d038a69 | 42 | PortInOut(PortName port, int mask = 0xFFFFFFFF) { |
Zaitsev | 10:41552d038a69 | 43 | core_util_critical_section_enter(); |
Zaitsev | 10:41552d038a69 | 44 | port_init(&_port, port, mask, PIN_INPUT); |
Zaitsev | 10:41552d038a69 | 45 | core_util_critical_section_exit(); |
Zaitsev | 10:41552d038a69 | 46 | } |
Zaitsev | 10:41552d038a69 | 47 | |
Zaitsev | 10:41552d038a69 | 48 | /** Write the value to the output port |
Zaitsev | 10:41552d038a69 | 49 | * |
Zaitsev | 10:41552d038a69 | 50 | * @param value An integer specifying a bit to write for every corresponding port pin |
Zaitsev | 10:41552d038a69 | 51 | */ |
Zaitsev | 10:41552d038a69 | 52 | void write(int value) { |
Zaitsev | 10:41552d038a69 | 53 | port_write(&_port, value); |
Zaitsev | 10:41552d038a69 | 54 | } |
Zaitsev | 10:41552d038a69 | 55 | |
Zaitsev | 10:41552d038a69 | 56 | /** Read the value currently output on the port |
Zaitsev | 10:41552d038a69 | 57 | * |
Zaitsev | 10:41552d038a69 | 58 | * @returns |
Zaitsev | 10:41552d038a69 | 59 | * An integer with each bit corresponding to associated port pin setting |
Zaitsev | 10:41552d038a69 | 60 | */ |
Zaitsev | 10:41552d038a69 | 61 | int read() { |
Zaitsev | 10:41552d038a69 | 62 | return port_read(&_port); |
Zaitsev | 10:41552d038a69 | 63 | } |
Zaitsev | 10:41552d038a69 | 64 | |
Zaitsev | 10:41552d038a69 | 65 | /** Set as an output |
Zaitsev | 10:41552d038a69 | 66 | */ |
Zaitsev | 10:41552d038a69 | 67 | void output() { |
Zaitsev | 10:41552d038a69 | 68 | core_util_critical_section_enter(); |
Zaitsev | 10:41552d038a69 | 69 | port_dir(&_port, PIN_OUTPUT); |
Zaitsev | 10:41552d038a69 | 70 | core_util_critical_section_exit(); |
Zaitsev | 10:41552d038a69 | 71 | } |
Zaitsev | 10:41552d038a69 | 72 | |
Zaitsev | 10:41552d038a69 | 73 | /** Set as an input |
Zaitsev | 10:41552d038a69 | 74 | */ |
Zaitsev | 10:41552d038a69 | 75 | void input() { |
Zaitsev | 10:41552d038a69 | 76 | core_util_critical_section_enter(); |
Zaitsev | 10:41552d038a69 | 77 | port_dir(&_port, PIN_INPUT); |
Zaitsev | 10:41552d038a69 | 78 | core_util_critical_section_exit(); |
Zaitsev | 10:41552d038a69 | 79 | } |
Zaitsev | 10:41552d038a69 | 80 | |
Zaitsev | 10:41552d038a69 | 81 | /** Set the input pin mode |
Zaitsev | 10:41552d038a69 | 82 | * |
Zaitsev | 10:41552d038a69 | 83 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
Zaitsev | 10:41552d038a69 | 84 | */ |
Zaitsev | 10:41552d038a69 | 85 | void mode(PinMode mode) { |
Zaitsev | 10:41552d038a69 | 86 | core_util_critical_section_enter(); |
Zaitsev | 10:41552d038a69 | 87 | port_mode(&_port, mode); |
Zaitsev | 10:41552d038a69 | 88 | core_util_critical_section_exit(); |
Zaitsev | 10:41552d038a69 | 89 | } |
Zaitsev | 10:41552d038a69 | 90 | |
Zaitsev | 10:41552d038a69 | 91 | /** A shorthand for write() |
Zaitsev | 10:41552d038a69 | 92 | */ |
Zaitsev | 10:41552d038a69 | 93 | PortInOut& operator= (int value) { |
Zaitsev | 10:41552d038a69 | 94 | write(value); |
Zaitsev | 10:41552d038a69 | 95 | return *this; |
Zaitsev | 10:41552d038a69 | 96 | } |
Zaitsev | 10:41552d038a69 | 97 | |
Zaitsev | 10:41552d038a69 | 98 | PortInOut& operator= (PortInOut& rhs) { |
Zaitsev | 10:41552d038a69 | 99 | write(rhs.read()); |
Zaitsev | 10:41552d038a69 | 100 | return *this; |
Zaitsev | 10:41552d038a69 | 101 | } |
Zaitsev | 10:41552d038a69 | 102 | |
Zaitsev | 10:41552d038a69 | 103 | /** A shorthand for read() |
Zaitsev | 10:41552d038a69 | 104 | */ |
Zaitsev | 10:41552d038a69 | 105 | operator int() { |
Zaitsev | 10:41552d038a69 | 106 | return read(); |
Zaitsev | 10:41552d038a69 | 107 | } |
Zaitsev | 10:41552d038a69 | 108 | |
Zaitsev | 10:41552d038a69 | 109 | private: |
Zaitsev | 10:41552d038a69 | 110 | port_t _port; |
Zaitsev | 10:41552d038a69 | 111 | }; |
Zaitsev | 10:41552d038a69 | 112 | |
Zaitsev | 10:41552d038a69 | 113 | } // namespace mbed |
Zaitsev | 10:41552d038a69 | 114 | |
Zaitsev | 10:41552d038a69 | 115 | #endif |
Zaitsev | 10:41552d038a69 | 116 | |
Zaitsev | 10:41552d038a69 | 117 | #endif |
Zaitsev | 10:41552d038a69 | 118 | |
Zaitsev | 10:41552d038a69 | 119 | /** @}*/ |