//! TODO: //! * GUI event dispatch. //! * Async interrupt handling of keyboard input. Reduces LCD glitching. //! * Attempt to reduce the size of the framebuffer to 240x960 by changing the front/back porch of //! the LCD driver. Reduces LCD glitching. //! * Bounce buffers to get rid of glitching completely. //! https://esp32.com/viewtopic.php?t=28230 //! https://esp32.com/viewtopic.php?f=12&t=26793&start=20#p95677 #![no_std] #![no_main] #![feature(allocator_api)] #![feature(macro_metavar_expr)] #![feature(c_variadic)] #![feature(c_size_t)] #![feature(debug_closure_helpers)] extern crate alloc; use core::alloc::Layout; use core::cell::RefCell; use core::fmt::Write; use core::sync::atomic::{AtomicBool, Ordering}; use alloc::boxed::Box; use alloc::collections::vec_deque::VecDeque; use alloc::format; use alloc::string::String; use alloc::sync::Arc; use alloc::vec; use alloc::vec::Vec; use embassy_embedded_hal::adapter::BlockingAsync; use embassy_embedded_hal::flash::partition::Partition; use embassy_executor::Spawner; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::channel::Channel; use embassy_sync::mutex::Mutex; use embassy_sync::signal::Signal; use embassy_time::{Duration, Timer}; use esp_alloc::{HeapRegion, MemoryCapability}; use esp_bootloader_esp_idf::partitions::PartitionTable; use esp_hal::Blocking; use esp_hal::clock::CpuClock; use esp_hal::dma::{BurstConfig, DmaDescriptor, DmaTxBuf, ExternalBurstConfig}; use esp_hal::efuse::Efuse; use esp_hal::gpio::{Flex, Input, InputConfig, Level, Output, OutputConfig, Pull}; use esp_hal::i2c::master::{I2c, I2cAddress}; use esp_hal::interrupt::software::SoftwareInterruptControl; use esp_hal::lcd_cam::LcdCam; use esp_hal::lcd_cam::lcd::dpi::Dpi; use esp_hal::mcpwm::{McPwm, PeripheralClockConfig}; use esp_hal::psram::{FlashFreq, PsramConfig, PsramSize, SpiRamFreq, SpiTimingConfigCoreClock}; use esp_hal::ram; use esp_hal::rng::TrngSource; use esp_hal::sha::ShaBackend; use esp_hal::system::Stack; use esp_hal::timer::timg::TimerGroup; use esp_rtos::embassy::Executor; use esp_storage::FlashStorage; use indoc::writedoc; use itertools::Itertools; use log::{error, info, warn}; use rmk::channel::{CONTROLLER_CHANNEL, ControllerSub}; use rmk::config::{ BehaviorConfig, DeviceConfig, PositionalConfig, RmkConfig, StorageConfig, VialConfig, }; use rmk::controller::{Controller, EventController}; use rmk::debounce::default_debouncer::DefaultDebouncer; use rmk::event::ControllerEvent; use rmk::hid::Report; use rmk::input_device::Runnable; use rmk::join_all; use rmk::keyboard::Keyboard; use rmk::storage::async_flash_wrapper; 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::keymap::create_hid_report_interceptor; use crate::logging::LOG_LEVEL_FILTER; use crate::matrix::IoeMatrix; use crate::peripherals::st7701s::St7701s; use crate::ui::backend::{FramebufferPtr, SlintBackend}; use crate::vial::{ CustomKeycodes, VIAL_KEYBOARD_DEF, VIAL_KEYBOARD_ID, VIAL_KEYBOARD_NAME, VIAL_PRODUCT_ID, VIAL_VENDOR_ID, }; mutually_exclusive_features::none_or_one_of!["usb-log", "alt-log", "rtt-log"]; mod crypto; mod db; mod ffi; mod keymap; mod logging; mod matrix; mod peripherals; mod ui; mod util; mod vial; #[cfg(feature = "alt-log")] mod console; // This creates a default app-descriptor required by the esp-idf bootloader. // For more information see: esp_bootloader_esp_idf::esp_app_desc!(); // Memory allocation regions. // These can be debugged using `xtensa-esp32s3-elf-size -A `. // A panic such as `memory allocation of 3740121773 bytes failed` is caused by a heap overflow. The size is `DEEDBAAD` in hex. /// Total heap size const HEAP_SIZE: usize = 112 * 1024; /// Size of the app core's stack const STACK_SIZE_CORE_APP: usize = 80 * 1024; // const FRAME_DURATION_MIN: Duration = Duration::from_millis(40); // 25 FPS const FRAME_DURATION_MIN: Duration = Duration::from_millis(100); // 10 FPS pub static PSRAM_ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty(); 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() .with_cpu_clock(CpuClock::max()) .with_psram(PsramConfig { size: PsramSize::AutoDetect, core_clock: Some(SpiTimingConfigCoreClock::SpiTimingConfigCoreClock80m), flash_frequency: FlashFreq::default(), ram_frequency: SpiRamFreq::Freq80m, }); let peripherals: esp_hal::peripherals::Peripherals = esp_hal::init(config); #[cfg(feature = "usb-log")] let console_task = logging::usb::setup_logging(); #[cfg(feature = "alt-log")] let console_task = logging::uart::setup_logging(peripherals.UART2, peripherals.GPIO12, peripherals.GPIO5); #[cfg(feature = "rtt-log")] let console_task = logging::rtt::setup_logging(); // Use the internal DRAM as the heap. // Memory reclaimed from the esp-idf bootloader. const HEAP_SIZE_RECLAIMED: usize = const { let range = esp_metadata_generated::memory_range!("DRAM2_UNINIT"); range.end - range.start }; esp_alloc::heap_allocator!(#[ram(reclaimed)] size: HEAP_SIZE_RECLAIMED); esp_alloc::heap_allocator!(size: HEAP_SIZE - HEAP_SIZE_RECLAIMED); info!("Heap initialized! {:#?}", esp_alloc::HEAP.stats()); // Initialize the PSRAM allocator. { let (psram_offset, psram_size) = esp_hal::psram::psram_raw_parts(&peripherals.PSRAM); unsafe { PSRAM_ALLOCATOR.add_region(HeapRegion::new( psram_offset, psram_size, MemoryCapability::External.into(), )); } info!( "PSRAM allocator initialized with capacity of {} MiB!", psram_size / 1024 / 1024 ); } // let mut io = Io::new(peripherals.IO_MUX); // io.set_interrupt_handler(interrupt_handler); // info!("IO Mux initialized!"); // Enable pull-up on GPIO0 to prevent booting into download mode. let gpio0 = Output::new( peripherals.GPIO0, Level::High, OutputConfig::default().with_pull(Pull::Up), ); // Enable LDO2 let _ = Output::new(peripherals.GPIO17, Level::High, OutputConfig::default()); // Enable antenna let _ = Output::new(peripherals.GPIO11, Level::Low, OutputConfig::default()); // TODO: Use PWM to control the pwm_pin. let mut _pwm = McPwm::new(peripherals.MCPWM0, PeripheralClockConfig::with_prescaler(1)); let mut _pwm_pin = Output::new(peripherals.GPIO21, Level::High, OutputConfig::default()); let mut sha_backend = ShaBackend::new(peripherals.SHA); let _sha_driver_handle = sha_backend.start(); let timg0 = TimerGroup::new(peripherals.TIMG0); let software_interrupt = SoftwareInterruptControl::new(peripherals.SW_INTERRUPT); esp_rtos::start( timg0.timer0, /*, software_interrupt.software_interrupt0 */ ); info!("ESP-RTOS started!"); // Enable the TRNG source, so `Trng` can be constructed. let _trng_source = TrngSource::new(peripherals.RNG, peripherals.ADC1); #[cfg(feature = "ble")] let mut host_resources = rmk::HostResources::new(); #[cfg(feature = "ble")] let stack = { use bt_hci::controller::ExternalController; use esp_radio::{Controller as RadioController, ble::controller::BleConnector}; let mut rng = esp_hal::rng::Trng::try_new().unwrap(); static RADIO: StaticCell> = StaticCell::new(); let radio = RADIO.init(esp_radio::init().unwrap()); let bluetooth = peripherals.BT; let connector = BleConnector::new(radio, bluetooth, Default::default()).unwrap(); let controller: ExternalController<_, 20> = ExternalController::new(connector); let central_addr = [0x18, 0xe2, 0x21, 0x80, 0xc0, 0xc7]; let ble_stack = rmk::ble::build_ble_stack(controller, central_addr, &mut rng, &mut host_resources) .await; info!("BLE stack for RMK built!"); ble_stack }; // Initialize USB #[cfg(not(feature = "no-usb"))] let usb_driver = { use esp_hal::otg_fs::Usb; use esp_hal::otg_fs::asynch::{Config, Driver}; static EP_MEMORY: StaticCell<[u8; 1024]> = StaticCell::new(); let ep_memory = EP_MEMORY.init_with(|| [0_u8; _]); let usb = Usb::new(peripherals.USB0, peripherals.GPIO20, peripherals.GPIO19); // Create the driver, from the HAL. let config = Config::default(); let driver = Driver::new(usb, ep_memory, config); info!("USB driver for RMK built!"); driver }; // Initialize the flash static PARTITION_TABLE_BUFFER: StaticCell> = StaticCell::new(); let partition_table_buffer = PARTITION_TABLE_BUFFER.init_with(|| { let mut buffer = Vec::::new_in(&PSRAM_ALLOCATOR); buffer.resize(1024, 0_u8); buffer }); static FLASH: StaticCell<( Mutex>, PartitionTable<'static>, )> = StaticCell::new(); let (flash, partition_table) = FLASH.init_with(|| { let mut flash = FlashStorage::new(peripherals.FLASH) // Flash memory may not be written to while another core is executing from it. // By default, `FlashStorage` is configured to abort the operation and log an error message. // However, it can also be configured to auto-park the other core, such that writing to // flash succeeds. // Alternatively, XiP from PSRAM could be used along with the `multicore_ignore` strategy, // to avoid having to park the other core, which could result in better performance. // Invalid configuration would then present itself as freezing/UB. .multicore_auto_park(); let partition_table = { esp_bootloader_esp_idf::partitions::read_partition_table( &mut flash, partition_table_buffer, ) .expect("Failed to read the partition table.") }; ( Mutex::::new(async_flash_wrapper(flash)), partition_table, ) }); { let mut buffer = String::new(); writeln!(buffer, "Partition table:").unwrap(); for (index, partition) in partition_table.iter().enumerate() { writedoc!( buffer, " Partition #{index} {label:?}: offset: 0x{offset:x} length: 0x{len:x} type: 0x{type:?} read only: {read_only} encrypted: {encrypted} magic: {magic} ", label = partition.label_as_str(), offset = partition.offset(), len = partition.len(), type = partition.partition_type(), read_only = partition.is_read_only(), encrypted = partition.is_encrypted(), magic = partition.magic(), ) .unwrap(); } info!("{}", buffer); } let flash_part_info_rmk = partition_table .iter() .find(|partition| partition.label_as_str() == "rmk") .expect("No \"rmk\" partition found. Make sure to use the custom partition-table.csv when flashing."); let flash_part_info_acid = partition_table .iter() .find(|partition| partition.label_as_str() == "acid") .expect("No \"acid\" partition found. Make sure to use the custom partition-table.csv when flashing."); let flash_part_rmk = Partition::new( flash, flash_part_info_rmk.offset(), flash_part_info_rmk.len(), ); let flash_part_acid = Partition::new( flash, flash_part_info_acid.offset(), flash_part_info_acid.len(), ); info!("Flash memory configured!"); 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(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).await; info!("ST7701S-based LCD display initialized!"); // RMK config let vial_config = VialConfig::new(VIAL_KEYBOARD_ID, VIAL_KEYBOARD_DEF, &[(0, 0), (1, 1)]); let storage_config = StorageConfig { start_addr: 0, num_sectors: { assert!( flash_part_info_rmk.len() as u32 % FlashStorage::SECTOR_SIZE == 0, "The size of the RMK partition must be a multiple of {} bytes. Current size: {}", FlashStorage::SECTOR_SIZE, flash_part_info_rmk.len() ); (flash_part_info_rmk.len() as u32 / FlashStorage::SECTOR_SIZE) as u8 }, ..Default::default() }; // Retrieve the hardware-unique MAC address. let mac_address = Efuse::read_base_mac_address(); static SERIAL_NUMBER: StaticCell> = StaticCell::new(); let serial_number = SERIAL_NUMBER.init_with(|| { /// A magic prefix string that is required for the device to be recognized by the Vial GUI. const VIAL_SERIAL_PREFIX: &str = "vial:f64c2b3c"; format!( "{VIAL_SERIAL_PREFIX}:acid:{:02x}", mac_address.iter().format("") ) .into_boxed_str() }); let device_config = DeviceConfig { vid: VIAL_VENDOR_ID, pid: VIAL_PRODUCT_ID, manufacturer: "", product_name: VIAL_KEYBOARD_NAME, serial_number, }; info!("RMK Device Config: {device_config:#04x?}"); let rmk_config = RmkConfig { device_config, vial_config, storage_config, ..Default::default() }; // Initialze keyboard stuffs // Initialize the storage and keymap let mut default_keymap = keymap::get_default_keymap(); let mut behavior_config = BehaviorConfig::default(); let mut per_key_config = PositionalConfig::default(); let (keymap, mut storage) = initialize_keymap_and_storage( &mut default_keymap, flash_part_rmk, &storage_config, &mut behavior_config, &mut per_key_config, ) .await; info!("Initialized keymap and storage for RMK!"); // 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()) .unwrap() .with_sda(peripherals.GPIO8) .with_scl(peripherals.GPIO9); let matrix_interrupt_low = Input::new(peripherals.GPIO7, InputConfig::default()); let mut matrix = IoeMatrix::new( matrix_interrupt_low, i2c.into_async(), DefaultDebouncer::new(), [I2C_ADDR_MATRIX_LEFT, I2C_ADDR_MATRIX_RIGHT], ) .await; let mut keyboard = Keyboard::new(&keymap); // Initialize the light controller info!("Keyboard initialized!"); static FRAMEBUFFER: StaticCell = StaticCell::new(); let framebuffer = FRAMEBUFFER.init(Framebuffer::new( 360 + /* TODO: Figure out why more bytes are needed: */ 8, 960, )); info!("Framebuffer created!"); // let window_size = [framebuffer.width, framebuffer.height]; let window_size = [framebuffer.height, framebuffer.width]; let framebuffer_ptr = FramebufferPtr(framebuffer.as_target_pixels() as _); 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, software_interrupt.software_interrupt0, software_interrupt.software_interrupt1, second_core_stack, move || { // static EXECUTOR: StaticCell> = StaticCell::new(); // let exec = EXECUTOR.init(InterruptExecutor::new( // software_interrupt.software_interrupt2, // )); // let spawner = exec.start(Priority::Priority3); // spawner.must_spawn(run_renderer_task()); static EXECUTOR: StaticCell = StaticCell::new(); let executor: &mut Executor = EXECUTOR.init(Executor::new()); executor.run(|spawner| { let slint_backend = SlintBackend { // peripherals: RefCell::new(Some(peripherals)), window_size, window: RefCell::new(None), framebuffer: framebuffer_ptr, quit_event_loop: Default::default(), events: Arc::new(critical_section::Mutex::new(RefCell::new(VecDeque::new()))), }; spawner.must_spawn(ui::run_renderer_task(slint_backend, flash_part_acid)); }); }, ); info!("Second core started!"); let mut user_controller = UserController::new(); 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. run_lcd(st7701s, framebuffer), 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_task ] .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; } } struct UserController { sub: ControllerSub, } impl UserController { fn new() -> Self { Self { sub: CONTROLLER_CHANNEL.subscriber().unwrap(), } } } impl Controller for UserController { type Event = ControllerEvent; async fn process_event(&mut self, event: Self::Event) { if let ControllerEvent::Key(keyboard_event, KeyAction::Single(Action::User(user_key_index))) = event && user_key_index == CustomKeycodes::FOCUS_LCD as u8 && keyboard_event.pressed { let enabled = !LCD_ENABLED.fetch_xor(true, Ordering::SeqCst); match enabled { false => { info!("Disabling LCD."); *rmk::channel::KEYBOARD_REPORT_SENDER.write().await = &rmk::channel::KEYBOARD_REPORT_RECEIVER; } true => { info!("Enabling LCD."); *rmk::channel::KEYBOARD_REPORT_SENDER.write().await = &KEYBOARD_REPORT_PROXY; } } } } async fn next_message(&mut self) -> Self::Event { self.sub.next_message_pure().await } } struct Framebuffer { width: u32, height: u32, dma_buf: Option, } impl Framebuffer { pub fn new(width: u32, height: u32) -> Self { let buffer_len = width as usize * height as usize * core::mem::size_of::(); // Allocate the framebuffer in the external PSRAM memory. // Note: We just leak this buffer. let buffer_ptr = unsafe { // ⚠️ Note: For chips that support DMA to/from PSRAM (ESP32-S3) DMA transfers to/from PSRAM // have extra alignment requirements. The address and size of the buffer pointed to by each // descriptor must be a multiple of the cache line (block) size. This is 32 bytes on ESP32-S3. PSRAM_ALLOCATOR.alloc_caps( MemoryCapability::External.into(), Layout::from_size_align(buffer_len, 32).unwrap(), ) }; let buffer = unsafe { core::slice::from_raw_parts_mut(buffer_ptr, buffer_len) }; let burst_config: BurstConfig = ExternalBurstConfig::Size16.into(); info!( "PSRAM SPI burst config: max_compatible_chunk_size={}", burst_config.max_compatible_chunk_size() ); let dma_buf_descs_len = esp_hal::dma::descriptor_count( buffer_len, burst_config.max_compatible_chunk_size(), false, ); // Descriptors are initialized by `DmaTxBuf::new`. let dma_buf_descs = Box::leak(vec![DmaDescriptor::EMPTY; dma_buf_descs_len].into_boxed_slice()); let dma_buf = DmaTxBuf::new(dma_buf_descs, buffer).unwrap(); Self { width, height, dma_buf: Some(dma_buf), } } pub fn as_target_pixels(&mut self) -> &mut [Rgb565Pixel] { bytemuck::cast_slice_mut::<_, Rgb565Pixel>(self.dma_buf.as_mut().unwrap().as_mut_slice()) } } #[embassy_executor::task] async fn run_lcd_task(st7701s: St7701s<'static, Blocking>, framebuffer: &'static mut Framebuffer) { run_lcd(st7701s, framebuffer).await } async fn run_lcd(mut st7701s: St7701s<'static, Blocking>, framebuffer: &'static mut Framebuffer) { loop { // Timer::after(Duration::from_millis(100)).await; // yield_now().await; SIGNAL_LCD_SUBMIT.wait().await; // TODO: Use bounce buffers: // https://docs.espressif.com/projects/esp-idf/en/v5.0/esp32s3/api-reference/peripherals/lcd.html#bounce-buffer-with-single-psram-frame-buffer // This can be implemented as a `DmaTxBuffer`. let transfer = match st7701s.dpi.send(false, framebuffer.dma_buf.take().unwrap()) { Err((error, result_dpi, result_dma_buf)) => { error!( "An error occurred while initiating transfer of the framebuffer to the LCD display: {error:?}" ); st7701s.dpi = result_dpi; framebuffer.dma_buf = Some(result_dma_buf); continue; } Ok(transfer) => transfer, }; // This could be used to allow other tasks to be executed on the first core, but that causes // the flash to be accessed, which interferes with the framebuffer transfer. // For that reason, it is disabled, and this task blocks the first core, until the transfer // is complete. #[cfg(not(feature = "limit-fps"))] while !transfer.is_done() { // Timer::after_millis(1).await; rmk::embassy_futures::yield_now().await; } let result; let dma_buf; (result, st7701s.dpi, dma_buf) = transfer.wait(); framebuffer.dma_buf = Some(dma_buf); SIGNAL_UI_RENDER.signal(()); if let Err(error) = result { error!( "An error occurred while transferring framebuffer to the LCD display: {error:?}" ); } } } // // TODO: Not needed currently. If it is ever enabled, don't forget to register it in Io. // #[handler] // #[ram] // TODO: Is this necessary? // fn interrupt_handler() { // // esp_println::println!( // // "GPIO Interrupt with priority {}", // // esp_hal::xtensa_lx::interrupt::get_level() // // ); // }