cpu_gpio.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016 Freie Universität Berlin
3  * SPDX-FileCopyrightText: 2017 OTA keys S.A.
4  * SPDX-License-Identifier: LGPL-2.1-only
5  */
6 
7 #pragma once
8 
20 #include <stdint.h>
21 #include "cpu.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #ifndef DOXYGEN
32 #define HAVE_GPIO_T
33 typedef uint32_t gpio_t;
35 #endif
36 
40 #define GPIO_UNDEF (0xffffffff)
41 
45 #if defined(CPU_FAM_STM32MP1)
46 #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 12)) | y)
47 #else
48 #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y)
49 #endif
50 
54 enum {
55 #ifdef GPIOA
56  PORT_A = 0,
57 #endif
58 #ifdef GPIOB
59  PORT_B = 1,
60 #endif
61 #ifdef GPIOC
62  PORT_C = 2,
63 #endif
64 #ifdef GPIOD
65  PORT_D = 3,
66 #endif
67 #ifdef GPIOE
68  PORT_E = 4,
69 #endif
70 #ifdef GPIOF
71  PORT_F = 5,
72 #endif
73 #ifdef GPIOG
74  PORT_G = 6,
75 #endif
76 #ifdef GPIOH
77  PORT_H = 7,
78 #endif
79 #ifdef GPIOI
80  PORT_I = 8,
81 #endif
82 #ifdef GPIOJ
83  PORT_J = 9,
84 #endif
85 #ifdef GPIOK
86  PORT_K = 10,
87 #endif
88 };
89 
93 typedef enum {
94 #ifdef CPU_FAM_STM32F1
95  GPIO_AF_OUT_PP = 0xb,
96  GPIO_AF_OUT_OD = 0xf,
97 #else
98  GPIO_AF0 = 0,
106 #ifndef CPU_FAM_STM32F0
115 #endif
116 #endif
120 
121 #ifdef CPU_FAM_STM32F1
122 #ifndef DOXYGEN
132 #define GPIO_MODE(mode, cnf, odr) (mode | (cnf << 2) | (odr << 4))
133 
140 #define HAVE_GPIO_MODE_T
141 typedef enum {
142  GPIO_IN = GPIO_MODE(0, 1, 0),
143  GPIO_IN_PD = GPIO_MODE(0, 2, 0),
144  GPIO_IN_PU = GPIO_MODE(0, 2, 1),
145  GPIO_OUT = GPIO_MODE(3, 0, 0),
146  GPIO_OD = GPIO_MODE(3, 1, 0),
147  GPIO_OD_PU = (0xff)
148 } gpio_mode_t;
150 #endif /* ndef DOXYGEN */
151 
156 #define HAVE_GPIO_PP_T
157 typedef enum {
158  GPIO_NOPULL = 4,
159  GPIO_PULLUP = 9,
160  GPIO_PULLDOWN = 8
161 } gpio_pp_t;
163 #else /* CPU_FAM_STM32F1 */
172 #define GPIO_MODE(io, pr, ot) ((io << 0) | (pr << 2) | (ot << 4))
173 
174 #ifndef DOXYGEN
179 #define HAVE_GPIO_MODE_T
180 typedef enum {
181  GPIO_IN = GPIO_MODE(0, 0, 0),
182  GPIO_IN_PD = GPIO_MODE(0, 2, 0),
183  GPIO_IN_PU = GPIO_MODE(0, 1, 0),
184  GPIO_OUT = GPIO_MODE(1, 0, 0),
185  GPIO_OD = GPIO_MODE(1, 0, 1),
186  GPIO_OD_PU = GPIO_MODE(1, 1, 1)
187 } gpio_mode_t;
189 #endif /* ndef DOXYGEN */
190 #endif /* ndef CPU_FAM_STM32F1 */
191 
192 #ifndef DOXYGEN
197 #define HAVE_GPIO_FLANK_T
198 typedef enum {
199  GPIO_RISING = 1,
200  GPIO_FALLING = 2,
201  GPIO_BOTH = 3
202 } gpio_flank_t;
204 #endif /* ndef DOXYGEN */
205 
212 void gpio_init_af(gpio_t pin, gpio_af_t af);
213 
219 void gpio_init_analog(gpio_t pin);
220 
221 #ifdef __cplusplus
222 }
223 #endif
224 
@ PORT_B
port B
Definition: periph_cpu.h:44
@ PORT_G
port G
Definition: periph_cpu.h:49
@ PORT_C
port C
Definition: periph_cpu.h:45
@ PORT_F
port F
Definition: periph_cpu.h:48
@ PORT_E
port E
Definition: periph_cpu.h:47
@ PORT_A
port A
Definition: periph_cpu.h:43
@ PORT_D
port D
Definition: periph_cpu.h:46
@ PORT_K
port K
Definition: periph_cpu.h:50
@ PORT_H
port H
Definition: periph_cpu.h:48
@ PORT_J
port J
Definition: periph_cpu.h:49
gpio_flank_t
Definition: periph_cpu.h:176
@ GPIO_OUT
select GPIO MASK as output
Definition: periph_cpu.h:161
@ GPIO_IN
select GPIO MASK as input
Definition: periph_cpu.h:160
gpio_af_t
Available MUX values for configuring a pin's alternate function.
Definition: cpu_gpio.h:93
@ GPIO_AF1
use alternate function 1
Definition: cpu_gpio.h:99
@ GPIO_AF2
use alternate function 2
Definition: cpu_gpio.h:100
@ GPIO_AF5
use alternate function 5
Definition: cpu_gpio.h:103
@ GPIO_AF15
use alternate function 15
Definition: cpu_gpio.h:114
@ GPIO_AF4
use alternate function 4
Definition: cpu_gpio.h:102
@ GPIO_AF8
use alternate function 8
Definition: cpu_gpio.h:107
@ GPIO_AF6
use alternate function 6
Definition: cpu_gpio.h:104
@ GPIO_AF10
use alternate function 10
Definition: cpu_gpio.h:109
@ GPIO_AF9
use alternate function 9
Definition: cpu_gpio.h:108
@ GPIO_AF14
use alternate function 14
Definition: cpu_gpio.h:113
@ GPIO_AF3
use alternate function 3
Definition: cpu_gpio.h:101
@ GPIO_AF_UNDEF
an UNDEF value definition, e.g.
Definition: cpu_gpio.h:117
@ GPIO_AF0
use alternate function 0
Definition: cpu_gpio.h:98
@ GPIO_AF12
use alternate function 12
Definition: cpu_gpio.h:111
@ GPIO_AF7
use alternate function 7
Definition: cpu_gpio.h:105
@ GPIO_AF13
use alternate function 13
Definition: cpu_gpio.h:112
@ GPIO_AF11
use alternate function 11
Definition: cpu_gpio.h:110
#define GPIO_MODE(io, pr, ot)
Generate GPIO mode bitfields.
Definition: cpu_gpio.h:172
void gpio_init_af(gpio_t pin, gpio_af_t af)
Configure the alternate function for the given pin.
void gpio_init_analog(gpio_t pin)
Configure the given pin to be used as ADC input.
gpio_af_t
Override alternative GPIO mode options.
Definition: periph_cpu.h:162
@ GPIO_AF_OUT_OD
alternate function output - open-drain
Definition: periph_cpu.h:164
@ GPIO_AF_OUT_PP
alternate function output - push-pull
Definition: periph_cpu.h:163
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:88
unsigned int gpio_t
GPIO type identifier.
Definition: gpio.h:91
@ GPIO_OD
configure as output in open-drain mode without pull resistor
Definition: gpio.h:123
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:121
@ GPIO_OD_PU
configure as output in open-drain mode with pull resistor enabled
Definition: gpio.h:125
@ GPIO_IN_PD
configure as input with pull-down resistor
Definition: gpio.h:120