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