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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.