diff --git a/firmware/acid-firmware/src/ui/dpi.rs b/firmware/acid-firmware/src/dpi.rs similarity index 60% rename from firmware/acid-firmware/src/ui/dpi.rs rename to firmware/acid-firmware/src/dpi.rs index e385f86..d06de84 100644 --- a/firmware/acid-firmware/src/ui/dpi.rs +++ b/firmware/acid-firmware/src/dpi.rs @@ -3,15 +3,101 @@ use embassy_time::Timer; use esp_hal::{ Blocking, dma::{BurstConfig, ExternalBurstConfig, InternalBurstConfig}, - lcd_cam::lcd::dpi::Dpi, + gpio::{Flex, Level, Output, OutputConfig}, + lcd_cam::{LcdCam, lcd::dpi::Dpi}, + ledc::{self, LSGlobalClkSource, Ledc, LowSpeed}, spi::master::AnySpi, }; use esp_hal_bounce_buffers::{DmaBounce, Swapchain, SwapchainWriter, allocate_dma_buffer_in}; use i_slint_core::software_renderer::{Rgb565Pixel, TargetPixel}; -use log::error; +use log::{error, info}; use crate::{DmaBounceController, PSRAM_ALLOCATOR, peripherals::st7701s::St7701s}; +#[allow(non_snake_case)] +pub struct DisplayPeripherals { + pub DMA_CH2: esp_hal::peripherals::DMA_CH2<'static>, + pub LCD_CAM: esp_hal::peripherals::LCD_CAM<'static>, + pub LEDC: esp_hal::peripherals::LEDC<'static>, + pub GPIO0: Output<'static>, + pub GPIO1: esp_hal::peripherals::GPIO1<'static>, + pub GPIO2: esp_hal::peripherals::GPIO2<'static>, + pub GPIO3: esp_hal::peripherals::GPIO3<'static>, + pub GPIO4: esp_hal::peripherals::GPIO4<'static>, + #[cfg(not(feature = "alt-log"))] + pub GPIO5: esp_hal::peripherals::GPIO5<'static>, + pub GPIO6: esp_hal::peripherals::GPIO6<'static>, + #[cfg(not(feature = "alt-log"))] + pub GPIO12: esp_hal::peripherals::GPIO12<'static>, + pub GPIO13: esp_hal::peripherals::GPIO13<'static>, + pub GPIO14: esp_hal::peripherals::GPIO14<'static>, + pub GPIO15: esp_hal::peripherals::GPIO15<'static>, + pub GPIO16: esp_hal::peripherals::GPIO16<'static>, + pub GPIO21: esp_hal::peripherals::GPIO21<'static>, + pub GPIO34: esp_hal::peripherals::GPIO34<'static>, + pub GPIO35: esp_hal::peripherals::GPIO35<'static>, + pub GPIO36: esp_hal::peripherals::GPIO36<'static>, + pub GPIO37: esp_hal::peripherals::GPIO37<'static>, + pub GPIO38: esp_hal::peripherals::GPIO38<'static>, + pub GPIO39: esp_hal::peripherals::GPIO39<'static>, + pub GPIO40: esp_hal::peripherals::GPIO40<'static>, + pub GPIO41: esp_hal::peripherals::GPIO41<'static>, + pub GPIO42: esp_hal::peripherals::GPIO42<'static>, + pub GPIO43: esp_hal::peripherals::GPIO43<'static>, + pub GPIO44: esp_hal::peripherals::GPIO44<'static>, +} + +impl DisplayPeripherals { + pub async fn into_display(self) -> St7701s<'static, Blocking> { + let mut ledc = Ledc::new(self.LEDC); + ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); + let bl_timer = ledc.timer::(ledc::timer::Number::Timer0); + let bl_channel = ledc.channel::(ledc::channel::Number::Channel0, self.GPIO21); + + let sck = Output::new(self.GPIO36, Level::High, OutputConfig::default()); + let mosi = Flex::new(self.GPIO35); + let cs = Output::new(self.GPIO6, Level::High, OutputConfig::default()); + + let lcd = LcdCam::new(self.LCD_CAM).lcd; + #[allow(unused_mut)] + let mut unconfigured_dpi = Dpi::new(lcd, self.DMA_CH2, Default::default()) + .unwrap() + .with_de(self.GPIO37) + .with_pclk(self.GPIO34) + .with_hsync(self.GPIO44) + .with_vsync(self.GPIO43) + // Blue + .with_data0(self.GPIO38) + .with_data1(self.GPIO39) + .with_data2(self.GPIO40) + .with_data3(self.GPIO41) + .with_data4(self.GPIO42) + // Green + .with_data7(self.GPIO13) + .with_data8(self.GPIO14) + .with_data9(self.GPIO15) + .with_data10(self.GPIO16) + // Red + .with_data11(self.GPIO0) + .with_data12(self.GPIO1) + .with_data13(self.GPIO2) + .with_data14(self.GPIO3) + .with_data15(self.GPIO4); + + #[cfg(not(feature = "alt-log"))] + { + unconfigured_dpi = unconfigured_dpi + // Green + .with_data5(peripherals.GPIO5) + .with_data6(peripherals.GPIO12); + } + + let st7701s = St7701s::new(sck, mosi, cs, unconfigured_dpi, bl_timer, bl_channel).await; + info!("ST7701S-based LCD display initialized!"); + st7701s + } +} + // TODO: Rename or get rid of. pub struct Framebuffer { pub width: u32, @@ -70,10 +156,10 @@ impl Framebuffer { } #[allow(unused)] -async fn test_bounce_buffers( +pub async fn test_bounce_buffers( channel: esp_hal::peripherals::DMA_CH0<'static>, peripheral: esp_hal::peripherals::SPI2<'static>, - mut st7701s: St7701s<'static, Blocking>, + display_peripherals: DisplayPeripherals, ) { error!("TEST BOUNCE BUFFERS SECTION ENTERED"); @@ -142,6 +228,7 @@ async fn test_bounce_buffers( } } + let mut st7701s = display_peripherals.into_display().await; let mut dma_bounce = DmaBounce::new( Global, channel, diff --git a/firmware/acid-firmware/src/flash.rs b/firmware/acid-firmware/src/flash.rs index 6c77f45..8e36876 100644 --- a/firmware/acid-firmware/src/flash.rs +++ b/firmware/acid-firmware/src/flash.rs @@ -1,7 +1,7 @@ use core::fmt::Write; use alloc::{string::String, vec::Vec}; -use embassy_embedded_hal::{adapter::BlockingAsync, flash::partition::Partition}; +use embassy_embedded_hal::adapter::BlockingAsync; use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, mutex::Mutex}; use esp_bootloader_esp_idf::partitions::PartitionTable; use esp_storage::FlashStorage; @@ -12,9 +12,15 @@ use static_cell::StaticCell; use crate::PSRAM_ALLOCATOR; +pub type Partition = embassy_embedded_hal::flash::partition::Partition< + 'static, + CriticalSectionRawMutex, + BlockingAsync>, +>; + pub struct Partitions { - pub rmk: Partition<'static, CriticalSectionRawMutex, BlockingAsync>>, - pub acid: Partition<'static, CriticalSectionRawMutex, BlockingAsync>>, + pub rmk: Partition, + pub acid: Partition, } /// Initialize the flash diff --git a/firmware/acid-firmware/src/main.rs b/firmware/acid-firmware/src/main.rs index 408f9f7..8f529cc 100644 --- a/firmware/acid-firmware/src/main.rs +++ b/firmware/acid-firmware/src/main.rs @@ -19,12 +19,10 @@ extern crate alloc; use core::cell::RefCell; use core::sync::atomic::{AtomicBool, Ordering}; -use alloc::alloc::Global; use alloc::boxed::Box; use alloc::collections::vec_deque::VecDeque; use alloc::format; use alloc::sync::Arc; -use alloc::vec::Vec; use embassy_executor::Spawner; use embassy_sync::blocking_mutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; @@ -34,29 +32,21 @@ use esp_alloc::MemoryCapability; use esp_hal::clock::CpuClock; use esp_hal::dma::{BurstConfig, ExternalBurstConfig, InternalBurstConfig}; use esp_hal::efuse::Efuse; -use esp_hal::gpio::{Flex, Input, InputConfig, Level, Output, OutputConfig, Pull}; +use esp_hal::gpio::{Input, InputConfig, Level, Output, OutputConfig, Pull}; use esp_hal::i2c::master::{I2c, I2cAddress}; use esp_hal::interrupt::software::{SoftwareInterrupt, SoftwareInterruptControl}; -use esp_hal::lcd_cam::LcdCam; -use esp_hal::lcd_cam::lcd::dpi::Dpi; -use esp_hal::ledc::{self, LSGlobalClkSource, Ledc, LowSpeed}; -use esp_hal::peripherals::{DMA_CH0, SPI2}; use esp_hal::psram::{FlashFreq, PsramConfig, PsramSize, SpiRamFreq, SpiTimingConfigCoreClock}; use esp_hal::rng::TrngSource; use esp_hal::sha::ShaBackend; -use esp_hal::spi::master::AnySpi; use esp_hal::system::Stack; use esp_hal::timer::timg::TimerGroup; use esp_hal::uart::{Uart, UartRx}; use esp_hal::{Blocking, interrupt}; -use esp_hal_bounce_buffers::{ - DmaBounce, RunningDmaBounceHandle, Swapchain, allocate_dma_buffer_in, -}; +use esp_hal_bounce_buffers::{DmaBounce, RunningDmaBounceHandle}; use esp_rtos::embassy::{Executor, InterruptExecutor}; use esp_storage::FlashStorage; -use i_slint_core::software_renderer::TargetPixel; use itertools::Itertools; -use log::{error, info, warn}; +use log::{info, warn}; use rmk::channel::{CONTROLLER_CHANNEL, ControllerSub}; use rmk::config::{DeviceConfig, RmkConfig, StorageConfig, VialConfig}; use rmk::controller::{Controller, EventController}; @@ -68,16 +58,16 @@ use rmk::join_all; use rmk::keyboard::Keyboard; use rmk::types::action::{Action, KeyAction}; use rmk::{initialize_keymap_and_storage, run_devices, run_rmk}; -use slint::platform::software_renderer::Rgb565Pixel; use static_cell::StaticCell; use {esp_alloc as _, esp_backtrace as _}; +use crate::dpi::{DisplayPeripherals, Framebuffer}; +use crate::flash::Partition; use crate::matrix::IoeMatrix; -use crate::peripherals::st7701s::{St7701s, St7701sController}; +use crate::peripherals::st7701s::St7701sController; use crate::proxy::create_hid_report_interceptor; use crate::ram::{PSRAM_ALLOCATOR, STACK_SIZE_CORE_APP}; use crate::ui::backend::SlintBackend; -use crate::ui::dpi::Framebuffer; use crate::vial::{ CustomKeycodes, VIAL_KEYBOARD_DEF, VIAL_KEYBOARD_ID, VIAL_KEYBOARD_NAME, VIAL_PRODUCT_ID, VIAL_VENDOR_ID, @@ -88,6 +78,7 @@ mutually_exclusive_features::none_or_one_of!["usb-log", "alt-log", "rtt-log"]; mod config; mod crypto; mod db; +mod dpi; mod ffi; mod flash; mod logging; @@ -112,12 +103,6 @@ const FRAME_DURATION_MIN: Duration = Duration::from_millis(100); // 10 FPS static KEYBOARD_REPORT_PROXY: Channel = Channel::new(); static LCD_ENABLED: AtomicBool = AtomicBool::new(false); -// /// Used to signal that MCU is ready to submit the framebuffer to the LCD. -// static SIGNAL_LCD_SUBMIT: Signal = Signal::new(); - -// /// Used to signal that the MCU is ready to render the GUI. -// static SIGNAL_UI_RENDER: Signal = Signal::new(); - #[esp_rtos::main] async fn main(_spawner: Spawner) { let config = esp_hal::Config::default() @@ -196,48 +181,52 @@ async fn main(_spawner: Spawner) { // high_priority_task_spawner: interrupt_core_1_spawner, uart_rx, software_interrupt1: software_interrupt.software_interrupt1, - LEDC: peripherals.LEDC, RNG: peripherals.RNG, ADC1: peripherals.ADC1, USB0: peripherals.USB0, FLASH: peripherals.FLASH, - LCD_CAM: peripherals.LCD_CAM, DMA_CH0: peripherals.DMA_CH0, - DMA_CH2: peripherals.DMA_CH2, - I2C0: peripherals.I2C0, SPI2: peripherals.SPI2, CPU_CTRL: peripherals.CPU_CTRL, - GPIO0: gpio0, - GPIO1: peripherals.GPIO1, - GPIO2: peripherals.GPIO2, - GPIO3: peripherals.GPIO3, - GPIO4: peripherals.GPIO4, - #[cfg(not(feature = "alt-log"))] - GPIO5: peripherals.GPIO5, - GPIO6: peripherals.GPIO6, - GPIO7: peripherals.GPIO7, - GPIO8: peripherals.GPIO8, - GPIO9: peripherals.GPIO9, - #[cfg(not(feature = "alt-log"))] - GPIO12: peripherals.GPIO12, - GPIO13: peripherals.GPIO13, - GPIO14: peripherals.GPIO14, - GPIO15: peripherals.GPIO15, - GPIO16: peripherals.GPIO16, GPIO19: peripherals.GPIO19, GPIO20: peripherals.GPIO20, - GPIO21: peripherals.GPIO21, - GPIO34: peripherals.GPIO34, - GPIO35: peripherals.GPIO35, - GPIO36: peripherals.GPIO36, - GPIO37: peripherals.GPIO37, - GPIO38: peripherals.GPIO38, - GPIO39: peripherals.GPIO39, - GPIO40: peripherals.GPIO40, - GPIO41: peripherals.GPIO41, - GPIO42: peripherals.GPIO42, - GPIO43: peripherals.GPIO43, - GPIO44: peripherals.GPIO44, + display: DisplayPeripherals { + DMA_CH2: peripherals.DMA_CH2, + LCD_CAM: peripherals.LCD_CAM, + LEDC: peripherals.LEDC, + GPIO0: gpio0, + GPIO1: peripherals.GPIO1, + GPIO2: peripherals.GPIO2, + GPIO3: peripherals.GPIO3, + GPIO4: peripherals.GPIO4, + #[cfg(not(feature = "alt-log"))] + GPIO5: peripherals.GPIO5, + GPIO6: peripherals.GPIO6, + #[cfg(not(feature = "alt-log"))] + GPIO12: peripherals.GPIO12, + GPIO13: peripherals.GPIO13, + GPIO14: peripherals.GPIO14, + GPIO15: peripherals.GPIO15, + GPIO16: peripherals.GPIO16, + GPIO21: peripherals.GPIO21, + GPIO34: peripherals.GPIO34, + GPIO35: peripherals.GPIO35, + GPIO36: peripherals.GPIO36, + GPIO37: peripherals.GPIO37, + GPIO38: peripherals.GPIO38, + GPIO39: peripherals.GPIO39, + GPIO40: peripherals.GPIO40, + GPIO41: peripherals.GPIO41, + GPIO42: peripherals.GPIO42, + GPIO43: peripherals.GPIO43, + GPIO44: peripherals.GPIO44, + }, + matrix: MatrixPeripherals { + I2C0: peripherals.I2C0, + GPIO7: peripherals.GPIO7, + GPIO8: peripherals.GPIO8, + GPIO9: peripherals.GPIO9, + }, }; interrupt_core_0_spawner.must_spawn(main_task(main_task_peripherals)); @@ -246,58 +235,34 @@ async fn main(_spawner: Spawner) { /// Peripherals passed to the main task. #[allow(non_snake_case)] struct MainPeripherals { - // high_priority_task_spawner: SendSpawner, uart_rx: UartRx<'static, Blocking>, software_interrupt1: SoftwareInterrupt<'static, 1>, - LEDC: esp_hal::peripherals::LEDC<'static>, RNG: esp_hal::peripherals::RNG<'static>, ADC1: esp_hal::peripherals::ADC1<'static>, USB0: esp_hal::peripherals::USB0<'static>, FLASH: esp_hal::peripherals::FLASH<'static>, - LCD_CAM: esp_hal::peripherals::LCD_CAM<'static>, DMA_CH0: esp_hal::peripherals::DMA_CH0<'static>, - DMA_CH2: esp_hal::peripherals::DMA_CH2<'static>, - I2C0: esp_hal::peripherals::I2C0<'static>, SPI2: esp_hal::peripherals::SPI2<'static>, CPU_CTRL: esp_hal::peripherals::CPU_CTRL<'static>, - GPIO0: Output<'static>, - GPIO1: esp_hal::peripherals::GPIO1<'static>, - GPIO2: esp_hal::peripherals::GPIO2<'static>, - GPIO3: esp_hal::peripherals::GPIO3<'static>, - GPIO4: esp_hal::peripherals::GPIO4<'static>, - #[cfg(not(feature = "alt-log"))] - GPIO5: esp_hal::peripherals::GPIO5<'static>, - GPIO6: esp_hal::peripherals::GPIO6<'static>, - GPIO7: esp_hal::peripherals::GPIO7<'static>, - GPIO8: esp_hal::peripherals::GPIO8<'static>, - GPIO9: esp_hal::peripherals::GPIO9<'static>, // GPIO10: esp_hal::peripherals::GPIO10<'static>, - #[cfg(not(feature = "alt-log"))] - GPIO12: esp_hal::peripherals::GPIO12<'static>, - GPIO13: esp_hal::peripherals::GPIO13<'static>, - GPIO14: esp_hal::peripherals::GPIO14<'static>, - GPIO15: esp_hal::peripherals::GPIO15<'static>, - GPIO16: esp_hal::peripherals::GPIO16<'static>, // GPIO18: esp_hal::peripherals::GPIO18<'static>, GPIO19: esp_hal::peripherals::GPIO19<'static>, GPIO20: esp_hal::peripherals::GPIO20<'static>, - GPIO21: esp_hal::peripherals::GPIO21<'static>, // GPIO33: esp_hal::peripherals::GPIO33<'static>, - GPIO34: esp_hal::peripherals::GPIO34<'static>, - GPIO35: esp_hal::peripherals::GPIO35<'static>, - GPIO36: esp_hal::peripherals::GPIO36<'static>, - GPIO37: esp_hal::peripherals::GPIO37<'static>, - GPIO38: esp_hal::peripherals::GPIO38<'static>, - GPIO39: esp_hal::peripherals::GPIO39<'static>, - GPIO40: esp_hal::peripherals::GPIO40<'static>, - GPIO41: esp_hal::peripherals::GPIO41<'static>, - GPIO42: esp_hal::peripherals::GPIO42<'static>, - GPIO43: esp_hal::peripherals::GPIO43<'static>, - GPIO44: esp_hal::peripherals::GPIO44<'static>, // GPIO45: esp_hal::peripherals::GPIO45<'static>, // GPIO46: esp_hal::peripherals::GPIO46<'static>, // GPIO47: esp_hal::peripherals::GPIO47<'static>, // GPIO48: esp_hal::peripherals::GPIO48<'static>, + display: DisplayPeripherals, + matrix: MatrixPeripherals, +} + +#[allow(non_snake_case)] +struct MatrixPeripherals { + I2C0: esp_hal::peripherals::I2C0<'static>, + GPIO7: esp_hal::peripherals::GPIO7<'static>, + GPIO8: esp_hal::peripherals::GPIO8<'static>, + GPIO9: esp_hal::peripherals::GPIO9<'static>, } #[embassy_executor::task] @@ -353,52 +318,8 @@ async fn main_task(peripherals: MainPeripherals) { info!("Flash memory configured!"); - let mut ledc = Ledc::new(peripherals.LEDC); - ledc.set_global_slow_clock(LSGlobalClkSource::APBClk); - let bl_timer = ledc.timer::(ledc::timer::Number::Timer0); - let bl_channel = ledc.channel::(ledc::channel::Number::Channel0, peripherals.GPIO21); - - let sck = Output::new(peripherals.GPIO36, Level::High, OutputConfig::default()); - let mosi = Flex::new(peripherals.GPIO35); - let cs = Output::new(peripherals.GPIO6, Level::High, OutputConfig::default()); - - let lcd = LcdCam::new(peripherals.LCD_CAM).lcd; - let unconfigured_dpi = Dpi::new(lcd, peripherals.DMA_CH2, Default::default()) - .unwrap() - .with_de(peripherals.GPIO37) - .with_pclk(peripherals.GPIO34) - .with_hsync(peripherals.GPIO44) - .with_vsync(peripherals.GPIO43) - // Blue - .with_data0(peripherals.GPIO38) - .with_data1(peripherals.GPIO39) - .with_data2(peripherals.GPIO40) - .with_data3(peripherals.GPIO41) - .with_data4(peripherals.GPIO42) - // Green - .with_data7(peripherals.GPIO13) - .with_data8(peripherals.GPIO14) - .with_data9(peripherals.GPIO15) - .with_data10(peripherals.GPIO16) - // Red - .with_data11(peripherals.GPIO0) - .with_data12(peripherals.GPIO1) - .with_data13(peripherals.GPIO2) - .with_data14(peripherals.GPIO3) - .with_data15(peripherals.GPIO4); - - #[cfg(not(feature = "alt-log"))] - let unconfigured_dpi = unconfigured_dpi - // Green - .with_data5(peripherals.GPIO5) - .with_data6(peripherals.GPIO12); - - let st7701s = St7701s::new(sck, mosi, cs, unconfigured_dpi, bl_timer, bl_channel).await; - - info!("ST7701S-based LCD display initialized!"); - // Uncomment this to run bounce buffer test code instead. - // test_bounce_buffers(peripherals.DMA_CH0, peripherals.SPI2, st7701s).await; + // dpi::test_bounce_buffers(peripherals.DMA_CH0, peripherals.SPI2, peripherals.display).await; // return; // RMK config @@ -458,17 +379,77 @@ async fn main_task(peripherals: MainPeripherals) { info!("Initialized keymap and storage for RMK!"); + let mut keyboard = Keyboard::new(&keymap); // Initialize the light controller + + info!("Keyboard initialized!"); + info!("Awaiting on all tasks..."); + + // TODO: Probably want to select! instead and re-try. + join_all![ + run_alloc_stats_reporter(), + initialize_and_run_rmk_devices(peripherals.matrix), + keyboard.run(), // Keyboard is special + run_rmk( + &keymap, + #[cfg(not(feature = "no-usb"))] + usb_driver, + #[cfg(feature = "ble")] + &stack, + &mut storage, + rmk_config, + ), + create_hid_report_interceptor(), + initialize_display_and_renderer( + peripherals.display, + peripherals.DMA_CH0, + peripherals.SPI2, + peripherals.CPU_CTRL, + peripherals.software_interrupt1, + flash_partitions.acid + ), + console::run_console(peripherals.uart_rx.into_async()) + ] + .await; +} + +async fn run_alloc_stats_reporter() { + let mut psram_used_prev = 0; + let mut heap_used_prev = 0; + loop { + let psram_stats = PSRAM_ALLOCATOR.stats(); + let heap_stats = esp_alloc::HEAP.stats(); + if psram_stats.current_usage != psram_used_prev { + let difference = psram_stats.current_usage as isize - psram_used_prev as isize; + psram_used_prev = psram_stats.current_usage; + warn!( + "PSRAM usage changed: {}{}\n{psram_stats}", + if difference < 0 { '-' } else { '+' }, + difference.abs() + ); + } + if heap_stats.current_usage != heap_used_prev { + let difference = heap_stats.current_usage as isize - heap_used_prev as isize; + heap_used_prev = heap_stats.current_usage; + warn!( + "HEAP usage changed: {}{}\n{heap_stats}", + if difference < 0 { '-' } else { '+' }, + difference.abs() + ); + } + Timer::after_secs(1).await; + } +} + +async fn initialize_and_run_rmk_devices(matrix_peripherals: MatrixPeripherals) { // Initialize the matrix and keyboard const I2C_ADDR_MATRIX_LEFT: I2cAddress = I2cAddress::SevenBit(0b0100000); const I2C_ADDR_MATRIX_RIGHT: I2cAddress = I2cAddress::SevenBit(0b0100001); - let i2c = I2c::new(peripherals.I2C0, Default::default()) + let i2c = I2c::new(matrix_peripherals.I2C0, Default::default()) .unwrap() - .with_sda(peripherals.GPIO8) - .with_scl(peripherals.GPIO9); - - let matrix_interrupt_low = Input::new(peripherals.GPIO7, InputConfig::default()); - + .with_sda(matrix_peripherals.GPIO8) + .with_scl(matrix_peripherals.GPIO9); + let matrix_interrupt_low = Input::new(matrix_peripherals.GPIO7, InputConfig::default()); let mut matrix = IoeMatrix::new( matrix_interrupt_low, i2c.into_async(), @@ -476,14 +457,26 @@ async fn main_task(peripherals: MainPeripherals) { [I2C_ADDR_MATRIX_LEFT, I2C_ADDR_MATRIX_RIGHT], ) .await; - let mut keyboard = Keyboard::new(&keymap); // Initialize the light controller + run_devices! ( + (matrix) => rmk::channel::EVENT_CHANNEL, + ) + .await +} - info!("Keyboard initialized!"); +async fn initialize_display_and_renderer( + display_peripherals: DisplayPeripherals, + dma_ch0: esp_hal::peripherals::DMA_CH0<'static>, + spi2: esp_hal::peripherals::SPI2<'static>, + cpu_ctrl: esp_hal::peripherals::CPU_CTRL<'static>, + software_interrupt1: SoftwareInterrupt<'static, 1>, + partition_acid: Partition, +) { + let st7701s = display_peripherals.into_display().await; static FRAMEBUFFER: StaticCell = StaticCell::new(); let framebuffer = FRAMEBUFFER.init(Framebuffer::new( - peripherals.DMA_CH0, - peripherals.SPI2.into(), + dma_ch0, + spi2.into(), st7701s.dpi, BurstConfig { internal_memory: InternalBurstConfig::Enabled, @@ -516,9 +509,9 @@ async fn main_task(peripherals: MainPeripherals) { static SECOND_CORE_STACK: StaticCell> = StaticCell::new(); let second_core_stack = SECOND_CORE_STACK.init(Stack::new()); esp_rtos::start_second_core( - peripherals.CPU_CTRL, - // peripherals.software_interrupt0, - peripherals.software_interrupt1, + cpu_ctrl, + // software_interrupt0, + software_interrupt1, second_core_stack, move || { // static EXECUTOR: StaticCell> = StaticCell::new(); @@ -538,7 +531,7 @@ async fn main_task(peripherals: MainPeripherals) { quit_event_loop: Default::default(), events: Arc::new(blocking_mutex::Mutex::new(RefCell::new(VecDeque::new()))), }; - spawner.must_spawn(ui::run_renderer_task(slint_backend, flash_partitions.acid)); + spawner.must_spawn(ui::run_renderer_task(slint_backend, partition_acid)); }); }, ); @@ -548,68 +541,7 @@ async fn main_task(peripherals: MainPeripherals) { let bb_controller = DmaBounceController::new(framebuffer.bounce_buffers.take().unwrap()); let mut user_controller = UserController::new(st7701s.controller, bb_controller); - info!("Awaiting on all tasks..."); - - // TODO: Probably want to select! instead and re-try. - join_all![ - run_alloc_stats_reporter(), - // We currently send the framebuffer data using the main core, which does not seem to slow - // down the rest of the tasks too much. - // async { - // warn!("Waiting..."); - // Timer::after_secs(3).await; - // warn!("Waited."); - // framebuffer.bounce_buffers.send().await; - // }, - // framebuffer.bounce_buffers.send(), - // ui::dpi::run_lcd(st7701s, framebuffer), - // lcd_task, - run_devices! ( - (matrix) => rmk::channel::EVENT_CHANNEL, - ), - keyboard.run(), // Keyboard is special - run_rmk( - &keymap, - #[cfg(not(feature = "no-usb"))] - usb_driver, - #[cfg(feature = "ble")] - &stack, - &mut storage, - rmk_config, - ), - create_hid_report_interceptor(), - user_controller.event_loop(), - console::run_console(peripherals.uart_rx.into_async()) - ] - .await; -} - -async fn run_alloc_stats_reporter() { - let mut psram_used_prev = 0; - let mut heap_used_prev = 0; - loop { - let psram_stats = PSRAM_ALLOCATOR.stats(); - let heap_stats = esp_alloc::HEAP.stats(); - if psram_stats.current_usage != psram_used_prev { - let difference = psram_stats.current_usage as isize - psram_used_prev as isize; - psram_used_prev = psram_stats.current_usage; - warn!( - "PSRAM usage changed: {}{}\n{psram_stats}", - if difference < 0 { '-' } else { '+' }, - difference.abs() - ); - } - if heap_stats.current_usage != heap_used_prev { - let difference = heap_stats.current_usage as isize - heap_used_prev as isize; - heap_used_prev = heap_stats.current_usage; - warn!( - "HEAP usage changed: {}{}\n{heap_stats}", - if difference < 0 { '-' } else { '+' }, - difference.abs() - ); - } - Timer::after_secs(1).await; - } + user_controller.event_loop().await } enum DmaBounceControllerInner { diff --git a/firmware/acid-firmware/src/ui/mod.rs b/firmware/acid-firmware/src/ui/mod.rs index 49c60fe..b3af4d1 100644 --- a/firmware/acid-firmware/src/ui/mod.rs +++ b/firmware/acid-firmware/src/ui/mod.rs @@ -48,7 +48,6 @@ use crate::{ }; pub mod backend; -pub mod dpi; pub mod messages; pub mod storage; pub mod window_adapter; @@ -373,16 +372,8 @@ impl State { loop { slint::run_event_loop().unwrap(); - // SIGNAL_LCD_SUBMIT.signal(()); #[cfg(feature = "limit-fps")] embassy_time::Timer::after(FRAME_DURATION_MIN).await; - // let frames_skipped = FRAMES_SKIPPED.wait().await; - - // if frames_skipped > 0 { - // error!("Renderer missed {frames_skipped} frames."); - // } - - // SIGNAL_UI_RENDER.wait().await; } #[expect(unreachable_code)]