Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up gigantic GPIO eyesore #2048

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 42 additions & 149 deletions esp-hal/src/soc/esp32/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
//!
//! Let's get through the functionality and configurations provided by this GPIO
//! module:
//! - `get_io_mux_reg(gpio_num: u8) -> &'static
//! crate::peripherals::io_mux::GPIO0:`:
//! - `get_io_mux_reg(gpio_num: u8) -> &'static io_mux::GPIO0:`:
//! * This function returns a reference to the GPIO register associated
//! with the given GPIO number. It uses unsafe code and transmutation to
//! access the GPIO registers based on the provided GPIO number.
Expand Down Expand Up @@ -46,6 +45,8 @@
//! registers for both the `PRO CPU` and `APP CPU`. The implementation uses the
//! `gpio` peripheral to access the appropriate registers.

use core::mem::transmute;

use crate::{
gpio::{
AlternateFunction,
Expand All @@ -54,7 +55,7 @@ use crate::{
InterruptStatusRegisterAccessBank0,
InterruptStatusRegisterAccessBank1,
},
peripherals::GPIO,
peripherals::{io_mux, GPIO, IO_MUX},
Cpu,
};

Expand All @@ -72,155 +73,47 @@ pub(crate) const ZERO_INPUT: u8 = 0x30;

pub(crate) const GPIO_FUNCTION: AlternateFunction = AlternateFunction::Function2;

pub(crate) fn get_io_mux_reg(gpio_num: u8) -> &'static crate::peripherals::io_mux::GPIO0 {
pub(crate) fn get_io_mux_reg(gpio_num: u8) -> &'static io_mux::GPIO0 {
unsafe {
let iomux = &*crate::peripherals::IO_MUX::PTR;
let iomux = &*IO_MUX::PTR;

match gpio_num {
0 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO0,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio0()),
1 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO1,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio1()),
2 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO2,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio2()),
3 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO3,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio3()),
4 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO4,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio4()),
5 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO5,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio5()),
6 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO6,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio6()),
7 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO7,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio7()),
8 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO8,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio8()),
9 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO9,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio9()),
10 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO10,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio10()),
11 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO11,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio11()),
12 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO12,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio12()),
13 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO13,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio13()),
14 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO14,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio14()),
15 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO15,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio15()),
16 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO16,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio16()),
17 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO17,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio17()),
18 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO18,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio18()),
19 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO19,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio19()),
20 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO20,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio20()),
21 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO21,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio21()),
22 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO22,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio22()),
23 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO23,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio23()),
24 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO24,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio24()),
25 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO25,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio25()),
26 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO26,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio26()),
27 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO27,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio27()),
32 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO32,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio32()),
33 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO33,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio33()),
34 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO34,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio34()),
35 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO35,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio35()),
36 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO36,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio36()),
37 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO37,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio37()),
38 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO38,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio38()),
39 => core::mem::transmute::<
&'static crate::peripherals::io_mux::GPIO39,
&'static crate::peripherals::io_mux::GPIO0,
>(iomux.gpio39()),
0 => transmute::<&'static io_mux::GPIO0, &'static io_mux::GPIO0>(iomux.gpio0()),
1 => transmute::<&'static io_mux::GPIO1, &'static io_mux::GPIO0>(iomux.gpio1()),
2 => transmute::<&'static io_mux::GPIO2, &'static io_mux::GPIO0>(iomux.gpio2()),
3 => transmute::<&'static io_mux::GPIO3, &'static io_mux::GPIO0>(iomux.gpio3()),
4 => transmute::<&'static io_mux::GPIO4, &'static io_mux::GPIO0>(iomux.gpio4()),
5 => transmute::<&'static io_mux::GPIO5, &'static io_mux::GPIO0>(iomux.gpio5()),
6 => transmute::<&'static io_mux::GPIO6, &'static io_mux::GPIO0>(iomux.gpio6()),
7 => transmute::<&'static io_mux::GPIO7, &'static io_mux::GPIO0>(iomux.gpio7()),
8 => transmute::<&'static io_mux::GPIO8, &'static io_mux::GPIO0>(iomux.gpio8()),
9 => transmute::<&'static io_mux::GPIO9, &'static io_mux::GPIO0>(iomux.gpio9()),
10 => transmute::<&'static io_mux::GPIO10, &'static io_mux::GPIO0>(iomux.gpio10()),
11 => transmute::<&'static io_mux::GPIO11, &'static io_mux::GPIO0>(iomux.gpio11()),
12 => transmute::<&'static io_mux::GPIO12, &'static io_mux::GPIO0>(iomux.gpio12()),
13 => transmute::<&'static io_mux::GPIO13, &'static io_mux::GPIO0>(iomux.gpio13()),
14 => transmute::<&'static io_mux::GPIO14, &'static io_mux::GPIO0>(iomux.gpio14()),
15 => transmute::<&'static io_mux::GPIO15, &'static io_mux::GPIO0>(iomux.gpio15()),
16 => transmute::<&'static io_mux::GPIO16, &'static io_mux::GPIO0>(iomux.gpio16()),
17 => transmute::<&'static io_mux::GPIO17, &'static io_mux::GPIO0>(iomux.gpio17()),
18 => transmute::<&'static io_mux::GPIO18, &'static io_mux::GPIO0>(iomux.gpio18()),
19 => transmute::<&'static io_mux::GPIO19, &'static io_mux::GPIO0>(iomux.gpio19()),
20 => transmute::<&'static io_mux::GPIO20, &'static io_mux::GPIO0>(iomux.gpio20()),
21 => transmute::<&'static io_mux::GPIO21, &'static io_mux::GPIO0>(iomux.gpio21()),
22 => transmute::<&'static io_mux::GPIO22, &'static io_mux::GPIO0>(iomux.gpio22()),
23 => transmute::<&'static io_mux::GPIO23, &'static io_mux::GPIO0>(iomux.gpio23()),
24 => transmute::<&'static io_mux::GPIO24, &'static io_mux::GPIO0>(iomux.gpio24()),
25 => transmute::<&'static io_mux::GPIO25, &'static io_mux::GPIO0>(iomux.gpio25()),
26 => transmute::<&'static io_mux::GPIO26, &'static io_mux::GPIO0>(iomux.gpio26()),
27 => transmute::<&'static io_mux::GPIO27, &'static io_mux::GPIO0>(iomux.gpio27()),
32 => transmute::<&'static io_mux::GPIO32, &'static io_mux::GPIO0>(iomux.gpio32()),
33 => transmute::<&'static io_mux::GPIO33, &'static io_mux::GPIO0>(iomux.gpio33()),
34 => transmute::<&'static io_mux::GPIO34, &'static io_mux::GPIO0>(iomux.gpio34()),
35 => transmute::<&'static io_mux::GPIO35, &'static io_mux::GPIO0>(iomux.gpio35()),
36 => transmute::<&'static io_mux::GPIO36, &'static io_mux::GPIO0>(iomux.gpio36()),
37 => transmute::<&'static io_mux::GPIO37, &'static io_mux::GPIO0>(iomux.gpio37()),
38 => transmute::<&'static io_mux::GPIO38, &'static io_mux::GPIO0>(iomux.gpio38()),
39 => transmute::<&'static io_mux::GPIO39, &'static io_mux::GPIO0>(iomux.gpio39()),
_ => panic!(),
}
}
Expand Down
Loading