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 gpio_file GPIO peripheral API
2 * @brief SWM050 GPIO API.
3 * @ingroup peripheral_apis
4 * LGPL License Terms @ref lgpl_license
5 * @author @htmlonly © @endhtmlonly 2019
6 * Icenowy Zheng <icenowy@aosc.io>
7 * @author @htmlonly &copy; @endhtmlonly 2019
8 * Caleb Szalacinski <contact@skiboy.net>
9 */
10/*
11 * This file is part of the libopencm3 project.
12 *
13 * Copyright (C) 2019 Icenowy Zheng <icenowy@aosc.io>
14 * Copyright (C) 2019 Caleb Szalacinski <contact@skiboy.net>
15 *
16 * This library is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Lesser General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Lesser General Public License for more details.
25 *
26 * You should have received a copy of the GNU Lesser General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 */
29/**@{*/
31
32/*---------------------------------------------------------------------------*/
33/** @brief Set a Group of Pins
34
35Set one or more pins of GPIO to 1. Please note that this chip doesn't support
36atomic pin setting.
37
38@param[in] gpios Pin identifiers @ref gpio_pin_id
39 If multiple pins are to be changed, use bitwise OR '|' to separate
40 them.
41*/
42void gpio_set(uint16_t gpios)
43{
44 GPIO_ADATA |= gpios;
45}
46
47/*---------------------------------------------------------------------------*/
48/** @brief Clear a Group of Pins
49
50Set one or more pins of GPIO to 0. Please note that this chip doesn't support
51atomic pin setting.
52
53@param[in] gpios Pin identifiers @ref gpio_pin_id
54 If multiple pins are to be changed, use bitwise OR '|' to separate
55 them.
56*/
57void gpio_clear(uint16_t gpios)
58{
59 GPIO_ADATA &= ~gpios;
60}
61
62/*---------------------------------------------------------------------------*/
63/** @brief Read a Group of Pins.
64
65@param[in] gpios Pin identifiers @ref gpio_pin_id
66 If multiple pins are to be read, use bitwise OR '|' to separate
67 them.
68@return The pin values as a bitfield. The bit position of the pin
69 value returned corresponds to the pin number.
70*/
71uint16_t gpio_get(uint16_t gpios)
72{
73 return GPIO_AEXT & gpios;
74}
75
76/*---------------------------------------------------------------------------*/
77/** @brief Toggle a Group of Pins
78
79Toggle one or more pins of GPIO. The non-toggled pins are not affected.
80
81@param[in] gpios Pin identifiers @ref gpio_pin_id
82 If multiple pins are to be changed, use bitwise OR '|' to separate
83 them.
84*/
85void gpio_toggle(uint16_t gpios)
86{
87 uint32_t curr_status = GPIO_ADATA & gpios;
88 GPIO_ADATA = (GPIO_ADATA & (~gpios)) | (~curr_status);
89}
90
91/*---------------------------------------------------------------------------*/
92/** @brief Set the direction of a Group of Pins to Input
93
94Set the direction of one or more pins of GPIO to input.
95
96@param[in] gpios Pin identifiers @ref gpio_pin_id
97 If multiple pins are to be changed, use bitwise OR '|' to separate
98 them.
99*/
100void gpio_input(uint16_t gpios)
101{
102 GPIO_ADIR &= ~gpios;
103}
104
105/*---------------------------------------------------------------------------*/
106/** @brief Set the direction of a Group of Pins to Output
107
108Set the direction of one or more pins of GPIO to output.
109
110@param[in] gpios Pin identifiers @ref gpio_pin_id
111 If multiple pins are to be changed, use bitwise OR '|' to separate
112 them.
113*/
114void gpio_output(uint16_t gpios)
115{
116 GPIO_ADIR |= gpios;
117}
118
119
120/*---------------------------------------------------------------------------*/
121/** @brief Sets the pins as external interrupts, rather than normal GPIO
122
123Enable interrupts on the selected pins. If you want to quickly
124switch on and off interrupts, use gpio_int_mask() after calling this.
125
126@param[in] gpios Pin identifiers @ref gpio_pin_id
127 If multiple pins are to be changed, use bitwise OR '|' to separate
128 them.
129
130@param[in] en True to enable, false to disable.
131*/
132void gpio_int_enable(uint16_t gpios, bool en)
133{
134 if (en) {
135 GPIO_INTEN_A |= gpios;
136 } else {
137 GPIO_INTEN_A &= ~gpios;
138 }
139}
140
141
142/*---------------------------------------------------------------------------*/
143/** @brief Sets bits in the interrupt mask
144
145When interrupts are masked, it prevents them from being received, which is a
146quicker way to turn on and off GPIO interrupts (after calling gpio_int_en()).
147
148@param[in] gpios Pin identifiers @ref gpio_pin_id
149 If multiple pins are to be changed, use bitwise OR '|' to separate
150 them.
151
152@param[in] masked Pin mask selection @ref gpio_int_masked
153 Whether to mask or unmask pins.
154*/
155void gpio_int_mask(uint16_t gpios, enum gpio_int_masked masked)
156{
157 if (masked) {
158 GPIO_INTMASK_A |= gpios;
159 } else {
160 GPIO_INTMASK_A &= ~gpios;
161 }
162}
163
164
165/*---------------------------------------------------------------------------*/
166/** @brief Sets whether the pins are edge triggered or level triggered
167
168Sets whether the pins are edge triggered or level triggered. Edge-triggered
169interrupt bits must be cleared by software.
170
171@param[in] gpios Pin identifiers @ref gpio_pin_id
172 If multiple pins are to be changed, use bitwise OR '|' to separate
173 them.
174
175@param[in] type Trigger Type @ref gpio_trig_type
176 Level or edge triggered
177*/
178void gpio_int_type(uint16_t gpios, enum gpio_trig_type type)
179{
180 if (type) {
181 GPIO_INTLEVEL_A |= gpios;
182 } else {
183 GPIO_INTLEVEL_A &= ~gpios;
184 }
185}
186
187
188/*---------------------------------------------------------------------------*/
189/** @brief Sets the interrupt trigger polarity
190
191Sets whether the interrupt is triggered by a high or low level/edge.
192
193@param[in] gpios Pin identifiers @ref gpio_pin_id
194 If multiple pins are to be changed, use bitwise OR '|' to separate
195 them.
196
197@param[in] pol Polarity @ref gpio_pol
198 High or low level/edge
199*/
200void gpio_int_pol(uint16_t gpios, enum gpio_pol pol)
201{
202 if (pol) {
203 GPIO_INTPOLARITY_A |= gpios;
204 } else {
205 GPIO_INTPOLARITY_A &= ~gpios;
206 }
207}
208
209
210/*---------------------------------------------------------------------------*/
211/** @brief Gets the masked interrupt status
212
213Returns the pin interrupt status masked with the mask set
214in @ref gpio_int_mask().
215
216@return The masked pin interrupt status as a bitfield. The bit position of the
217 pin value returned corresponds to the pin number.
218*/
219uint16_t gpio_int_status(void)
220{
221 return GPIO_INTSTAT_A;
222}
223
224
225/*---------------------------------------------------------------------------*/
226/** @brief Gets the raw unmasked interrupt status
227
228Returns the raw unmasked interrupt status.
229
230@return The unmasked pin interrupt status as a bitfield. The bit position of the
231 pin value returned corresponds to the pin number.
232*/
234{
235 return GPIO_RAWINTSTAT_A;
236}
237
238
239/*---------------------------------------------------------------------------*/
240/** @brief Clear the specified pin interrupts
241
242Clears the specified pin interrupts. Edge-triggered interrupts must be cleared
243by software.
244
245@param[in] gpios Pin identifiers @ref gpio_pin_id
246 If multiple pins are to be changed, use bitwise OR '|' to separate
247 them.
248*/
249void gpio_int_clear(uint16_t gpios)
250{
251 GPIO_INTEOI_A |= gpios;
252}
253
254/**@}*/
void gpio_int_clear(uint16_t gpios)
Clear the specified pin interrupts.
Definition: gpio.c:249
void gpio_int_type(uint16_t gpios, enum gpio_trig_type type)
Sets whether the pins are edge triggered or level triggered.
Definition: gpio.c:178
uint16_t gpio_get(uint16_t gpios)
Read a Group of Pins.
Definition: gpio.c:71
void gpio_int_mask(uint16_t gpios, enum gpio_int_masked masked)
Sets bits in the interrupt mask.
Definition: gpio.c:155
void gpio_output(uint16_t gpios)
Set the direction of a Group of Pins to Output.
Definition: gpio.c:114
void gpio_int_enable(uint16_t gpios, bool en)
Sets the pins as external interrupts, rather than normal GPIO.
Definition: gpio.c:132
void gpio_set(uint16_t gpios)
Set a Group of Pins.
Definition: gpio.c:42
void gpio_int_pol(uint16_t gpios, enum gpio_pol pol)
Sets the interrupt trigger polarity.
Definition: gpio.c:200
uint16_t gpio_int_status(void)
Gets the masked interrupt status.
Definition: gpio.c:219
void gpio_input(uint16_t gpios)
Set the direction of a Group of Pins to Input.
Definition: gpio.c:100
void gpio_clear(uint16_t gpios)
Clear a Group of Pins.
Definition: gpio.c:57
void gpio_toggle(uint16_t gpios)
Toggle a Group of Pins.
Definition: gpio.c:85
uint16_t gpio_int_raw_status(void)
Gets the raw unmasked interrupt status.
Definition: gpio.c:233
gpio_int_masked
Definition: gpio.h:80
gpio_pol
Definition: gpio.h:62
#define GPIO_INTEOI_A
Interrupt clear register.
Definition: gpio.h:106
#define GPIO_RAWINTSTAT_A
Interrupt status before masking.
Definition: gpio.h:104
#define GPIO_ADATA
Data register.
Definition: gpio.h:90
#define GPIO_INTSTAT_A
Interrupt status after masking.
Definition: gpio.h:102
#define GPIO_ADIR
Direction register.
Definition: gpio.h:92
#define GPIO_INTMASK_A
Interrupt mask register.
Definition: gpio.h:96
#define GPIO_AEXT
External register (wat)
Definition: gpio.h:108
#define GPIO_INTPOLARITY_A
Interrupt polarity register.
Definition: gpio.h:100
#define GPIO_INTLEVEL_A
Interrupt trigger mode register.
Definition: gpio.h:98
#define GPIO_INTEN_A
Interrupt enable register.
Definition: gpio.h:94
gpio_trig_type
Definition: gpio.h:71