From c6958d8dfb3e76fe96a28ccd23f4d9473e8d5c14 Mon Sep 17 00:00:00 2001 From: Juraj Michalek Date: Mon, 16 Dec 2024 19:39:29 +0100 Subject: [PATCH] prepare M5Stack CoreS3 for BSP --- m5stack-cores3/Cargo.toml | 1 + m5stack-cores3/src/main.rs | 79 +++++++++++--------------------------- 2 files changed, 24 insertions(+), 56 deletions(-) diff --git a/m5stack-cores3/Cargo.toml b/m5stack-cores3/Cargo.toml index 0bee09d..e8c0b9d 100644 --- a/m5stack-cores3/Cargo.toml +++ b/m5stack-cores3/Cargo.toml @@ -28,6 +28,7 @@ spooky-embedded = { path = "../spooky-embedded", default-features = false, featu esp-display-interface-spi-dma = "0.2.0" shared-bus = { version = "0.3.0" } log = "0.4.22" +esp-bsp = { path = "../../esp-bsp-rs", features = ["m5stackcores3"] } [profile.dev] # Rust debug is too slow. diff --git a/m5stack-cores3/src/main.rs b/m5stack-cores3/src/main.rs index c932caf..0f27a51 100644 --- a/m5stack-cores3/src/main.rs +++ b/m5stack-cores3/src/main.rs @@ -1,9 +1,10 @@ #![no_std] #![no_main] +use esp_display_interface_spi_dma::display_interface_spi_dma; use aw9523::I2CGpioExpanderInterface; use axp2101::{Axp2101, I2CPowerManagementInterface}; -use esp_display_interface_spi_dma::display_interface_spi_dma; +use esp_bsp::boards::m5stackcores3::{lcd_dma_spi, lcd_display, lcd_display_interface, i2c_init, lcd_reset_pin}; #[allow(unused_imports)] use esp_backtrace as _; @@ -34,9 +35,6 @@ use mipidsi::options::ColorInversion; use shared_bus::BusManagerSimple; use spooky_embedded::{ app::app_loop, - /*controllers::{ - accel::AccelMovementController, composites::accel_composite::AccelCompositeController, - },*/ embedded_display::LCD_MEMORY_SIZE, }; @@ -47,90 +45,59 @@ fn main() -> ! { let mut delay = Delay::new(); - info!("About to initialize the SPI LED driver"); + println!("Initializing M5Stack CoreS3"); - let lcd_sclk = peripherals.GPIO36; - let lcd_mosi = peripherals.GPIO37; - let lcd_cs = peripherals.GPIO3; - let lcd_dc = Output::new(peripherals.GPIO35, Level::Low); - let lcd_reset = Output::new(peripherals.GPIO15, Level::Low); - - let i2c_sda = peripherals.GPIO12; - let i2c_scl = peripherals.GPIO11; - let i2c_bus = I2c::new(peripherals.I2C0, esp_hal::i2c::master::Config::default()) - .with_sda(i2c_sda) - .with_scl(i2c_scl); - let bus = BusManagerSimple::new(i2c_bus); + // Initialize I2C shared bus + let bus = BusManagerSimple::new(i2c_init!(peripherals)); + // Initialize AXP2101 power management info!("Initializing AXP2101"); let axp_interface = I2CPowerManagementInterface::new(bus.acquire_i2c()); let mut axp = Axp2101::new(axp_interface); axp.init().unwrap(); - info!("Initializing GPIO Expander"); + // Initialize AW9523 GPIO expander + info!("Initializing AW9523"); let aw_interface = I2CGpioExpanderInterface::new(bus.acquire_i2c()); let mut aw = aw9523::Aw9523::new(aw_interface); aw.init().unwrap(); - let dma = Dma::new(peripherals.DMA); - let dma_channel = dma.channel0; - - let spi = Spi::new_with_config( - peripherals.SPI2, - esp_hal::spi::master::Config { - frequency: 40u32.MHz(), - ..esp_hal::spi::master::Config::default() - }, - ) - .with_sck(lcd_sclk) - .with_mosi(lcd_mosi) - .with_cs(lcd_cs) - .with_dma(dma_channel.configure(false, DmaPriority::Priority0)); + // Initialize DMA and SPI for LCD + let spi = lcd_dma_spi!(peripherals); - info!("SPI ready"); + println!("SPI ready"); - let di = display_interface_spi_dma::new_no_cs(LCD_MEMORY_SIZE, spi, lcd_dc); + // Initialize the display interface + let di = lcd_display_interface!(peripherals, spi); - // ESP32-S3-BOX display initialization workaround: Wait for the display to power up. - // If delay is 250ms, picture will be fuzzy. - // If there is no delay, display is blank + // Delay to let the display power up delay.delay_ns(500_000u32); - let mut display = mipidsi::Builder::new(mipidsi::models::ILI9341Rgb565, di) - .display_size(240, 320) - .orientation( - mipidsi::options::Orientation::new() - .flip_vertical() - .flip_horizontal(), - ) - .color_order(mipidsi::options::ColorOrder::Bgr) + + // Initialize the display using the builder pattern macro + let mut display = lcd_display!(peripherals, di) .invert_colors(ColorInversion::Inverted) - .reset_pin(lcd_reset) .init(&mut delay) .unwrap(); - info!("Initializing..."); + println!("Initializing display..."); Text::new( "Initializing...", Point::new(80, 110), MonoTextStyle::new(&FONT_8X13, RgbColor::WHITE), ) - .draw(&mut display) - .unwrap(); - - // let icm = Icm42670::new(i2c, Address::Primary).unwrap(); + .draw(&mut display) + .unwrap(); + // Initialize RNG let mut rng = Rng::new(peripherals.RNG); let mut seed_buffer = [0u8; 32]; rng.read(&mut seed_buffer); - // let accel_movement_controller = AccelMovementController::new(icm, 0.2); + // Create the movement controller let demo_movement_controller = spooky_core::demo_movement_controller::DemoMovementController::new(seed_buffer); - let movement_controller = demo_movement_controller; - // let movement_controller = - // AccelCompositeController::new(demo_movement_controller, accel_movement_controller); println!("Entering main loop"); - app_loop(&mut display, seed_buffer, movement_controller); + app_loop(&mut display, seed_buffer, demo_movement_controller); }