libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio.c
Go to the documentation of this file.
1/** @addtogroup gpio_file GPIO peripheral API
2 *
3 * @brief <b>Access functions for the I/O Controller</b>
4 *
5 * @ingroup peripheral_apis
6 * LGPL License Terms @ref lgpl_license
7 * @author @htmlonly &copy; @endhtmlonly 2016
8 * Maxim Sloyko <maxims@google.com>
9 *
10 */
11
12/*
13 * This file is part of the libopencm3 project.
14 *
15 * Copyright (C) 2017-2018 Unicore MX project<dev(at)lists(dot)unicore-mx(dot)org>
16 * Copyright (C) 2021 Eduard Drusa <ventyl86(at)netkosice(dot)sk>
17 *
18 * This library is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with this library. If not, see <http://www.gnu.org/licenses/>.
30 */
31
32/** @{ */
33
34#include <libopencm3/nrf/gpio.h>
35
36/** @brief Atomic set output
37 *
38 * @param[in] gpioport Port identifier @ref gpio_port_id
39 * @param[in] gpios Pin identifiers @ref gpio_pin_id
40 */
41void gpio_set(uint32_t gpioport, uint32_t gpios)
42{
43 (void) gpioport;
44 GPIO_OUTSET = gpios;
45}
46
47/** @brief Atomic clear output
48 *
49 * @param[in] gpioport Port identifier @ref gpio_port_id
50 * @param[in] gpios Pin identifiers @ref gpio_pin_id
51 */
52void gpio_clear(uint32_t gpioport, uint32_t gpios)
53{
54 (void) gpioport;
55 GPIO_OUTCLR = gpios;
56}
57
58/** @brief Toggle output
59 *
60 * @param[in] gpioport Port identifier @ref gpio_port_id
61 * @param[in] gpios Pin identifiers @ref gpio_pin_id
62 */
63void gpio_toggle(uint32_t gpioport, uint32_t gpios)
64{
65 (void) gpioport;
66 uint32_t reg_val = GPIO_OUT;
67 GPIO_OUTCLR = reg_val & gpios;
68 GPIO_OUTSET = (~reg_val) & gpios;
69}
70
71/** @brief Read GPIO values
72 *
73 * @param[in] gpioport Port identifier @ref gpio_port_id
74 * @param[in] gpios Pin identifiers @ref gpio_pin_id
75 */
76uint32_t gpio_get(uint32_t gpioport, uint32_t gpios)
77{
78 (void) gpioport;
79 return GPIO_IN & gpios;
80}
81
82/** @brief Set GPIO Pin Mode
83 *
84 * Sets the mode (input/output) and configuration (analog/digitial and
85 * open drain/push pull), for a set of GPIO pins on a given GPIO port.
86 *
87 * @param[in] gpioport Port identifier @ref gpio_port_id
88 * @param[in] mode Pin mode @ref gpio_mode
89 * @param[in] pull_up_down Pull up / pull down configuration @ref gpio_pupd
90 * @param[in] gpios Pin identifiers @ref gpio_pin_id
91 * If multiple pins are to be set, use bitwise OR '|' to separate
92 * them.
93 */
94void gpio_mode_setup(uint32_t gpioport, uint32_t mode, uint32_t pull_up_down,
95 uint32_t gpios)
96{
97 (void) gpioport;
98
99 uint8_t i = 0;
100 while (gpios) {
101 if (gpios & 1) {
102 GPIO_PIN_CNF(i) = (
103 GPIO_PIN_CNF(i) &
106 )
107 ) | (mode << GPIO_CNF_MODE_SHIFT)
108 | (pull_up_down << GPIO_CNF_PUPD_SHIFT);
109 }
110 ++i;
111 gpios >>= 1;
112 }
113}
114
115/** Configure GPIO pin input and output specifics.
116 *
117 * Configure drive strength and input sensing for given GPIO port.
118 * @param [in] gpioport GPIO port identifier, see @ref gpio_port_id
119 * @param [in] drive Drive schema used to drive pin, see @ref gpio_drive
120 * @param [in] sense Pin sensing mechanism, see @ref gpio_sense
121 * @param[in] gpios Pin identifiers @ref gpio_pin_id
122 * If multiple pins are to be set, use bitwise OR '|' to separate
123 * them.
124 */
125void gpio_set_options(uint32_t gpioport, uint32_t drive, uint32_t sense,
126 uint32_t gpios)
127{
128 (void) gpioport;
129
130 uint8_t i = 0;
131 while (gpios) {
132 if (gpios & 1) {
133 GPIO_PIN_CNF(i) = (GPIO_PIN_CNF(i) &
136 )
137 ) | (drive << GPIO_CNF_DRIVE_SHIFT)
138 | (sense << GPIO_CNF_SENSE_SHIFT);
139 }
140 ++i;
141 gpios >>= 1;
142 }
143
144}
145
146
147/** @brief Configure Task in GPIO TE Module
148 *
149 * @param[in] task_num uint8_t Task number (0-3)
150 * @param[in] pin_num uint8_t GPIO Pin number (0-31)
151 * @param[in] polarity uint8_t polarity Operation to perform when task is triggered.
152 * @param[in] init uint8_t Initial state of the pin, non-zero means initially active,
153 * zero means initially inactive
154 */
155void gpio_configure_task(uint8_t task_num,
156 uint8_t pin_num, uint8_t polarity, uint32_t init)
157{
158 /* any non-zero value means, that pin is active */
159 if (init) {
161 }
162
164 | (pin_num << GPIO_TE_CONFIG_PSEL_SHIFT)
165 | (polarity << GPIO_TE_CONFIG_POLARITY_SHIFT)
166 | init;
167}
168
169/** @brief Configure Event in GPIO TE Module
170 *
171 * @param[in] event_num Event number (0-3)
172 * @param[in] pin_num GPIO Pin number (0-31)
173 * @param[in] polarity Operation to perform when task is triggered.
174 */
175void gpio_configure_event(uint8_t event_num, uint8_t pin_num, uint8_t polarity)
176{
178 | (pin_num << GPIO_TE_CONFIG_PSEL_SHIFT)
179 | (polarity << GPIO_TE_CONFIG_POLARITY_SHIFT);
180}
181
182/** @brief Enable GPIO interrupts
183 *
184 * @param[in] mask interrupts to enable.
185 */
186void gpio_enable_interrupts(uint32_t mask)
187{
188 GPIO_INTENSET = mask;
189}
190
191/** @brief Disable GPIO interrupts
192 *
193 * @param[in] mask interrupts to disable.
194 */
195void gpio_disable_interrupts(uint32_t mask)
196{
197 GPIO_INTENCLR = mask;
198}
199
200/** @brief Disable all GPIO interrupts
201 *
202 */
204{
205 GPIO_INTENCLR = 0xffffffff;
206}
207
208/** @} */
#define GPIO_TE_CONFIG_PSEL_SHIFT
Definition: common/gpio.h:151
#define GPIO_OUTCLR
Definition: common/gpio.h:44
#define GPIO_OUTSET
Definition: common/gpio.h:43
#define GPIO_CNF_PUPD_MASK
Definition: common/gpio.h:69
#define GPIO_CNF_SENSE_MASK
Definition: common/gpio.h:113
#define GPIO_TE_CONFIG_MODE_SHIFT
Definition: common/gpio.h:148
#define GPIO_TE_CONFIG(n)
Definition: common/gpio.h:139
#define GPIO_PIN_CNF(N)
Definition: common/gpio.h:52
#define GPIO_INTENCLR
Definition: common/gpio.h:137
#define GPIO_CNF_DRIVE_MASK
Definition: common/gpio.h:81
#define GPIO_CNF_PUPD_SHIFT
Definition: common/gpio.h:70
#define GPIO_TE_CONFIG_OUTINIT
Definition: common/gpio.h:157
#define GPIO_CNF_SENSE_SHIFT
Definition: common/gpio.h:112
#define GPIO_OUT
Definition: common/gpio.h:42
#define GPIO_IN
Definition: common/gpio.h:46
#define GPIO_INTENSET
Definition: common/gpio.h:136
#define GPIO_CNF_DRIVE_SHIFT
Definition: common/gpio.h:80
#define GPIO_TE_MODE_TASK
Definition: common/gpio.h:161
#define GPIO_TE_MODE_EVENT
Definition: common/gpio.h:160
#define GPIO_TE_CONFIG_POLARITY_SHIFT
Definition: common/gpio.h:154
#define GPIO_CNF_MODE_SHIFT
Definition: common/gpio.h:58
#define GPIO_CNF_MODE_MASK
Definition: common/gpio.h:57
uint32_t gpio_get(uint32_t gpioport, uint32_t gpios)
Read GPIO values.
Definition: gpio.c:76
void gpio_configure_event(uint8_t event_num, uint8_t pin_num, uint8_t polarity)
Configure Event in GPIO TE Module.
Definition: gpio.c:175
void gpio_disable_interrupts(uint32_t mask)
Disable GPIO interrupts.
Definition: gpio.c:195
void gpio_set(uint32_t gpioport, uint32_t gpios)
Atomic set output.
Definition: gpio.c:41
void gpio_configure_task(uint8_t task_num, uint8_t pin_num, uint8_t polarity, uint32_t init)
Configure Task in GPIO TE Module.
Definition: gpio.c:155
void gpio_set_options(uint32_t gpioport, uint32_t drive, uint32_t sense, uint32_t gpios)
Configure GPIO pin input and output specifics.
Definition: gpio.c:125
void gpio_clear_interrupts(void)
Disable all GPIO interrupts.
Definition: gpio.c:203
void gpio_clear(uint32_t gpioport, uint32_t gpios)
Atomic clear output.
Definition: gpio.c:52
void gpio_toggle(uint32_t gpioport, uint32_t gpios)
Toggle output.
Definition: gpio.c:63
void gpio_enable_interrupts(uint32_t mask)
Enable GPIO interrupts.
Definition: gpio.c:186
void gpio_mode_setup(uint32_t gpioport, uint32_t mode, uint32_t pull_up_down, uint32_t gpios)
Set GPIO Pin Mode.
Definition: gpio.c:94