libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio.c
Go to the documentation of this file.
1/** @defgroup VF6xx_gpio GPIO
2 *
3 * @ingroup VF6xx
4 *
5 * @section vf6xx_gpio_api_ex GPIO API.
6 *
7 * @brief <b>VF6xx General-Purpose Input/Output (GPIO)</b>
8 *
9 * @author @htmlonly &copy; @endhtmlonly 2014 Stefan Agner <stefan@agner.ch>
10 *
11 * @date 03 July 2014
12 *
13 * This library supports the GPIO module in the VF6xx SoC of Freescale.
14 * Access is provided by GPIO number according to the Pinmux list of the
15 * Reference Manual, similar as GPIOs are available on Linux.
16 *
17 * LGPL License Terms @ref lgpl_license
18 */
19/*
20 * This file is part of the libopencm3 project.
21 *
22 * Copyright (C) 2014 Stefan Agner <stefan@agner.ch>
23 *
24 * This library is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU Lesser General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
28 *
29 * This library is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * along with this library. If not, see <http://www.gnu.org/licenses/>.
36 */
37
38/**@{*/
39
42
43/*---------------------------------------------------------------------------*/
44/** @brief Set GPIO
45
46Set GPIO by GPIO number according to MUX list
47
48@param[in] gpio unsigned 32 bit. GPIO number
49*/
50
51void gpio_set(uint32_t gpio)
52{
53 uint32_t port = GPIO(gpio / 32);
54 GPIO_PSOR(port) = GPIO_OFFSET(gpio);
55}
56
57/*---------------------------------------------------------------------------*/
58/** @brief Set GPIO
59
60Clears GPIO by GPIO number according to MUX list
61
62@param[in] gpio unsigned 32 bit. GPIO number
63*/
64
65void gpio_clear(uint32_t gpio)
66{
67 uint32_t port = GPIO(gpio / 32);
68 GPIO_PCOR(port) = GPIO_OFFSET(gpio);
69}
70
71/*---------------------------------------------------------------------------*/
72/** @brief Get GPIOs logic state
73
74Get logic level of GPIO given by GPIO number according to MUX list. Reading
75a GPIO value is possible even if the port is not muxed for GPIO.
76
77@param[in] gpio unsigned 32 bit. GPIO number
78@returns the logic state of the given GPIO.
79*/
80
81bool gpio_get(uint32_t gpio)
82{
83 uint32_t port = GPIO(gpio / 32);
84 return !!(GPIO_PDIR(port) & GPIO_OFFSET(gpio));
85}
86
87/*---------------------------------------------------------------------------*/
88/** @brief Toggles GPIO
89
90Toggles GPIO by GPIO number according to MUX list
91
92@param[in] gpio unsigned 32 bit. GPIO number
93*/
94
95void gpio_toggle(uint32_t gpio)
96{
97 uint32_t port = GPIO(gpio / 32);
98 GPIO_PTOR(port) = GPIO_OFFSET(gpio);
99}
100
101/*---------------------------------------------------------------------------*/
102/** @brief Read a whole GPIO Port
103
104Gets all 32 GPIOs of a Port.
105
106@param[in] gpioport unsigned 32 bit. GPIO port @ref gpio_reg_base
107@returns the logic states of the given GPIO port.
108*/
109
110uint32_t gpio_port_read(uint32_t gpioport)
111{
112 return GPIO_PDIR(gpioport);
113}
114
115/*---------------------------------------------------------------------------*/
116/** @brief Write a whole GPIO Port
117
118Sets all 32 GPIOs of a Port.
119
120@param[in] gpioport unsigned 32 bit. GPIO port @ref gpio_reg_base
121@param[in] gpio unsigned 32 bit. 1 for a logic 1 driven at port, 0 for a logic
1220 driven at port.
123*/
124
125void gpio_port_write(uint32_t gpioport, uint32_t data)
126{
127 GPIO_PDOR(gpioport) = data;
128}
129
130/**@}*/
131
uint32_t gpio_port_read(uint32_t gpioport)
Read a whole GPIO Port.
Definition: gpio.c:110
bool gpio_get(uint32_t gpio)
Get GPIOs logic state.
Definition: gpio.c:81
void gpio_set(uint32_t gpio)
Set GPIO.
Definition: gpio.c:51
void gpio_port_write(uint32_t gpioport, uint32_t data)
Write a whole GPIO Port.
Definition: gpio.c:125
void gpio_toggle(uint32_t gpio)
Toggles GPIO.
Definition: gpio.c:95
void gpio_clear(uint32_t gpio)
Set GPIO.
Definition: gpio.c:65
#define GPIO_PTOR(gpio_base)
Definition: gpio.h:62
#define GPIO_PSOR(gpio_base)
Definition: gpio.h:60
#define GPIO_PDOR(gpio_base)
Definition: gpio.h:59
#define GPIO_OFFSET(gpio)
Definition: gpio.h:55
#define GPIO_PDIR(gpio_base)
Definition: gpio.h:63
#define GPIO_PCOR(gpio_base)
Definition: gpio.h:61
#define GPIO(port)
Definition: gpio.h:48