Pinned to some recent date
drivers/PortIn.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_PORTIN_H |
Simon Cooksey |
0:fb7af294d5d9 | 17 | #define MBED_PORTIN_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_PORTIN |
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 input |
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 | * Example: |
Simon Cooksey |
0:fb7af294d5d9 | 35 | * @code |
Simon Cooksey |
0:fb7af294d5d9 | 36 | * // Switch on an LED if any of mbed pins 21-26 is high |
Simon Cooksey |
0:fb7af294d5d9 | 37 | * |
Simon Cooksey |
0:fb7af294d5d9 | 38 | * #include "mbed.h" |
Simon Cooksey |
0:fb7af294d5d9 | 39 | * |
Simon Cooksey |
0:fb7af294d5d9 | 40 | * PortIn p(Port2, 0x0000003F); // p21-p26 |
Simon Cooksey |
0:fb7af294d5d9 | 41 | * DigitalOut ind(LED4); |
Simon Cooksey |
0:fb7af294d5d9 | 42 | * |
Simon Cooksey |
0:fb7af294d5d9 | 43 | * int main() { |
Simon Cooksey |
0:fb7af294d5d9 | 44 | * while(1) { |
Simon Cooksey |
0:fb7af294d5d9 | 45 | * int pins = p.read(); |
Simon Cooksey |
0:fb7af294d5d9 | 46 | * if(pins) { |
Simon Cooksey |
0:fb7af294d5d9 | 47 | * ind = 1; |
Simon Cooksey |
0:fb7af294d5d9 | 48 | * } else { |
Simon Cooksey |
0:fb7af294d5d9 | 49 | * ind = 0; |
Simon Cooksey |
0:fb7af294d5d9 | 50 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 51 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 52 | * } |
Simon Cooksey |
0:fb7af294d5d9 | 53 | * @endcode |
Simon Cooksey |
0:fb7af294d5d9 | 54 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 55 | class PortIn { |
Simon Cooksey |
0:fb7af294d5d9 | 56 | public: |
Simon Cooksey |
0:fb7af294d5d9 | 57 | |
Simon Cooksey |
0:fb7af294d5d9 | 58 | /** Create an PortIn, connected to the specified port |
Simon Cooksey |
0:fb7af294d5d9 | 59 | * |
Simon Cooksey |
0:fb7af294d5d9 | 60 | * @param port Port to connect to (Port0-Port5) |
Simon Cooksey |
0:fb7af294d5d9 | 61 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
Simon Cooksey |
0:fb7af294d5d9 | 62 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 63 | PortIn(PortName port, int mask = 0xFFFFFFFF) { |
Simon Cooksey |
0:fb7af294d5d9 | 64 | core_util_critical_section_enter(); |
Simon Cooksey |
0:fb7af294d5d9 | 65 | port_init(&_port, port, mask, PIN_INPUT); |
Simon Cooksey |
0:fb7af294d5d9 | 66 | core_util_critical_section_exit(); |
Simon Cooksey |
0:fb7af294d5d9 | 67 | } |
Simon Cooksey |
0:fb7af294d5d9 | 68 | |
Simon Cooksey |
0:fb7af294d5d9 | 69 | /** Read the value currently output on the port |
Simon Cooksey |
0:fb7af294d5d9 | 70 | * |
Simon Cooksey |
0:fb7af294d5d9 | 71 | * @returns |
Simon Cooksey |
0:fb7af294d5d9 | 72 | * An integer with each bit corresponding to associated port pin setting |
Simon Cooksey |
0:fb7af294d5d9 | 73 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 74 | int read() { |
Simon Cooksey |
0:fb7af294d5d9 | 75 | return port_read(&_port); |
Simon Cooksey |
0:fb7af294d5d9 | 76 | } |
Simon Cooksey |
0:fb7af294d5d9 | 77 | |
Simon Cooksey |
0:fb7af294d5d9 | 78 | /** Set the input pin mode |
Simon Cooksey |
0:fb7af294d5d9 | 79 | * |
Simon Cooksey |
0:fb7af294d5d9 | 80 | * @param mode PullUp, PullDown, PullNone, OpenDrain |
Simon Cooksey |
0:fb7af294d5d9 | 81 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 82 | void mode(PinMode mode) { |
Simon Cooksey |
0:fb7af294d5d9 | 83 | core_util_critical_section_enter(); |
Simon Cooksey |
0:fb7af294d5d9 | 84 | port_mode(&_port, mode); |
Simon Cooksey |
0:fb7af294d5d9 | 85 | core_util_critical_section_exit(); |
Simon Cooksey |
0:fb7af294d5d9 | 86 | } |
Simon Cooksey |
0:fb7af294d5d9 | 87 | |
Simon Cooksey |
0:fb7af294d5d9 | 88 | /** A shorthand for read() |
Simon Cooksey |
0:fb7af294d5d9 | 89 | */ |
Simon Cooksey |
0:fb7af294d5d9 | 90 | operator int() { |
Simon Cooksey |
0:fb7af294d5d9 | 91 | return read(); |
Simon Cooksey |
0:fb7af294d5d9 | 92 | } |
Simon Cooksey |
0:fb7af294d5d9 | 93 | |
Simon Cooksey |
0:fb7af294d5d9 | 94 | private: |
Simon Cooksey |
0:fb7af294d5d9 | 95 | port_t _port; |
Simon Cooksey |
0:fb7af294d5d9 | 96 | }; |
Simon Cooksey |
0:fb7af294d5d9 | 97 | |
Simon Cooksey |
0:fb7af294d5d9 | 98 | } // namespace mbed |
Simon Cooksey |
0:fb7af294d5d9 | 99 | |
Simon Cooksey |
0:fb7af294d5d9 | 100 | #endif |
Simon Cooksey |
0:fb7af294d5d9 | 101 | |
Simon Cooksey |
0:fb7af294d5d9 | 102 | #endif |
Simon Cooksey |
0:fb7af294d5d9 | 103 | |
Simon Cooksey |
0:fb7af294d5d9 | 104 | /** @}*/ |