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