flipperzero_sys/inlines/
furi_hal_gpio.rs

1//! Inlines for Furi HAL GPIO interface.
2//!
3//! See: [`furi_hal_gpio.h`][1]
4//!
5//! [1]: https://github.com/flipperdevices/flipperzero-firmware/blob/release/firmware/targets/f7/furi_hal/furi_hal_gpio.h
6
7use crate as sys;
8
9/// Number of GPIO on one port.
10pub const GPIO_NUMBER: usize = 16;
11
12/// GPIO write pin.
13///
14/// # Safety
15///
16/// `gpio` must be non-null, and the memory it points to must be initialized.
17#[inline]
18pub unsafe extern "C" fn furi_hal_gpio_write(gpio: *const sys::GpioPin, state: bool) {
19    let gpio = unsafe { *gpio };
20    let port = gpio.port;
21    let pin = gpio.pin;
22
23    unsafe { furi_hal_gpio_write_port_pin(port, pin, state) }
24}
25
26/// GPIO write pin.
27///
28/// # Safety
29///
30/// `port` must be non-null, and the memory it points to must be initialized.
31#[inline]
32pub unsafe extern "C" fn furi_hal_gpio_write_port_pin(
33    port: *mut sys::GPIO_TypeDef,
34    pin: u16,
35    state: bool,
36) {
37    // writing to BSSR is an atomic operation
38    unsafe {
39        core::ptr::write_volatile(
40            &mut (*port).BSRR,
41            (pin as u32) << if state { 0 } else { GPIO_NUMBER },
42        );
43    }
44}
45
46/// GPIO read pin.
47///
48/// # Safety
49///
50/// `gpio` must be non-null, and the memory it points to must be initialized.
51#[inline]
52pub unsafe extern "C" fn furi_hal_gpio_read(gpio: *const sys::GpioPin) -> bool {
53    let gpio = unsafe { *gpio };
54    let port = gpio.port;
55    let pin = gpio.pin;
56
57    unsafe { furi_hal_gpio_read_port_pin(port, pin) }
58}
59
60/// GPIO read pin.
61///
62/// # Safety
63///
64/// `port` must be non-null, and the memory it points to must be initialized.
65#[inline]
66pub unsafe extern "C" fn furi_hal_gpio_read_port_pin(
67    port: *mut sys::GPIO_TypeDef,
68    pin: u16,
69) -> bool {
70    let port = unsafe { *port };
71    let input_data_register_value = unsafe { core::ptr::read_volatile(&port.IDR) };
72    input_data_register_value & pin as u32 != 0x00
73}