inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
NYX 0:85b3fd62ea1a 1 /* mbed Microcontroller Library
NYX 0:85b3fd62ea1a 2 * Copyright (c) 2006-2013 ARM Limited
NYX 0:85b3fd62ea1a 3 *
NYX 0:85b3fd62ea1a 4 * Licensed under the Apache License, Version 2.0 (the "License");
NYX 0:85b3fd62ea1a 5 * you may not use this file except in compliance with the License.
NYX 0:85b3fd62ea1a 6 * You may obtain a copy of the License at
NYX 0:85b3fd62ea1a 7 *
NYX 0:85b3fd62ea1a 8 * http://www.apache.org/licenses/LICENSE-2.0
NYX 0:85b3fd62ea1a 9 *
NYX 0:85b3fd62ea1a 10 * Unless required by applicable law or agreed to in writing, software
NYX 0:85b3fd62ea1a 11 * distributed under the License is distributed on an "AS IS" BASIS,
NYX 0:85b3fd62ea1a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
NYX 0:85b3fd62ea1a 13 * See the License for the specific language governing permissions and
NYX 0:85b3fd62ea1a 14 * limitations under the License.
NYX 0:85b3fd62ea1a 15 */
NYX 0:85b3fd62ea1a 16 #ifndef MBED_PORTIN_H
NYX 0:85b3fd62ea1a 17 #define MBED_PORTIN_H
NYX 0:85b3fd62ea1a 18
NYX 0:85b3fd62ea1a 19 #include "platform/platform.h"
NYX 0:85b3fd62ea1a 20
NYX 0:85b3fd62ea1a 21 #if defined (DEVICE_PORTIN) || defined(DOXYGEN_ONLY)
NYX 0:85b3fd62ea1a 22
NYX 0:85b3fd62ea1a 23 #include "hal/port_api.h"
NYX 0:85b3fd62ea1a 24 #include "platform/mbed_critical.h"
NYX 0:85b3fd62ea1a 25
NYX 0:85b3fd62ea1a 26 namespace mbed {
NYX 0:85b3fd62ea1a 27 /** \addtogroup drivers */
NYX 0:85b3fd62ea1a 28
NYX 0:85b3fd62ea1a 29 /** A multiple pin digital input
NYX 0:85b3fd62ea1a 30 *
NYX 0:85b3fd62ea1a 31 * @note Synchronization level: Interrupt safe
NYX 0:85b3fd62ea1a 32 *
NYX 0:85b3fd62ea1a 33 * Example:
NYX 0:85b3fd62ea1a 34 * @code
NYX 0:85b3fd62ea1a 35 * // Switch on an LED if any of mbed pins 21-26 is high
NYX 0:85b3fd62ea1a 36 *
NYX 0:85b3fd62ea1a 37 * #include "mbed.h"
NYX 0:85b3fd62ea1a 38 *
NYX 0:85b3fd62ea1a 39 * PortIn p(Port2, 0x0000003F); // p21-p26
NYX 0:85b3fd62ea1a 40 * DigitalOut ind(LED4);
NYX 0:85b3fd62ea1a 41 *
NYX 0:85b3fd62ea1a 42 * int main() {
NYX 0:85b3fd62ea1a 43 * while(1) {
NYX 0:85b3fd62ea1a 44 * int pins = p.read();
NYX 0:85b3fd62ea1a 45 * if(pins) {
NYX 0:85b3fd62ea1a 46 * ind = 1;
NYX 0:85b3fd62ea1a 47 * } else {
NYX 0:85b3fd62ea1a 48 * ind = 0;
NYX 0:85b3fd62ea1a 49 * }
NYX 0:85b3fd62ea1a 50 * }
NYX 0:85b3fd62ea1a 51 * }
NYX 0:85b3fd62ea1a 52 * @endcode
NYX 0:85b3fd62ea1a 53 * @ingroup drivers
NYX 0:85b3fd62ea1a 54 */
NYX 0:85b3fd62ea1a 55 class PortIn {
NYX 0:85b3fd62ea1a 56 public:
NYX 0:85b3fd62ea1a 57
NYX 0:85b3fd62ea1a 58 /** Create an PortIn, connected to the specified port
NYX 0:85b3fd62ea1a 59 *
NYX 0:85b3fd62ea1a 60 * @param port Port to connect to (Port0-Port5)
NYX 0:85b3fd62ea1a 61 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
NYX 0:85b3fd62ea1a 62 */
NYX 0:85b3fd62ea1a 63 PortIn(PortName port, int mask = 0xFFFFFFFF) {
NYX 0:85b3fd62ea1a 64 core_util_critical_section_enter();
NYX 0:85b3fd62ea1a 65 port_init(&_port, port, mask, PIN_INPUT);
NYX 0:85b3fd62ea1a 66 core_util_critical_section_exit();
NYX 0:85b3fd62ea1a 67 }
NYX 0:85b3fd62ea1a 68
NYX 0:85b3fd62ea1a 69 /** Read the value currently output on the port
NYX 0:85b3fd62ea1a 70 *
NYX 0:85b3fd62ea1a 71 * @returns
NYX 0:85b3fd62ea1a 72 * An integer with each bit corresponding to associated port pin setting
NYX 0:85b3fd62ea1a 73 */
NYX 0:85b3fd62ea1a 74 int read() {
NYX 0:85b3fd62ea1a 75 return port_read(&_port);
NYX 0:85b3fd62ea1a 76 }
NYX 0:85b3fd62ea1a 77
NYX 0:85b3fd62ea1a 78 /** Set the input pin mode
NYX 0:85b3fd62ea1a 79 *
NYX 0:85b3fd62ea1a 80 * @param mode PullUp, PullDown, PullNone, OpenDrain
NYX 0:85b3fd62ea1a 81 */
NYX 0:85b3fd62ea1a 82 void mode(PinMode mode) {
NYX 0:85b3fd62ea1a 83 core_util_critical_section_enter();
NYX 0:85b3fd62ea1a 84 port_mode(&_port, mode);
NYX 0:85b3fd62ea1a 85 core_util_critical_section_exit();
NYX 0:85b3fd62ea1a 86 }
NYX 0:85b3fd62ea1a 87
NYX 0:85b3fd62ea1a 88 /** A shorthand for read()
NYX 0:85b3fd62ea1a 89 */
NYX 0:85b3fd62ea1a 90 operator int() {
NYX 0:85b3fd62ea1a 91 return read();
NYX 0:85b3fd62ea1a 92 }
NYX 0:85b3fd62ea1a 93
NYX 0:85b3fd62ea1a 94 private:
NYX 0:85b3fd62ea1a 95 port_t _port;
NYX 0:85b3fd62ea1a 96 };
NYX 0:85b3fd62ea1a 97
NYX 0:85b3fd62ea1a 98 } // namespace mbed
NYX 0:85b3fd62ea1a 99
NYX 0:85b3fd62ea1a 100 #endif
NYX 0:85b3fd62ea1a 101
NYX 0:85b3fd62ea1a 102 #endif