Loading...
Searching...
No Matches
furi_hal_gpio.h
1#pragma once
2#include "stdbool.h"
3#include <stm32wbxx_ll_gpio.h>
4#include <stm32wbxx_ll_system.h>
5#include <stm32wbxx_ll_exti.h>
6
7#ifdef __cplusplus
8extern "C" {
9#endif
10
14#define GPIO_NUMBER (16U)
15
19typedef void (*GpioExtiCallback)(void* ctx);
20
24typedef struct {
25 GpioExtiCallback callback;
26 void* context;
28
32typedef enum {
33 GpioModeInput,
34 GpioModeOutputPushPull,
35 GpioModeOutputOpenDrain,
36 GpioModeAltFunctionPushPull,
37 GpioModeAltFunctionOpenDrain,
38 GpioModeAnalog,
39 GpioModeInterruptRise,
40 GpioModeInterruptFall,
41 GpioModeInterruptRiseFall,
42 GpioModeEventRise,
43 GpioModeEventFall,
44 GpioModeEventRiseFall,
45} GpioMode;
46
50typedef enum {
51 GpioPullNo,
52 GpioPullUp,
53 GpioPullDown,
54} GpioPull;
55
59typedef enum {
60 GpioSpeedLow,
61 GpioSpeedMedium,
62 GpioSpeedHigh,
63 GpioSpeedVeryHigh,
64} GpioSpeed;
65
69typedef enum {
70 GpioAltFn0MCO = 0,
71 GpioAltFn0LSCO = 0,
72 GpioAltFn0JTMS_SWDIO = 0,
73 GpioAltFn0JTCK_SWCLK = 0,
74 GpioAltFn0JTDI = 0,
75 GpioAltFn0RTC_OUT = 0,
76 GpioAltFn0JTD_TRACE = 0,
77 GpioAltFn0NJTRST = 0,
78 GpioAltFn0RTC_REFIN = 0,
79 GpioAltFn0TRACED0 = 0,
80 GpioAltFn0TRACED1 = 0,
81 GpioAltFn0TRACED2 = 0,
82 GpioAltFn0TRACED3 = 0,
83 GpioAltFn0TRIG_INOUT = 0,
84 GpioAltFn0TRACECK = 0,
85 GpioAltFn0SYS = 0,
87 GpioAltFn1TIM1 = 1,
88 GpioAltFn1TIM2 = 1,
89 GpioAltFn1LPTIM1 = 1,
91 GpioAltFn2TIM2 = 2,
92 GpioAltFn2TIM1 = 2,
94 GpioAltFn3SAI1 = 3,
95 GpioAltFn3SPI2 = 3,
96 GpioAltFn3TIM1 = 3,
98 GpioAltFn4I2C1 = 4,
99 GpioAltFn4I2C3 = 4,
101 GpioAltFn5SPI1 = 5,
102 GpioAltFn5SPI2 = 5,
104 GpioAltFn6MCO = 6,
105 GpioAltFn6LSCO = 6,
106 GpioAltFn6RF_DTB0 = 6,
107 GpioAltFn6RF_DTB1 = 6,
108 GpioAltFn6RF_DTB2 = 6,
109 GpioAltFn6RF_DTB3 = 6,
110 GpioAltFn6RF_DTB4 = 6,
111 GpioAltFn6RF_DTB5 = 6,
112 GpioAltFn6RF_DTB6 = 6,
113 GpioAltFn6RF_DTB7 = 6,
114 GpioAltFn6RF_DTB8 = 6,
115 GpioAltFn6RF_DTB9 = 6,
116 GpioAltFn6RF_DTB10 = 6,
117 GpioAltFn6RF_DTB11 = 6,
118 GpioAltFn6RF_DTB12 = 6,
119 GpioAltFn6RF_DTB13 = 6,
120 GpioAltFn6RF_DTB14 = 6,
121 GpioAltFn6RF_DTB15 = 6,
122 GpioAltFn6RF_DTB16 = 6,
123 GpioAltFn6RF_DTB17 = 6,
124 GpioAltFn6RF_DTB18 = 6,
125 GpioAltFn6RF_MISO = 6,
126 GpioAltFn6RF_MOSI = 6,
127 GpioAltFn6RF_SCK = 6,
128 GpioAltFn6RF_NSS = 6,
130 GpioAltFn7USART1 = 7,
132 GpioAltFn8LPUART1 = 8,
133 GpioAltFn8IR = 8,
135 GpioAltFn9TSC = 9,
137 GpioAltFn10QUADSPI = 10,
138 GpioAltFn10USB = 10,
140 GpioAltFn11LCD = 11,
142 GpioAltFn12COMP1 = 12,
143 GpioAltFn12COMP2 = 12,
144 GpioAltFn12TIM1 = 12,
146 GpioAltFn13SAI1 = 13,
148 GpioAltFn14TIM2 = 14,
149 GpioAltFn14TIM16 = 14,
150 GpioAltFn14TIM17 = 14,
151 GpioAltFn14LPTIM2 = 14,
153 GpioAltFn15EVENTOUT = 15,
155 GpioAltFnUnused = 16,
156} GpioAltFn;
157
161typedef struct {
162 GPIO_TypeDef* port;
163 uint16_t pin;
164} GpioPin;
165
171void furi_hal_gpio_init_simple(const GpioPin* gpio, const GpioMode mode);
172
180void furi_hal_gpio_init(
181 const GpioPin* gpio,
182 const GpioMode mode,
183 const GpioPull pull,
184 const GpioSpeed speed);
185
194void furi_hal_gpio_init_ex(
195 const GpioPin* gpio,
196 const GpioMode mode,
197 const GpioPull pull,
198 const GpioSpeed speed,
199 const GpioAltFn alt_fn);
200
207void furi_hal_gpio_add_int_callback(const GpioPin* gpio, GpioExtiCallback cb, void* ctx);
208
213void furi_hal_gpio_enable_int_callback(const GpioPin* gpio);
214
219void furi_hal_gpio_disable_int_callback(const GpioPin* gpio);
220
225void furi_hal_gpio_remove_int_callback(const GpioPin* gpio);
226
232static inline void furi_hal_gpio_write(const GpioPin* gpio, const bool state) {
233 // writing to BSSR is an atomic operation
234 if(state == true) {
235 gpio->port->BSRR = gpio->pin;
236 } else {
237 gpio->port->BSRR = (uint32_t)gpio->pin << GPIO_NUMBER;
238 }
239}
240
247static inline void
248 furi_hal_gpio_write_port_pin(GPIO_TypeDef* port, uint16_t pin, const bool state) {
249 // writing to BSSR is an atomic operation
250 if(state == true) {
251 port->BSRR = pin;
252 } else {
253 port->BSRR = pin << GPIO_NUMBER;
254 }
255}
256
262static inline bool furi_hal_gpio_read(const GpioPin* gpio) {
263 if((gpio->port->IDR & gpio->pin) != 0x00U) {
264 return true;
265 } else {
266 return false;
267 }
268}
269
276static inline bool furi_hal_gpio_read_port_pin(GPIO_TypeDef* port, uint16_t pin) {
277 if((port->IDR & pin) != 0x00U) {
278 return true;
279 } else {
280 return false;
281 }
282}
283
284#ifdef __cplusplus
285}
286#endif
Gpio interrupt type.
Definition furi_hal_gpio.h:24
Gpio structure.
Definition furi_hal_gpio.h:161