use esp_hal::{
Blocking,
dma::AnySpiDmaChannel,
gpio::AnyPin,
i2c::master::{BusTimeout, Config as I2cConfig, I2c},
interrupt::software::SoftwareInterruptControl,
peripherals::{BT, PCNT, Peripherals, RMT, UART0, WIFI},
spi::master::AnySpi,
time::Rate,
timer::{AnyTimer, timg::TimerGroup},
};
use crate::board::SystemResources;
use crate::board::spi2::Spi2Resources;
use crate::io::buttons::ButtonResources;
pub struct M5Bus<'a> {
pub gpio5: AnyPin<'a>,
pub gpio26: AnyPin<'a>,
}
pub struct Board {
pub spi2: Spi2Resources<'static>,
pub i2c0: I2c<'static, Blocking>,
pub buttons: ButtonResources<'static>,
pub bt: BT<'static>,
pub wifi: WIFI<'static>,
pub uart0: UART0<'static>,
pub uart0_rx: AnyPin<'static>,
pub uart0_tx: AnyPin<'static>,
pub sk6812: AnyPin<'static>,
pub rmt: RMT<'static>,
pub pcnt: PCNT<'static>,
pub psram: esp_hal::peripherals::PSRAM<'static>,
pub m5bus: M5Bus<'static>,
pub system: SystemResources<'static>,
}
impl Board {
pub fn split(peripherals: Peripherals) -> Self {
let spi2 = Spi2Resources {
spi2: AnySpi::from(peripherals.SPI2),
spi2_dma: AnySpiDmaChannel::from(peripherals.DMA_SPI2),
sck: AnyPin::from(peripherals.GPIO18),
mosi: AnyPin::from(peripherals.GPIO23),
miso: AnyPin::from(peripherals.GPIO19),
display_cs: AnyPin::from(peripherals.GPIO14),
display_dc: AnyPin::from(peripherals.GPIO27),
display_rst: AnyPin::from(peripherals.GPIO33),
display_bl: AnyPin::from(peripherals.GPIO32),
card_cs: AnyPin::from(peripherals.GPIO4),
};
let i2c0 = I2c::new(
peripherals.I2C0,
I2cConfig::default()
.with_frequency(Rate::from_khz(400))
.with_timeout(BusTimeout::BusCycles(20)),
)
.expect("I2C0 init failed")
.with_sda(AnyPin::from(peripherals.GPIO21))
.with_scl(AnyPin::from(peripherals.GPIO22));
let buttons = ButtonResources {
left: AnyPin::from(peripherals.GPIO39),
center: AnyPin::from(peripherals.GPIO38),
right: AnyPin::from(peripherals.GPIO37),
};
let tg0 = TimerGroup::new(peripherals.TIMG0);
let tg1 = TimerGroup::new(peripherals.TIMG1);
let system = SystemResources {
sw_int: SoftwareInterruptControl::new(peripherals.SW_INTERRUPT),
timer0_0: AnyTimer::from(tg0.timer0),
timer0_1: AnyTimer::from(tg0.timer1),
timer1_0: AnyTimer::from(tg1.timer0),
timer1_1: AnyTimer::from(tg1.timer1),
cpu_ctrl: peripherals.CPU_CTRL,
lpwr: peripherals.LPWR,
};
Board {
spi2,
i2c0,
buttons,
bt: peripherals.BT,
wifi: peripherals.WIFI,
uart0: peripherals.UART0,
uart0_rx: AnyPin::from(peripherals.GPIO3),
uart0_tx: AnyPin::from(peripherals.GPIO1),
sk6812: AnyPin::from(peripherals.GPIO15),
rmt: peripherals.RMT,
pcnt: peripherals.PCNT,
psram: peripherals.PSRAM,
m5bus: M5Bus {
gpio5: AnyPin::from(peripherals.GPIO5),
gpio26: AnyPin::from(peripherals.GPIO26),
},
system,
}
}
}