libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
l1/gpio.h
Go to the documentation of this file.
1/** @defgroup gpio_defines GPIO Defines
2
3@brief <b>Defined Constants and Types for the STM32L1xx General Purpose I/O</b>
4
5@ingroup STM32L1xx_defines
6
7@version 1.0.0
8
9@date 1 July 2012
10
11LGPL License Terms @ref lgpl_license
12 */
13/*
14 * This file is part of the libopencm3 project.
15 *
16 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
17 * Copyright (C) 2012 Piotr Esden-Tempski <piotr@esden.net>
18 * Copyright (C) 2012 Karl Palsson <karlp@tweak.net.au>
19 *
20 * This library is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU Lesser General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
24 *
25 * This library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU Lesser General Public License for more details.
29 *
30 * You should have received a copy of the GNU Lesser General Public License
31 * along with this library. If not, see <http://www.gnu.org/licenses/>.
32 */
33
34/**@{*/
35
36#ifndef LIBOPENCM3_GPIO_H
37#define LIBOPENCM3_GPIO_H
38
40
41/* --- Convenience macros -------------------------------------------------- */
42
43/* GPIO port base addresses (for convenience) */
44/* GPIO port base addresses (for convenience) */
45/** @defgroup gpio_port_id GPIO Port IDs
46@ingroup gpio_defines
47
48@{*/
49#define GPIOA GPIO_PORT_A_BASE
50#define GPIOB GPIO_PORT_B_BASE
51#define GPIOC GPIO_PORT_C_BASE
52#define GPIOD GPIO_PORT_D_BASE
53#define GPIOE GPIO_PORT_E_BASE
54#define GPIOH GPIO_PORT_H_BASE
55/**@}*/
56
57/* --- GPIO registers ------------------------------------------------------ */
58
59/* Port mode register (GPIOx_MODER) */
60#define GPIO_MODER(port) MMIO32((port) + 0x00)
61#define GPIOA_MODER GPIO_MODER(GPIOA)
62#define GPIOB_MODER GPIO_MODER(GPIOB)
63#define GPIOC_MODER GPIO_MODER(GPIOC)
64#define GPIOD_MODER GPIO_MODER(GPIOD)
65#define GPIOE_MODER GPIO_MODER(GPIOE)
66#define GPIOH_MODER GPIO_MODER(GPIOH)
67
68/* Port output type register (GPIOx_OTYPER) */
69#define GPIO_OTYPER(port) MMIO32((port) + 0x04)
70#define GPIOA_OTYPER GPIO_OTYPER(GPIOA)
71#define GPIOB_OTYPER GPIO_OTYPER(GPIOB)
72#define GPIOC_OTYPER GPIO_OTYPER(GPIOC)
73#define GPIOD_OTYPER GPIO_OTYPER(GPIOD)
74#define GPIOE_OTYPER GPIO_OTYPER(GPIOE)
75#define GPIOH_OTYPER GPIO_OTYPER(GPIOH)
76
77/* Port output speed register (GPIOx_OSPEEDR) */
78#define GPIO_OSPEEDR(port) MMIO32((port) + 0x08)
79#define GPIOA_OSPEEDR GPIO_OSPEEDR(GPIOA)
80#define GPIOB_OSPEEDR GPIO_OSPEEDR(GPIOB)
81#define GPIOC_OSPEEDR GPIO_OSPEEDR(GPIOC)
82#define GPIOD_OSPEEDR GPIO_OSPEEDR(GPIOD)
83#define GPIOE_OSPEEDR GPIO_OSPEEDR(GPIOE)
84#define GPIOH_OSPEEDR GPIO_OSPEEDR(GPIOH)
85
86/* Port pull-up/pull-down register (GPIOx_PUPDR) */
87#define GPIO_PUPDR(port) MMIO32((port) + 0x0c)
88#define GPIOA_PUPDR GPIO_PUPDR(GPIOA)
89#define GPIOB_PUPDR GPIO_PUPDR(GPIOB)
90#define GPIOC_PUPDR GPIO_PUPDR(GPIOC)
91#define GPIOD_PUPDR GPIO_PUPDR(GPIOD)
92#define GPIOE_PUPDR GPIO_PUPDR(GPIOE)
93#define GPIOH_PUPDR GPIO_PUPDR(GPIOH)
94
95/* Port input data register (GPIOx_IDR) */
96#define GPIO_IDR(port) MMIO32((port) + 0x10)
97#define GPIOA_IDR GPIO_IDR(GPIOA)
98#define GPIOB_IDR GPIO_IDR(GPIOB)
99#define GPIOC_IDR GPIO_IDR(GPIOC)
100#define GPIOD_IDR GPIO_IDR(GPIOD)
101#define GPIOE_IDR GPIO_IDR(GPIOE)
102#define GPIOH_IDR GPIO_IDR(GPIOH)
103
104/* Port output data register (GPIOx_ODR) */
105#define GPIO_ODR(port) MMIO32((port) + 0x14)
106#define GPIOA_ODR GPIO_ODR(GPIOA)
107#define GPIOB_ODR GPIO_ODR(GPIOB)
108#define GPIOC_ODR GPIO_ODR(GPIOC)
109#define GPIOD_ODR GPIO_ODR(GPIOD)
110#define GPIOE_ODR GPIO_ODR(GPIOE)
111#define GPIOH_ODR GPIO_ODR(GPIOH)
112
113/* Port bit set/reset register (GPIOx_BSRR) */
114#define GPIO_BSRR(port) MMIO32((port) + 0x18)
115#define GPIOA_BSRR GPIO_BSRR(GPIOA)
116#define GPIOB_BSRR GPIO_BSRR(GPIOB)
117#define GPIOC_BSRR GPIO_BSRR(GPIOC)
118#define GPIOD_BSRR GPIO_BSRR(GPIOD)
119#define GPIOE_BSRR GPIO_BSRR(GPIOE)
120#define GPIOH_BSRR GPIO_BSRR(GPIOH)
121
122/* Port configuration lock register (GPIOx_LCKR) */
123#define GPIO_LCKR(port) MMIO32((port) + 0x1C)
124#define GPIOA_LCKR GPIO_LCKR(GPIOA)
125#define GPIOB_LCKR GPIO_LCKR(GPIOB)
126#define GPIOC_LCKR GPIO_LCKR(GPIOC)
127#define GPIOD_LCKR GPIO_LCKR(GPIOD)
128#define GPIOE_LCKR GPIO_LCKR(GPIOE)
129#define GPIOH_LCKR GPIO_LCKR(GPIOH)
130
131/* Alternate function low register (GPIOx_AFRL) */
132#define GPIO_AFRL(port) MMIO32((port) + 0x20)
133#define GPIOA_AFRL GPIO_AFRL(GPIOA)
134#define GPIOB_AFRL GPIO_AFRL(GPIOB)
135#define GPIOC_AFRL GPIO_AFRL(GPIOC)
136#define GPIOD_AFRL GPIO_AFRL(GPIOD)
137#define GPIOE_AFRL GPIO_AFRL(GPIOE)
138#define GPIOH_AFRL GPIO_AFRL(GPIOH)
139
140/* Alternate function high register (GPIOx_AFRH) */
141#define GPIO_AFRH(port) MMIO32((port) + 0x24)
142#define GPIOA_AFRH GPIO_AFRH(GPIOA)
143#define GPIOB_AFRH GPIO_AFRH(GPIOB)
144#define GPIOC_AFRH GPIO_AFRH(GPIOC)
145#define GPIOD_AFRH GPIO_AFRH(GPIOD)
146#define GPIOE_AFRH GPIO_AFRH(GPIOE)
147#define GPIOH_AFRH GPIO_AFRH(GPIOH)
148
149/* --- GPIOx_MODER values-------------------------------------------- */
150
151#define GPIO_MODE(n, mode) ((mode) << (2 * (n)))
152#define GPIO_MODE_MASK(n) (0x3 << (2 * (n)))
153/** @defgroup gpio_mode GPIO Pin Direction and Analog/Digital Mode
154@ingroup gpio_defines
155@{*/
156#define GPIO_MODE_INPUT 0x00 /* Default */
157#define GPIO_MODE_OUTPUT 0x01
158#define GPIO_MODE_AF 0x02
159#define GPIO_MODE_ANALOG 0x03
160/**@}*/
161
162/* --- GPIOx_OTYPER values -------------------------------------------- */
163/* Output type (OTx values) */
164/** @defgroup gpio_output_type GPIO Output Pin Driver Type
165@ingroup gpio_defines
166@{*/
167/** Push-Pull */
168#define GPIO_OTYPE_PP 0x0
169/** Open Drain */
170#define GPIO_OTYPE_OD 0x1
171/**@}*/
172
173/* Output speed values */
174#define GPIO_OSPEED(n, speed) ((speed) << (2 * (n)))
175#define GPIO_OSPEED_MASK(n) (0x3 << (2 * (n)))
176/** @defgroup gpio_speed GPIO Output Pin Speed
177@ingroup gpio_defines
178@{*/
179#define GPIO_OSPEED_400KHZ 0x0
180#define GPIO_OSPEED_2MHZ 0x1
181#define GPIO_OSPEED_10MHZ 0x2
182#define GPIO_OSPEED_40MHZ 0x3
183/**@}*/
184
185/* --- GPIOx_PUPDR values ------------------------------------------- */
186
187#define GPIO_PUPD(n, pupd) ((pupd) << (2 * (n)))
188#define GPIO_PUPD_MASK(n) (0x3 << (2 * (n)))
189/** @defgroup gpio_pup GPIO Output Pin Pullup
190@ingroup gpio_defines
191@{*/
192#define GPIO_PUPD_NONE 0x0
193#define GPIO_PUPD_PULLUP 0x1
194#define GPIO_PUPD_PULLDOWN 0x2
195/**@}*/
196
197/* --- GPIO_IDR values ----------------------------------------------------- */
198
199/* GPIO_IDR[15:0]: IDRy[15:0]: Port input data (y = 0..15) */
200
201/* --- GPIO_ODR values ----------------------------------------------------- */
202
203/* GPIO_ODR[15:0]: ODRy[15:0]: Port output data (y = 0..15) */
204
205/* --- GPIO_BSRR values ---------------------------------------------------- */
206
207/* GPIO_BSRR[31:16]: BRy: Port x reset bit y (y = 0..15) */
208/* GPIO_BSRR[15:0]: BSy: Port x set bit y (y = 0..15) */
209
210/* --- GPIO_LCKR values ---------------------------------------------------- */
211
212#define GPIO_LCKK (1 << 16)
213/* GPIO_LCKR[15:0]: LCKy: Port x lock bit y (y = 0..15) */
214
215/* --- GPIOx_AFRL/H values ------------------------------------------------- */
216
217/* Note: AFRL is used for bits 0..7, AFRH is used for 8..15 */
218/* See datasheet table 5, page 35 for the definitions */
219
220#define GPIO_AFR(n, af) ((af) << ((n) * 4))
221#define GPIO_AFR_MASK(n) (0xf << ((n) * 4))
222/** @defgroup gpio_af_num Alternate Function Pin Selection
223@ingroup gpio_defines
224@{*/
225#define GPIO_AF0 0x0
226#define GPIO_AF1 0x1
227#define GPIO_AF2 0x2
228#define GPIO_AF3 0x3
229#define GPIO_AF4 0x4
230#define GPIO_AF5 0x5
231#define GPIO_AF6 0x6
232#define GPIO_AF7 0x7
233#define GPIO_AF8 0x8
234#define GPIO_AF9 0x9
235#define GPIO_AF10 0xa
236#define GPIO_AF11 0xb
237#define GPIO_AF12 0xc
238#define GPIO_AF13 0xd
239#define GPIO_AF14 0xe
240#define GPIO_AF15 0xf
241/**@}*/
242
243/* --- Function prototypes ------------------------------------------------- */
244
246
247/*
248 * L1, like F2 and F4, has the "new" GPIO peripheral, so use that style
249 * however the number of ports is reduced and H port naming is different.
250 * TODO: this should all really be moved to a "common" gpio header
251 */
252
253void gpio_mode_setup(uint32_t gpioport, uint8_t mode, uint8_t pull_up_down,
254 uint16_t gpios);
255void gpio_set_output_options(uint32_t gpioport, uint8_t otype, uint8_t speed,
256 uint16_t gpios);
257void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint16_t gpios);
258
260
261#endif
262/**@}*/
263
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint16_t gpios)
Set GPIO Alternate Function Selection.
void gpio_mode_setup(uint32_t gpioport, uint8_t mode, uint8_t pull_up_down, uint16_t gpios)
Set GPIO Pin Mode.
void gpio_set_output_options(uint32_t gpioport, uint8_t otype, uint8_t speed, uint16_t gpios)
Set GPIO Output Options.