libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio.h
Go to the documentation of this file.
1/** @defgroup gpio_defines General Purpose I/O Defines
2 *
3 * @brief <b>Defined Constants and Types for the LM4F General Purpose I/O</b>
4 *
5 * @ingroup LM4Fxx_defines
6 *
7 * @version 1.0.0
8 *
9 * @author @htmlonly &copy; @endhtmlonly 2011
10 * Gareth McMullin <gareth@blacksphere.co.nz>
11 * @author @htmlonly &copy; @endhtmlonly 2013
12 * Alexandru Gagniuc <mr.nuke.me@gmail.com>
13 *
14 * @date 16 March 2013
15 *
16 * LGPL License Terms @ref lgpl_license
17 */
18
19/*
20 * This file is part of the libopencm3 project.
21 *
22 * Copyright (C) 2011 Gareth McMullin <gareth@blacksphere.co.nz>
23 * Copyright (C) 2013 Alexandru Gagniuc <mr.nuke.me@gmail.com>
24 *
25 * This library is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU Lesser General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
29 *
30 * This library is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU Lesser General Public License for more details.
34 *
35 * You should have received a copy of the GNU Lesser General Public License
36 * along with this library. If not, see <http://www.gnu.org/licenses/>.
37 */
38
39#ifndef LM4F_GPIO_H
40#define LM4F_GPIO_H
41
42/**@{*/
43
46
47/* =============================================================================
48 * Convenience macros
49 * ---------------------------------------------------------------------------*/
50/** @defgroup gpio_reg_base GPIO register base addresses
51 * @{*/
52#define GPIOA GPIOA_BASE
53#define GPIOB GPIOB_BASE
54#define GPIOC GPIOC_BASE
55#define GPIOD GPIOD_BASE
56#define GPIOE GPIOE_BASE
57#define GPIOF GPIOF_BASE
58#define GPIOG GPIOG_BASE
59#define GPIOH GPIOH_BASE
60#define GPIOJ GPIOJ_BASE
61#define GPIOK GPIOK_BASE
62#define GPIOL GPIOL_BASE
63#define GPIOM GPIOM_BASE
64#define GPION GPION_BASE
65#define GPIOP GPIOP_BASE
66#define GPIOQ GPIOQ_BASE
67/** @} */
68
69/* =============================================================================
70 * GPIO number definitions (for convenience)
71 *
72 * These are usable across all GPIO registers,
73 * except GPIO_LOCK and GPIO_PCTL
74 * ---------------------------------------------------------------------------*/
75/** @defgroup gpio_pin_id GPIO pin identifiers
76 * @{*/
77#define GPIO0 (1 << 0)
78#define GPIO1 (1 << 1)
79#define GPIO2 (1 << 2)
80#define GPIO3 (1 << 3)
81#define GPIO4 (1 << 4)
82#define GPIO5 (1 << 5)
83#define GPIO6 (1 << 6)
84#define GPIO7 (1 << 7)
85#define GPIO_ALL 0xff
86/** @} */
87
88/* =============================================================================
89 * GPIO registers
90 * ---------------------------------------------------------------------------*/
91
92/* GPIO Data */
93#define GPIO_DATA(port) (&MMIO32((port) + 0x000))
94
95/* GPIO Direction */
96#define GPIO_DIR(port) MMIO32((port) + 0x400)
97
98/* GPIO Interrupt Sense */
99#define GPIO_IS(port) MMIO32((port) + 0x404)
100
101/* GPIO Interrupt Both Edges */
102#define GPIO_IBE(port) MMIO32((port) + 0x408)
103
104/* GPIO Interrupt Event */
105#define GPIO_IEV(port) MMIO32((port) + 0x40c)
106
107/* GPIO Interrupt Mask */
108#define GPIO_IM(port) MMIO32((port) + 0x410)
109
110/* GPIO Raw Interrupt Status */
111#define GPIO_RIS(port) MMIO32((port) + 0x414)
112
113/* GPIO Masked Interrupt Status */
114#define GPIO_MIS(port) MMIO32((port) + 0x418)
115
116/* GPIO Interrupt Clear */
117#define GPIO_ICR(port) MMIO32((port) + 0x41c)
118
119/* GPIO Alternate Function Select */
120#define GPIO_AFSEL(port) MMIO32((port) + 0x420)
121
122/* GPIO 2-mA Drive Select */
123#define GPIO_DR2R(port) MMIO32((port) + 0x500)
124
125/* GPIO 4-mA Drive Select */
126#define GPIO_DR4R(port) MMIO32((port) + 0x504)
127
128/* GPIO 8-mA Drive Select */
129#define GPIO_DR8R(port) MMIO32((port) + 0x508)
130
131/* GPIO Open Drain Select */
132#define GPIO_ODR(port) MMIO32((port) + 0x50c)
133
134/* GPIO Pull-Up Select */
135#define GPIO_PUR(port) MMIO32((port) + 0x510)
136
137/* GPIO Pull-Down Select */
138#define GPIO_PDR(port) MMIO32((port) + 0x514)
139
140/* GPIO Slew Rate Control Select */
141#define GPIO_SLR(port) MMIO32((port) + 0x518)
142
143/* GPIO Digital Enable */
144#define GPIO_DEN(port) MMIO32((port) + 0x51c)
145
146/* GPIO Lock */
147#define GPIO_LOCK(port) MMIO32((port) + 0x520)
148
149/* GPIO Commit */
150#define GPIO_CR(port) MMIO32((port) + 0x524)
151
152/* GPIO Analog Mode Select */
153#define GPIO_AMSEL(port) MMIO32((port) + 0x528)
154
155/* GPIO Port Control */
156#define GPIO_PCTL(port) MMIO32((port) + 0x52C)
157
158/* GPIO ADC Control */
159#define GPIO_ADCCTL(port) MMIO32((port) + 0x530)
160
161/* GPIO DMA Control */
162#define GPIO_DMACTL(port) MMIO32((port) + 0x534)
163
164/* GPIO Peripheral Identification */
165#define GPIO_PERIPH_ID4(port) MMIO32((port) + 0xFD0)
166#define GPIO_PERIPH_ID5(port) MMIO32((port) + 0xFD4)
167#define GPIO_PERIPH_ID6(port) MMIO32((port) + 0xFD8)
168#define GPIO_PERIPH_ID7(port) MMIO32((port) + 0xFDC)
169#define GPIO_PERIPH_ID0(port) MMIO32((port) + 0xFE0)
170#define GPIO_PERIPH_ID1(port) MMIO32((port) + 0xFE4)
171#define GPIO_PERIPH_ID2(port) MMIO32((port) + 0xFE8)
172#define GPIO_PERIPH_ID3(port) MMIO32((port) + 0xFEC)
173
174/* GPIO PrimeCell Identification */
175#define GPIO_PCELL_ID0(port) MMIO32((port) + 0xFF0)
176#define GPIO_PCELL_ID1(port) MMIO32((port) + 0xFF4)
177#define GPIO_PCELL_ID2(port) MMIO32((port) + 0xFF8)
178#define GPIO_PCELL_ID3(port) MMIO32((port) + 0xFFC)
179
180/* =============================================================================
181 * Convenience enums
182 * ---------------------------------------------------------------------------*/
184 GPIO_MODE_OUTPUT, /**< Configure pin as output */
185 GPIO_MODE_INPUT, /**< Configure pin as input */
186 GPIO_MODE_ANALOG, /**< Configure pin as analog function */
187};
188
190 GPIO_PUPD_NONE, /**< Do not pull the pin high or low */
191 GPIO_PUPD_PULLUP, /**< Pull the pin high */
192 GPIO_PUPD_PULLDOWN, /**< Pull the pin low */
193};
194
196 GPIO_OTYPE_PP, /**< Push-pull configuration */
197 GPIO_OTYPE_OD, /**< Open drain configuration */
198};
199
201 GPIO_DRIVE_2MA, /**< 2mA drive */
202 GPIO_DRIVE_4MA, /**< 4mA drive */
203 GPIO_DRIVE_8MA, /**< 8mA drive */
204 GPIO_DRIVE_8MA_SLEW_CTL,/**< 8mA drive with slew rate control */
205};
206
208 GPIO_TRIG_LVL_LOW, /**< Level trigger, signal low */
209 GPIO_TRIG_LVL_HIGH, /**< Level trigger, signal high */
210 GPIO_TRIG_EDGE_FALL, /**< Falling edge trigger */
211 GPIO_TRIG_EDGE_RISE, /**< Rising edge trigger*/
212 GPIO_TRIG_EDGE_BOTH, /**< Falling and Rising edges trigger*/
213};
214/* =============================================================================
215 * Function prototypes
216 * ---------------------------------------------------------------------------*/
218
219void gpio_enable_ahb_aperture(void);
220void gpio_mode_setup(uint32_t gpioport, enum gpio_mode mode,
221 enum gpio_pullup pullup, uint8_t gpios);
222void gpio_set_output_config(uint32_t gpioport, enum gpio_output_type otype,
223 enum gpio_drive_strength drive, uint8_t gpios);
224void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint8_t gpios);
225
226void gpio_toggle(uint32_t gpioport, uint8_t gpios);
227void gpio_unlock_commit(uint32_t gpioport, uint8_t gpios);
228
229/* Let's keep these ones inlined. GPIO control should be fast */
230/** @ingroup gpio_control
231 * @{ */
232
233/**
234 * \brief Get status of a Group of Pins (atomic)
235 *
236 * Reads the level of the given pins. Bit 0 of the returned data corresponds to
237 * GPIO0 level, bit 1 to GPIO1 level. and so on. Bits corresponding to masked
238 * pins (corresponding bit of gpios parameter set to zero) are returned as 0.
239 *
240 * This is an atomic operation.
241 *
242 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
243 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
244 * by OR'ing then together.
245 *
246 * @return The level of the GPIO port. The pins not specified in gpios are
247 * masked to zero.
248 */
249static inline uint8_t gpio_read(uint32_t gpioport, uint8_t gpios)
250{
251 return GPIO_DATA(gpioport)[gpios];
252}
253
254/**
255 * \brief Set level of a Group of Pins (atomic)
256 *
257 * Sets the level of the given pins. Bit 0 of the data parameter corresponds to
258 * GPIO0, bit 1 to GPIO1. and so on. Maskedpins (corresponding bit of gpios
259 * parameter set to zero) are returned not affected.
260 *
261 * This is an atomic operation.
262 *
263 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
264 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
265 * by OR'ing then together.
266 * @param[in] data Level to set pin to. Bit 0 of data corresponds to GPIO0, bit
267 * 1 to GPIO1. and so on.
268 */
269static inline void gpio_write(uint32_t gpioport, uint8_t gpios, uint8_t data)
270{
271 /* ipaddr[9:2] mask the bits to be set, hence the array index */
272 GPIO_DATA(gpioport)[gpios] = data;
273}
274
275/**
276 * \brief Set a Group of Pins (atomic)
277 *
278 * Set one or more pins of the given GPIO port. This is an atomic operation.
279 *
280 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
281 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
282 * by OR'ing then together.
283 */
284static inline void gpio_set(uint32_t gpioport, uint8_t gpios)
285{
286 gpio_write(gpioport, gpios, 0xff);
287}
288
289/**
290 * \brief Clear a Group of Pins (atomic)
291 *
292 * Clear one or more pins of the given GPIO port. This is an atomic operation.
293 *
294 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
295 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
296 * by OR'ing then together.
297 */
298static inline void gpio_clear(uint32_t gpioport, uint8_t gpios)
299{
300 gpio_write(gpioport, gpios, 0);
301}
302
303/**
304 * \brief Read level of all pins from a port (atomic)
305 *
306 * Read the current value of the given GPIO port. This is an atomic operation.
307 *
308 * This is functionally identical to @ref gpio_read (gpioport, GPIO_ALL).
309 *
310 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
311 *
312 * @return The level of all the pins on the GPIO port.
313 */
314static inline uint8_t gpio_port_read(uint32_t gpioport)
315{
316 return gpio_read(gpioport, GPIO_ALL);
317}
318
319/**
320 * \brief Set level of of all pins from a port (atomic)
321 *
322 * Set the level of all pins on the given GPIO port. This is an atomic
323 * operation.
324 *
325 * This is functionally identical to @ref gpio_write (gpioport, GPIO_ALL, data).
326 *
327 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
328 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
329 * by OR'ing then together.
330 * @param[in] data Level to set pin to. Bit 0 of data corresponds to GPIO0, bit
331 * 1 to GPIO1. and so on.
332 */
333static inline void gpio_port_write(uint32_t gpioport, uint8_t data)
334{
335 gpio_write(gpioport, GPIO_ALL, data);
336}
337/** @} */
338
339void gpio_configure_trigger(uint32_t gpioport, enum gpio_trigger trigger,
340 uint8_t gpios);
341void gpio_enable_interrupts(uint32_t gpioport, uint8_t gpios);
342void gpio_disable_interrupts(uint32_t gpioport, uint8_t gpios);
343
344
345/* Let's keep these ones inlined. GPIO. They are designed to be used in ISRs */
346/** @ingroup gpio_irq
347 * @{ */
348/** \brief Determine if interrupt is generated by the given pin
349 *
350 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
351 * @param[in] srcpins source pin or group of pins to check.
352 */
353static inline bool gpio_is_interrupt_source(uint32_t gpioport, uint8_t srcpins)
354{
355 return GPIO_MIS(gpioport) & srcpins;
356}
357
358/**
359 * \brief Mark interrupt as serviced
360 *
361 * After an interrupt is services, its flag must be cleared. If the flag is not
362 * cleared, then execution will jump back to the start of the ISR after the ISR
363 * returns.
364 *
365 * @param[in] gpioport GPIO block register address base @ref gpio_reg_base
366 * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified
367 * by OR'ing then together.
368 */
369static inline void gpio_clear_interrupt_flag(uint32_t gpioport, uint8_t gpios)
370{
371 GPIO_ICR(gpioport) |= gpios;
372}
373
374/** @} */
376
377#endif
378
379/**@}*/
380
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
static void gpio_port_write(uint32_t gpioport, uint8_t data)
Set level of of all pins from a port (atomic)
Definition: gpio.h:333
static uint8_t gpio_read(uint32_t gpioport, uint8_t gpios)
Get status of a Group of Pins (atomic)
Definition: gpio.h:249
static void gpio_write(uint32_t gpioport, uint8_t gpios, uint8_t data)
Set level of a Group of Pins (atomic)
Definition: gpio.h:269
static void gpio_set(uint32_t gpioport, uint8_t gpios)
Set a Group of Pins (atomic)
Definition: gpio.h:284
static uint8_t gpio_port_read(uint32_t gpioport)
Read level of all pins from a port (atomic)
Definition: gpio.h:314
static void gpio_clear(uint32_t gpioport, uint8_t gpios)
Clear a Group of Pins (atomic)
Definition: gpio.h:298
void gpio_mode_setup(uint32_t gpioport, enum gpio_mode mode, enum gpio_pullup pullup, uint8_t gpios)
Configure a group of pins.
Definition: gpio.c:209
void gpio_unlock_commit(uint32_t gpioport, uint8_t gpios)
Unlock the commit control of a special function pin.
Definition: gpio.c:365
void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint8_t gpios)
Multiplex group of pins to the given alternate function.
Definition: gpio.c:321
#define GPIO_MIS(port)
Definition: gpio.h:114
gpio_drive_strength
Definition: gpio.h:200
gpio_pullup
Definition: gpio.h:189
gpio_mode
Definition: gpio.h:183
void gpio_enable_interrupts(uint32_t gpioport, uint8_t gpios)
Enable interrupts on specified GPIO pins.
Definition: gpio.c:572
void gpio_configure_trigger(uint32_t gpioport, enum gpio_trigger trigger, uint8_t gpios)
Configure the interrupt trigger on the given GPIO pins.
Definition: gpio.c:527
#define GPIO_ICR(port)
Definition: gpio.h:117
void gpio_set_output_config(uint32_t gpioport, enum gpio_output_type otype, enum gpio_drive_strength drive, uint8_t gpios)
Configure output parameters of a group of pins.
Definition: gpio.c:270
void gpio_enable_ahb_aperture(void)
Enable access to GPIO registers via the AHB aperture.
Definition: gpio.c:186
void gpio_disable_interrupts(uint32_t gpioport, uint8_t gpios)
Disable interrupts on specified GPIO pins.
Definition: gpio.c:590
void gpio_toggle(uint32_t gpioport, uint8_t gpios)
Toggle a Group of Pins.
Definition: gpio.c:441
gpio_output_type
Definition: gpio.h:195
#define GPIO_DATA(port)
Definition: gpio.h:93
gpio_trigger
Definition: gpio.h:207
@ GPIO_DRIVE_8MA_SLEW_CTL
8mA drive with slew rate control
Definition: gpio.h:204
@ GPIO_DRIVE_8MA
8mA drive
Definition: gpio.h:203
@ GPIO_DRIVE_2MA
2mA drive
Definition: gpio.h:201
@ GPIO_DRIVE_4MA
4mA drive
Definition: gpio.h:202
@ GPIO_PUPD_PULLUP
Pull the pin high.
Definition: gpio.h:191
@ GPIO_PUPD_PULLDOWN
Pull the pin low.
Definition: gpio.h:192
@ GPIO_PUPD_NONE
Do not pull the pin high or low.
Definition: gpio.h:190
@ GPIO_MODE_ANALOG
Configure pin as analog function.
Definition: gpio.h:186
@ GPIO_MODE_INPUT
Configure pin as input.
Definition: gpio.h:185
@ GPIO_MODE_OUTPUT
Configure pin as output.
Definition: gpio.h:184
@ GPIO_OTYPE_OD
Open drain configuration.
Definition: gpio.h:197
@ GPIO_OTYPE_PP
Push-pull configuration.
Definition: gpio.h:196
@ GPIO_TRIG_EDGE_BOTH
Falling and Rising edges trigger.
Definition: gpio.h:212
@ GPIO_TRIG_EDGE_FALL
Falling edge trigger.
Definition: gpio.h:210
@ GPIO_TRIG_LVL_LOW
Level trigger, signal low.
Definition: gpio.h:208
@ GPIO_TRIG_LVL_HIGH
Level trigger, signal high.
Definition: gpio.h:209
@ GPIO_TRIG_EDGE_RISE
Rising edge trigger.
Definition: gpio.h:211
static void gpio_clear_interrupt_flag(uint32_t gpioport, uint8_t gpios)
Mark interrupt as serviced.
Definition: gpio.h:369
static bool gpio_is_interrupt_source(uint32_t gpioport, uint8_t srcpins)
Determine if interrupt is generated by the given pin.
Definition: gpio.h:353
#define GPIO_ALL
Definition: gpio.h:85