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