BSB Developer Docs
Loading...
Searching...
No Matches
f21/furi_hal/furi_hal_gpio.h
1#pragma once
2#include "stdbool.h"
3#include <stm32u5xx_ll_gpio.h>
4#include <stm32u5xx_safe.h>
5// #include <stm32u5xx_ll_system.h>
6// #include <stm32u5xx_ll_exti.h>
7
8#ifdef __cplusplus
9extern "C" {
10#endif
11
15#define GPIO_NUMBER (16U)
16
20typedef void (*GpioExtiCallback)(void* ctx);
21
25typedef struct {
26 GpioExtiCallback callback;
27 void* context;
29
33typedef enum {
34 GpioModeInput,
35 GpioModeOutputPushPull,
36 GpioModeOutputOpenDrain,
37 GpioModeAltFunctionPushPull,
38 GpioModeAltFunctionOpenDrain,
39 GpioModeAnalog,
40 GpioModeInterruptRise,
41 GpioModeInterruptFall,
42 GpioModeInterruptRiseFall,
43 GpioModeEventRise,
44 GpioModeEventFall,
45 GpioModeEventRiseFall,
46} GpioMode;
47
51typedef enum {
52 GpioPullNo,
53 GpioPullUp,
54 GpioPullDown,
55} GpioPull;
56
60typedef enum {
61 GpioSpeedLow,
62 GpioSpeedMedium,
63 GpioSpeedHigh,
64 GpioSpeedVeryHigh,
65} GpioSpeed;
66
72typedef enum {
73 GpioAltFn0RTC_50HZ = 0x00,
74 GpioAltFn0MCO = 0x00,
75 GpioAltFn0SWJ = 0x00,
76 GpioAltFn0TRACE = 0x00,
77 GpioAltFn0LPTIM1 = 0x00,
78 GpioAltFn0CSLEEP = 0x00,
79 GpioAltFn0CSTOP = 0x00,
80 GpioAltFn0CRS = 0x00,
81 GpioAltFn0SRDSTOP = 0x00,
83 GpioAltFn1TIM1 = 0x01,
84 GpioAltFn1TIM2 = 0x01,
85 GpioAltFn1TIM5 = 0x01,
86 GpioAltFn1TIM8 = 0x01,
87 GpioAltFn1LPTIM1 = 0x01,
88 GpioAltFn1IR = 0x01,
90 GpioAltFn2TIM1 = 0x02,
91 GpioAltFn2TIM2 = 0x02,
92 GpioAltFn2TIM3 = 0x02,
93 GpioAltFn2TIM4 = 0x02,
94 GpioAltFn2TIM5 = 0x02,
95 GpioAltFn2LPTIM1 = 0x02,
96 GpioAltFn2LPTIM2 = 0x02,
97 GpioAltFn2LPTIM3 = 0x02,
98 GpioAltFn2I2C5 = 0x02,
99 GpioAltFn2I2C6 = 0x02,
100 GpioAltFn2GFXTIM = 0x02,
102 GpioAltFn3I2C4 = 0x03,
103 GpioAltFn3OCTOSPI1 = 0x03,
104 GpioAltFn3SAI1 = 0x03,
105 GpioAltFn3SPI2 = 0x03,
106 GpioAltFn3TIM1 = 0x03,
107 GpioAltFn3TIM8 = 0x03,
108 GpioAltFn3TIM8_COMP1 = 0x03,
109 GpioAltFn3TIM8_COMP2 = 0x03,
110 GpioAltFn3TIM1_COMP1 = 0x03,
111 GpioAltFn3TIM1_COMP2 = 0x03,
112 GpioAltFn3USART2 = 0x03,
113 GpioAltFn3ADF1 = 0x03,
114 GpioAltFn3USB_HS = 0x03,
116 GpioAltFn4I2C1 = 0x04,
117 GpioAltFn4I2C2 = 0x04,
118 GpioAltFn4I2C3 = 0x04,
119 GpioAltFn4I2C4 = 0x04,
120 GpioAltFn4PSSI = 0x04,
121 GpioAltFn4DCMI = 0x04,
122 GpioAltFn4LPTIM3 = 0x04,
123 GpioAltFn4I2C5 = 0x04,
125 GpioAltFn5I2C4 = 0x05,
126 GpioAltFn5OCTOSPI1 = 0x05,
127 GpioAltFn5OCTOSPI2 = 0x05,
128 GpioAltFn5SPI1 = 0x05,
129 GpioAltFn5SPI2 = 0x05,
130 GpioAltFn5SPI3 = 0x05,
131 GpioAltFn5DCMI = 0x05,
132 GpioAltFn5MDF1 = 0x05,
133 GpioAltFn5PSSI = 0x05,
134 GpioAltFn5GFXTIM = 0x05,
136 GpioAltFn6OCTOSPI1 = 0x06,
137 GpioAltFn6OCTOSPI2 = 0x06,
138 GpioAltFn6MDF1 = 0x06,
139 GpioAltFn6SPI3 = 0x06,
140 GpioAltFn6I2C3 = 0x06,
142 GpioAltFn7USART1 = 0x07,
143 GpioAltFn7USART2 = 0x07,
144 GpioAltFn7USART3 = 0x07,
145 GpioAltFn7USART6 = 0x07,
146 GpioAltFn7LTDC = 0x07,
148 GpioAltFn8LPUART1 = 0x08,
149 GpioAltFn8UART4 = 0x08,
150 GpioAltFn8UART5 = 0x08,
151 GpioAltFn8SDMMC1 = 0x08,
152 GpioAltFn8SDMMC2 = 0x08,
153 GpioAltFn8LTDC = 0x08,
154 GpioAltFn8HSPI1 = 0x08,
156 GpioAltFn9FDCAN1 = 0x09,
157 GpioAltFn9TSC = 0x09,
159 GpioAltFn10DCMI = 0x0A,
160 GpioAltFn10PSSI = 0x0A,
161 GpioAltFn10USB = 0x0A,
162 GpioAltFn10OCTOSPI1 = 0x0A,
163 GpioAltFn10OCTOSPI2 = 0x0A,
164 GpioAltFn10CRS = 0x0A,
165 GpioAltFn10USB_HS = 0x0A,
166 GpioAltFn10DSI = 0x0A,
167 GpioAltFn10GFXTIM = 0x0A,
169 GpioAltFn11UCPD1 = 0x0B,
170 GpioAltFn11SDMMC2 = 0x0B,
171 GpioAltFn11LPGPIO1 = 0x0B,
172 GpioAltFn11FMC = 0x0B,
173 GpioAltFn11DSI = 0x0B,
174 GpioAltFn11GFXTIM = 0x0B,
176 GpioAltFn12COMP1 = 0x0C,
177 GpioAltFn12COMP2 = 0x0C,
178 GpioAltFn12FMC = 0x0C,
179 GpioAltFn12TIM1_COMP1 = 0x0C,
180 GpioAltFn12TIM1_COMP2 = 0x0C,
181 GpioAltFn12TIM8_COMP2 = 0x0C,
182 GpioAltFn12SDMMC1 = 0x0C,
183 GpioAltFn12SDMMC2 = 0x0C,
185 GpioAltFn13SAI1 = 0x0D,
186 GpioAltFn13SAI2 = 0x0D,
187 GpioAltFn13LPTIM4 = 0x0D,
188 GpioAltFn13LPTIM2 = 0x0D,
189 GpioAltFn13GFXTIM = 0x0D,
191 GpioAltFn14LPTIM2 = 0x0E,
192 GpioAltFn14LPTIM3 = 0x0E,
193 GpioAltFn14TIM2 = 0x0E,
194 GpioAltFn14TIM15 = 0x0E,
195 GpioAltFn14TIM15_COMP1 = 0x0E,
196 GpioAltFn14TIM16 = 0x0E,
197 GpioAltFn14TIM16_COMP1 = 0x0E,
198 GpioAltFn14TIM17 = 0x0E,
199 GpioAltFn14TIM17_COMP1 = 0x0E,
200 GpioAltFn14FMC = 0x0E,
202 GpioAltFn15EVENTOUT = 0x0F,
203 GpioAltFnUnused = 16,
204} GpioAltFn;
205
209typedef struct {
210 GPIO_TypeDef* port;
211 uint16_t pin;
212} GpioPin;
213
219void furi_hal_gpio_init_simple(const GpioPin* gpio, const GpioMode mode);
220
228void furi_hal_gpio_init(
229 const GpioPin* gpio,
230 const GpioMode mode,
231 const GpioPull pull,
232 const GpioSpeed speed);
233
242void furi_hal_gpio_init_ex(
243 const GpioPin* gpio,
244 const GpioMode mode,
245 const GpioPull pull,
246 const GpioSpeed speed,
247 const GpioAltFn alt_fn);
248
255void furi_hal_gpio_add_int_callback(const GpioPin* gpio, GpioExtiCallback cb, void* ctx);
256
261void furi_hal_gpio_enable_int_callback(const GpioPin* gpio);
262
267void furi_hal_gpio_disable_int_callback(const GpioPin* gpio);
268
273void furi_hal_gpio_remove_int_callback(const GpioPin* gpio);
274
280static inline void furi_hal_gpio_write(const GpioPin* gpio, const bool state) {
281 // writing to BSSR is an atomic operation
282 if(state == true) {
283 gpio->port->BSRR = gpio->pin;
284 } else {
285 gpio->port->BSRR = (uint32_t)gpio->pin << GPIO_NUMBER;
286 }
287}
288
293#define furi_hal_gpio_write_open_drain(gpio, state) furi_hal_gpio_write(gpio, state)
294
301static inline void
302 furi_hal_gpio_write_port_pin(GPIO_TypeDef* port, uint16_t pin, const bool state) {
303 // writing to BSSR is an atomic operation
304 if(state == true) {
305 port->BSRR = pin;
306 } else {
307 port->BSRR = pin << GPIO_NUMBER;
308 }
309}
310
316static inline bool furi_hal_gpio_read(const GpioPin* gpio) {
317 if((gpio->port->IDR & gpio->pin) != 0x00U) {
318 return true;
319 } else {
320 return false;
321 }
322}
323
330static inline bool furi_hal_gpio_read_port_pin(GPIO_TypeDef* port, uint16_t pin) {
331 if((port->IDR & pin) != 0x00U) {
332 return true;
333 } else {
334 return false;
335 }
336}
337
338#ifdef __cplusplus
339}
340#endif
Gpio interrupt type.
Definition f21/furi_hal/furi_hal_gpio.h:25
Gpio structure.
Definition f21/furi_hal/furi_hal_gpio.h:209