mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
mbed library release version 165

Who changed what in which revision?

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