libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
gpio_common_f234.h
Go to the documentation of this file.
1 /** @addtogroup gpio_defines
2  *
3  * @author @htmlonly © @endhtmlonly 2011
4  * Fergus Noble <fergusnoble@gmail.com>
5  * @author @htmlonly &copy; @endhtmlonly 2012
6  * Ken Sarkies <ksarkies@internode.on.net>
7  *
8  */
9 /*
10  * This file is part of the libopencm3 project.
11  *
12  * Copyright (C) 2011 Fergus Noble <fergusnoble@gmail.com>
13  * Copyright (C) 2012 Ken Sarkies <ksarkies@internode.on.net>
14  *
15  * This library is free software: you can redistribute it and/or modify
16  * it under the terms of the GNU Lesser General Public License as published by
17  * the Free Software Foundation, either version 3 of the License, or
18  * (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23  * GNU Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public License
26  * along with this library. If not, see <http://www.gnu.org/licenses/>.
27  */
28 
29 /* THIS FILE SHOULD NOT BE INCLUDED DIRECTLY, BUT ONLY VIA GPIO.H
30 The order of header inclusion is important. gpio.h includes the device
31 specific memorymap.h header before including this header file.*/
32 
33 /** @cond */
34 #ifdef LIBOPENCM3_GPIO_H
35 /** @endcond */
36 #ifndef LIBOPENCM3_GPIO_COMMON_F234_H
37 #define LIBOPENCM3_GPIO_COMMON_F234_H
38 
39 /**@{*/
40 
42 
43 /* GPIO port base addresses (for convenience) */
44 /** @defgroup gpio_port_id GPIO Port IDs
45 @ingroup gpio_defines
46 
47 @{*/
48 #define GPIOA GPIO_PORT_A_BASE
49 #define GPIOB GPIO_PORT_B_BASE
50 #define GPIOC GPIO_PORT_C_BASE
51 #define GPIOD GPIO_PORT_D_BASE
52 #define GPIOE GPIO_PORT_E_BASE
53 #define GPIOF GPIO_PORT_F_BASE
54 #define GPIOG GPIO_PORT_G_BASE
55 #define GPIOH GPIO_PORT_H_BASE
56 
57 /**@}*/
58 
59 /* --- GPIO registers for STM32F2, STM32F3 and STM32F4 --------------------- */
60 
61 /* Port mode register (GPIOx_MODER) */
62 #define GPIO_MODER(port) MMIO32((port) + 0x00)
63 #define GPIOA_MODER GPIO_MODER(GPIOA)
64 #define GPIOB_MODER GPIO_MODER(GPIOB)
65 #define GPIOC_MODER GPIO_MODER(GPIOC)
66 #define GPIOD_MODER GPIO_MODER(GPIOD)
67 #define GPIOE_MODER GPIO_MODER(GPIOE)
68 #define GPIOF_MODER GPIO_MODER(GPIOF)
69 #define GPIOG_MODER GPIO_MODER(GPIOG)
70 #define GPIOH_MODER GPIO_MODER(GPIOH)
71 
72 /* Port output type register (GPIOx_OTYPER) */
73 #define GPIO_OTYPER(port) MMIO32((port) + 0x04)
74 #define GPIOA_OTYPER GPIO_OTYPER(GPIOA)
75 #define GPIOB_OTYPER GPIO_OTYPER(GPIOB)
76 #define GPIOC_OTYPER GPIO_OTYPER(GPIOC)
77 #define GPIOD_OTYPER GPIO_OTYPER(GPIOD)
78 #define GPIOE_OTYPER GPIO_OTYPER(GPIOE)
79 #define GPIOF_OTYPER GPIO_OTYPER(GPIOF)
80 #define GPIOG_OTYPER GPIO_OTYPER(GPIOG)
81 #define GPIOH_OTYPER GPIO_OTYPER(GPIOH)
82 
83 /* Port output speed register (GPIOx_OSPEEDR) */
84 #define GPIO_OSPEEDR(port) MMIO32((port) + 0x08)
85 #define GPIOA_OSPEEDR GPIO_OSPEEDR(GPIOA)
86 #define GPIOB_OSPEEDR GPIO_OSPEEDR(GPIOB)
87 #define GPIOC_OSPEEDR GPIO_OSPEEDR(GPIOC)
88 #define GPIOD_OSPEEDR GPIO_OSPEEDR(GPIOD)
89 #define GPIOE_OSPEEDR GPIO_OSPEEDR(GPIOE)
90 #define GPIOF_OSPEEDR GPIO_OSPEEDR(GPIOF)
91 #define GPIOG_OSPEEDR GPIO_OSPEEDR(GPIOG)
92 #define GPIOH_OSPEEDR GPIO_OSPEEDR(GPIOH)
93 
94 /* Port pull-up/pull-down register (GPIOx_PUPDR) */
95 #define GPIO_PUPDR(port) MMIO32((port) + 0x0c)
96 #define GPIOA_PUPDR GPIO_PUPDR(GPIOA)
97 #define GPIOB_PUPDR GPIO_PUPDR(GPIOB)
98 #define GPIOC_PUPDR GPIO_PUPDR(GPIOC)
99 #define GPIOD_PUPDR GPIO_PUPDR(GPIOD)
100 #define GPIOE_PUPDR GPIO_PUPDR(GPIOE)
101 #define GPIOF_PUPDR GPIO_PUPDR(GPIOF)
102 #define GPIOG_PUPDR GPIO_PUPDR(GPIOG)
103 #define GPIOH_PUPDR GPIO_PUPDR(GPIOH)
104 
105 /* Port input data register (GPIOx_IDR) */
106 #define GPIO_IDR(port) MMIO32((port) + 0x10)
107 #define GPIOA_IDR GPIO_IDR(GPIOA)
108 #define GPIOB_IDR GPIO_IDR(GPIOB)
109 #define GPIOC_IDR GPIO_IDR(GPIOC)
110 #define GPIOD_IDR GPIO_IDR(GPIOD)
111 #define GPIOE_IDR GPIO_IDR(GPIOE)
112 #define GPIOF_IDR GPIO_IDR(GPIOF)
113 #define GPIOG_IDR GPIO_IDR(GPIOG)
114 #define GPIOH_IDR GPIO_IDR(GPIOH)
115 
116 /* Port output data register (GPIOx_ODR) */
117 #define GPIO_ODR(port) MMIO32((port) + 0x14)
118 #define GPIOA_ODR GPIO_ODR(GPIOA)
119 #define GPIOB_ODR GPIO_ODR(GPIOB)
120 #define GPIOC_ODR GPIO_ODR(GPIOC)
121 #define GPIOD_ODR GPIO_ODR(GPIOD)
122 #define GPIOE_ODR GPIO_ODR(GPIOE)
123 #define GPIOF_ODR GPIO_ODR(GPIOF)
124 #define GPIOG_ODR GPIO_ODR(GPIOG)
125 #define GPIOH_ODR GPIO_ODR(GPIOH)
126 
127 /* Port bit set/reset register (GPIOx_BSRR) */
128 #define GPIO_BSRR(port) MMIO32((port) + 0x18)
129 #define GPIOA_BSRR GPIO_BSRR(GPIOA)
130 #define GPIOB_BSRR GPIO_BSRR(GPIOB)
131 #define GPIOC_BSRR GPIO_BSRR(GPIOC)
132 #define GPIOD_BSRR GPIO_BSRR(GPIOD)
133 #define GPIOE_BSRR GPIO_BSRR(GPIOE)
134 #define GPIOF_BSRR GPIO_BSRR(GPIOF)
135 #define GPIOG_BSRR GPIO_BSRR(GPIOG)
136 #define GPIOH_BSRR GPIO_BSRR(GPIOH)
137 
138 /* Port configuration lock register (GPIOx_LCKR) */
139 #define GPIO_LCKR(port) MMIO32((port) + 0x1c)
140 #define GPIOA_LCKR GPIO_LCKR(GPIOA)
141 #define GPIOB_LCKR GPIO_LCKR(GPIOB)
142 #define GPIOC_LCKR GPIO_LCKR(GPIOC)
143 #define GPIOD_LCKR GPIO_LCKR(GPIOD)
144 #define GPIOE_LCKR GPIO_LCKR(GPIOE)
145 #define GPIOF_LCKR GPIO_LCKR(GPIOF)
146 #define GPIOG_LCKR GPIO_LCKR(GPIOG)
147 #define GPIOH_LCKR GPIO_LCKR(GPIOH)
148 
149 /* Alternate function low register (GPIOx_AFRL) */
150 #define GPIO_AFRL(port) MMIO32((port) + 0x20)
151 #define GPIOA_AFRL GPIO_AFRL(GPIOA)
152 #define GPIOB_AFRL GPIO_AFRL(GPIOB)
153 #define GPIOC_AFRL GPIO_AFRL(GPIOC)
154 #define GPIOD_AFRL GPIO_AFRL(GPIOD)
155 #define GPIOE_AFRL GPIO_AFRL(GPIOE)
156 #define GPIOF_AFRL GPIO_AFRL(GPIOF)
157 #define GPIOG_AFRL GPIO_AFRL(GPIOG)
158 #define GPIOH_AFRL GPIO_AFRL(GPIOH)
159 
160 /* Alternate function high register (GPIOx_AFRH) */
161 #define GPIO_AFRH(port) MMIO32((port) + 0x24)
162 #define GPIOA_AFRH GPIO_AFRH(GPIOA)
163 #define GPIOB_AFRH GPIO_AFRH(GPIOB)
164 #define GPIOC_AFRH GPIO_AFRH(GPIOC)
165 #define GPIOD_AFRH GPIO_AFRH(GPIOD)
166 #define GPIOE_AFRH GPIO_AFRH(GPIOE)
167 #define GPIOF_AFRH GPIO_AFRH(GPIOF)
168 #define GPIOG_AFRH GPIO_AFRH(GPIOG)
169 #define GPIOH_AFRH GPIO_AFRH(GPIOH)
170 
171 /* --- GPIOx_MODER values -------------------------------------------------- */
172 
173 #define GPIO_MODE(n, mode) ((mode) << (2 * (n)))
174 #define GPIO_MODE_MASK(n) (0x3 << (2 * (n)))
175 /** @defgroup gpio_mode GPIO Pin Direction and Analog/Digital Mode
176 @ingroup gpio_defines
177 @{*/
178 #define GPIO_MODE_INPUT 0x0
179 #define GPIO_MODE_OUTPUT 0x1
180 #define GPIO_MODE_AF 0x2
181 #define GPIO_MODE_ANALOG 0x3
182 /**@}*/
183 
184 /* --- GPIOx_OTYPER values ------------------------------------------------- */
185 
186 /** @defgroup gpio_output_type GPIO Output Pin Driver Type
187 @ingroup gpio_defines
188 @{*/
189 /** Push Pull */
190 #define GPIO_OTYPE_PP 0x0
191 /** Open Drain */
192 #define GPIO_OTYPE_OD 0x1
193 /**@}*/
194 
195 /* --- GPIOx_OSPEEDR values ------------------------------------------------ */
196 
197 #define GPIO_OSPEED(n, speed) ((speed) << (2 * (n)))
198 #define GPIO_OSPEED_MASK(n) (0x3 << (2 * (n)))
199 /** @defgroup gpio_speed GPIO Output Pin Speed
200 @ingroup gpio_defines
201 @{*/
202 #define GPIO_OSPEED_2MHZ 0x0
203 #define GPIO_OSPEED_25MHZ 0x1
204 #define GPIO_OSPEED_50MHZ 0x2
205 #define GPIO_OSPEED_100MHZ 0x3
206 /**@}*/
207 
208 /* --- GPIOx_PUPDR values -------------------------------------------------- */
209 
210 #define GPIO_PUPD(n, pupd) ((pupd) << (2 * (n)))
211 #define GPIO_PUPD_MASK(n) (0x3 << (2 * (n)))
212 /** @defgroup gpio_pup GPIO Output Pin Pullup
213 @ingroup gpio_defines
214 @{*/
215 #define GPIO_PUPD_NONE 0x0
216 #define GPIO_PUPD_PULLUP 0x1
217 #define GPIO_PUPD_PULLDOWN 0x2
218 /**@}*/
219 
220 /* --- GPIOx_IDR values ---------------------------------------------------- */
221 
222 /* GPIOx_IDR[15:0]: IDRy[15:0]: Port input data (y = 0..15) */
223 
224 /* --- GPIOx_ODR values ---------------------------------------------------- */
225 
226 /* GPIOx_ODR[15:0]: ODRy[15:0]: Port output data (y = 0..15) */
227 
228 /* --- GPIOx_BSRR values --------------------------------------------------- */
229 
230 /* GPIOx_BSRR[31:16]: BRy: Port x reset bit y (y = 0..15) */
231 /* GPIOx_BSRR[15:0]: BSy: Port x set bit y (y = 0..15) */
232 
233 /* --- GPIOx_LCKR values --------------------------------------------------- */
234 
235 #define GPIO_LCKK (1 << 16)
236 /* GPIOx_LCKR[15:0]: LCKy: Port x lock bit y (y = 0..15) */
237 
238 /* --- GPIOx_AFRL/H values ------------------------------------------------- */
239 
240 /* Note: AFRL is used for bits 0..7, AFRH is used for 8..15 */
241 /* See datasheet table 6 (pg. 48) for alternate function mappings. */
242 
243 #define GPIO_AFR(n, af) ((af) << ((n) * 4))
244 #define GPIO_AFR_MASK(n) (0xf << ((n) * 4))
245 /** @defgroup gpio_af_num Alternate Function Pin Selection
246 @ingroup gpio_defines
247 @{*/
248 #define GPIO_AF0 0x0
249 #define GPIO_AF1 0x1
250 #define GPIO_AF2 0x2
251 #define GPIO_AF3 0x3
252 #define GPIO_AF4 0x4
253 #define GPIO_AF5 0x5
254 #define GPIO_AF6 0x6
255 #define GPIO_AF7 0x7
256 #define GPIO_AF8 0x8
257 #define GPIO_AF9 0x9
258 #define GPIO_AF10 0xa
259 #define GPIO_AF11 0xb
260 #define GPIO_AF12 0xc
261 #define GPIO_AF13 0xd
262 #define GPIO_AF14 0xe
263 #define GPIO_AF15 0xf
264 /**@}*/
265 
266 /* Note: EXTI source selection is now in the SYSCFG peripheral. */
267 
268 /* --- Function prototypes ------------------------------------------------- */
269 
271 
272 /*
273  * Note: The F2 and F4 series have a completely new GPIO peripheral with
274  * different configuration options. Here we implement a different API partly to
275  * more closely match the peripheral capabilities and also to deliberately
276  * break compatibility with old F1 code so there is no confusion with similar
277  * sounding functions that have very different functionality.
278  */
279 
280 void gpio_mode_setup(uint32_t gpioport, uint8_t mode, uint8_t pull_up_down,
281  uint16_t gpios);
282 void gpio_set_output_options(uint32_t gpioport, uint8_t otype, uint8_t speed,
283  uint16_t gpios);
284 void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint16_t gpios);
285 
286 END_DECLS
287 /**@}*/
288 #endif
289 /** @cond */
290 #else
291 #warning "gpio_common_f234.h should not be included explicitly, only via gpio.h"
292 #endif
293 /** @endcond */
294 
#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.