Mbed OS Reference
Loading...
Searching...
No Matches
gpio_api.h
1
2/** \addtogroup hal */
3/** @{*/
4/* mbed Microcontroller Library
5 * Copyright (c) 2006-2020 ARM Limited
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#ifndef MBED_GPIO_API_H
21#define MBED_GPIO_API_H
22
23#include <stdint.h>
24#include "device.h"
25#include "pinmap.h"
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31/**
32 * \defgroup hal_gpio GPIO HAL functions
33 *
34 * # Defined behavior
35 * * ::gpio_init and other init functions can be called with NC or a valid PinName for the target - Verified by gpio_nc_test
36 * * ::gpio_is_connected can be used to test whether a gpio_t object was initialized with NC - Verified by gpio_nc_test
37 * * ::gpio_init initializes the GPIO pin
38 * * ::gpio_free returns the pin owned by the GPIO object to its reset state
39 * * ::gpio_mode sets the mode of the given pin
40 * * ::gpio_dir sets the direction of the given pin
41 * * ::gpio_write sets the gpio output value
42 * * ::gpio_read reads the input value
43 * * ::gpio_init_in inits the input pin and sets mode to PullDefault
44 * * ::gpio_init_in_ex inits the input pin and sets the mode
45 * * ::gpio_init_out inits the pin as an output, with predefined output value 0
46 * * ::gpio_init_out_ex inits the pin as an output and sets the output value
47 * * ::gpio_init_inout inits the pin to be input/output and set pin mode and value
48 * * The GPIO operations ::gpio_write, ::gpio_read take less than 20us to complete
49 * * The function ::gpio_get_capabilities fills the given
50 * `gpio_capabilities_t` instance according to pin capabilities.
51 *
52 * # Undefined behavior
53 * * Calling any ::gpio_mode, ::gpio_dir, ::gpio_write or ::gpio_read on a gpio_t object that was initialized
54 * with NC.
55 * * Calling ::gpio_set with NC.
56 *
57 * @{
58 */
59
60/**
61 * \defgroup hal_gpio_tests GPIO HAL tests
62 * The GPIO HAL tests ensure driver conformance to defined behaviour.
63 *
64 * To run the GPIO hal tests use the command:
65 *
66 * mbed test -t <toolchain> -m <target> -n tests-mbed_hal_fpga_ci_test_shield-gpio,tests-mbed_hal-gpio
67 *
68 */
69
70/** GPIO capabilities for a given pin
71 */
72typedef struct {
73 uint8_t pull_none : 1;
74 uint8_t pull_down : 1;
75 uint8_t pull_up : 1;
77
78/** Set the given pin as GPIO
79 *
80 * @param pin The pin to be set as GPIO
81 * @return The GPIO port mask for this pin
82 **/
83uint32_t gpio_set(PinName pin);
84
85/** Checks if gpio object is connected (pin was not initialized with NC)
86 * @param obj The GPIO object
87 * @return 0 if object was initialized with NC
88 * @return non-zero if object was initialized with a valid PinName
89 **/
90int gpio_is_connected(const gpio_t *obj);
91
92/** Initialize the GPIO pin
93 *
94 * @param obj The GPIO object to initialize
95 * @param pin The GPIO pin to initialize (may be NC)
96 */
97void gpio_init(gpio_t *obj, PinName pin);
98
99/** Releases the GPIO pin
100 *
101 * @param obj The GPIO object to release
102 */
103void gpio_free(gpio_t *obj);
104
105/** Set the input pin mode
106 *
107 * @param obj The GPIO object (must be connected)
108 * @param mode The pin mode to be set
109 */
110void gpio_mode(gpio_t *obj, PinMode mode);
111
112/** Set the pin direction
113 *
114 * @param obj The GPIO object (must be connected)
115 * @param direction The pin direction to be set
116 */
117void gpio_dir(gpio_t *obj, PinDirection direction);
118
119/** Set the output value
120 *
121 * @param obj The GPIO object (must be connected)
122 * @param value The value to be set
123 */
124void gpio_write(gpio_t *obj, int value);
125
126/** Read the input value
127 *
128 * @param obj The GPIO object (must be connected)
129 * @return An integer value 1 or 0
130 */
131int gpio_read(gpio_t *obj);
132
133// the following functions are generic and implemented in the common gpio.c file
134// TODO: fix, will be moved to the common gpio header file
135
136/** Init the input pin and set mode to PullDefault
137 *
138 * @param gpio The GPIO object
139 * @param pin The pin name (may be NC)
140 */
141void gpio_init_in(gpio_t *gpio, PinName pin);
142
143/** Init the input pin and set the mode
144 *
145 * @param gpio The GPIO object
146 * @param pin The pin name (may be NC)
147 * @param mode The pin mode to be set
148 */
149void gpio_init_in_ex(gpio_t *gpio, PinName pin, PinMode mode);
150
151/** Init the output pin as an output, with predefined output value 0
152 *
153 * @param gpio The GPIO object
154 * @param pin The pin name (may be NC)
155 */
156void gpio_init_out(gpio_t *gpio, PinName pin);
157
158/** Init the pin as an output and set the output value
159 *
160 * @param gpio The GPIO object
161 * @param pin The pin name (may be NC)
162 * @param value The value to be set
163 */
164void gpio_init_out_ex(gpio_t *gpio, PinName pin, int value);
165
166/** Init the pin to be in/out
167 *
168 * @param gpio The GPIO object
169 * @param pin The pin name (may be NC)
170 * @param direction The pin direction to be set
171 * @param mode The pin mode to be set
172 * @param value The value to be set for an output pin
173 */
174void gpio_init_inout(gpio_t *gpio, PinName pin, PinDirection direction, PinMode mode, int value);
175
176/** Fill the given gpio_capabilities_t instance according to pin capabilities.
177 */
179
180/** Get the pins that support all GPIO tests
181 *
182 * Return a PinMap array of pins that support GPIO. The
183 * array is terminated with {NC, NC, 0}.
184 *
185 * Targets should override the weak implementation of this
186 * function to provide the actual pinmap for GPIO testing.
187 *
188 * @return PinMap array
189 */
190const PinMap *gpio_pinmap(void);
191
192/**@}*/
193
194#ifdef __cplusplus
195}
196#endif
197
198#endif
199
200/** @}*/
void gpio_free(gpio_t *obj)
Releases the GPIO pin.
void gpio_dir(gpio_t *obj, PinDirection direction)
Set the pin direction.
void gpio_init_out(gpio_t *gpio, PinName pin)
Init the output pin as an output, with predefined output value 0.
void gpio_init_in(gpio_t *gpio, PinName pin)
Init the input pin and set mode to PullDefault.
int gpio_is_connected(const gpio_t *obj)
Checks if gpio object is connected (pin was not initialized with NC)
void gpio_init(gpio_t *obj, PinName pin)
Initialize the GPIO pin.
void gpio_mode(gpio_t *obj, PinMode mode)
Set the input pin mode.
int gpio_read(gpio_t *obj)
Read the input value.
const PinMap * gpio_pinmap(void)
Get the pins that support all GPIO tests.
void gpio_init_in_ex(gpio_t *gpio, PinName pin, PinMode mode)
Init the input pin and set the mode.
void gpio_init_inout(gpio_t *gpio, PinName pin, PinDirection direction, PinMode mode, int value)
Init the pin to be in/out.
uint32_t gpio_set(PinName pin)
Set the given pin as GPIO.
void gpio_write(gpio_t *obj, int value)
Set the output value.
void gpio_init_out_ex(gpio_t *gpio, PinName pin, int value)
Init the pin as an output and set the output value.
void gpio_get_capabilities(gpio_t *gpio, gpio_capabilities_t *cap)
Fill the given gpio_capabilities_t instance according to pin capabilities.
Definition: pinmap.h:31
GPIO capabilities for a given pin.
Definition: gpio_api.h:72