config: add persistent flash-backed configuration with a HID command interface and CLI management
Build and Release Firmware / firmware (ak4490) (push) Successful in 50s
Build and Release Firmware / firmware (cs4398) (push) Successful in 44s
Build and Release Firmware / firmware (evk) (push) Successful in 43s
Build and Release Firmware / cli (aarch64-unknown-linux-musl) (push) Successful in 1m8s
Build and Release Firmware / cli (x86_64-pc-windows-gnu) (push) Successful in 1m32s
Build and Release Firmware / cli (x86_64-unknown-linux-musl) (push) Successful in 1m6s
Build and Release Firmware / release (push) Successful in 7s

This commit is contained in:
ktims committed 2026-11-11 18:09:07 -07:00
1 parent 7b49f116d4
commit 44679cda2c
23 files changed
+3483 -148

No files matched your search

+118 -45
View File
@@ -24,6 +24,8 @@ use hal::typestates::pin::state::Gpio;
use lpc55_hal as hal;
use pac::interrupt;
use static_cell::StaticCell;
#[cfg(feature = "hid")]
use usb_device::device::UsbDeviceState;
use usb_device::{
bus::{self},
device::{StringDescriptors, UsbVidPid},
@@ -43,7 +45,13 @@ use crate::hw::led1;
use crate::hw::led2;
use crate::traits::Dac;
use shared::AudioState;
use shared::hid::{AudioDeviceCommandReport, AudioTelemetryReport};
#[cfg(feature = "hid")]
use shared::config::ConfigString;
#[cfg(feature = "hid")]
use shared::hardware::HardwareInfo;
use shared::hid::AudioTelemetryReport;
#[cfg(feature = "hid")]
use shared::hid::DeviceCommandReport;
#[cfg(feature = "ak4490")]
pub mod dac {
@@ -66,7 +74,10 @@ pub mod dac {
pub use self::wm8904::Wm8904Dac as DacImpl;
}
mod config;
mod dma;
#[cfg(feature = "hid")]
mod hid_config;
mod hw;
mod traits;
@@ -106,7 +117,39 @@ const USB_FRAME_RATE: u32 = 8000; // microframe rate: 8000 for HS, 1000 for FS
// const NODATA_TIMEOUT_FRAMES: usize = SAMPLE_RATE as usize / 100; // ~100ms
const HID_INTERVAL_MS: u8 = 10;
const HID_CMD_INTERVAL_MS: u8 = 100;
const HID_CMD_INTERVAL_MS: u8 = 10;
const FLASH_PAGE_SIZE: usize = 512;
const CONFIG_LENGTH: usize = 32 * 1024;
const CONFIG_SLOT_COUNT: usize = 2;
// First page is reserved for slot selection, reserve two slots so remainder / 2 is aligned
const CONFIG_RESERVED: usize = 2 * FLASH_PAGE_SIZE;
const SLOT_SIZE: usize = (CONFIG_LENGTH - CONFIG_RESERVED) / CONFIG_SLOT_COUNT;
// Ensure config page alignment
const _: () = assert!(
CONFIG_RESERVED.is_multiple_of(FLASH_PAGE_SIZE),
"Reserved space must align to flash page size"
);
const _: () = assert!(
SLOT_SIZE.is_multiple_of(FLASH_PAGE_SIZE),
"Slot size must be a multiple of flash page size"
);
const _: () = assert!(
CONFIG_SLOT_COUNT.is_multiple_of(2),
"Slot count must be even"
);
unsafe extern "C" {
#[link_name = "_config_start"]
static CONFIG_START: u8;
}
pub fn config_base() -> usize {
core::ptr::addr_of!(CONFIG_START) as usize
}
struct CodecPins {
#[allow(dead_code)]
@@ -842,6 +885,22 @@ fn main() -> ! {
.system_frequency(96.MHz())
.configure(&mut anactrl, &mut pmc, &mut syscon)
.unwrap();
// Flash controller commands require a core clock of at most 100 MHz, so the config is read
// before the system PLL takes the core to 150 MHz.
let flash = hal.flash.enabled(&mut syscon);
let mut flash = hal::FlashGordon::new(flash);
defmt::info!("config: loading");
let (running_config, active_slot) = config::load(&mut flash);
defmt::info!(
"config: loaded slot={} vid={:04x} pid={:04x}",
active_slot,
running_config.usb_vendor_id,
running_config.usb_product_id
);
#[cfg(not(feature = "hid"))]
let _ = active_slot;
hw::init_sys_pll1();
#[cfg(feature = "evk")]
hw::init_audio_pll();
@@ -921,24 +980,46 @@ fn main() -> ! {
#[cfg(feature = "hid")]
let (mut hid, mut hid_cmd) = {
let hid = HIDClass::new_ep_in(&usb_bus, AudioTelemetryReport::desc(), HID_INTERVAL_MS);
// Linux's usbhid refuses to bind an interface without an interrupt IN endpoint
// (no hidraw node), so the command interface needs one even though it is unused.
let hid_cmd = HIDClass::new(
&usb_bus,
AudioDeviceCommandReport::desc(),
HID_CMD_INTERVAL_MS,
);
// Commands arrive as feature reports over the control pipe; Linux's usbhid needs an
// interrupt IN endpoint to bind the interface, and replies use it.
let hid_cmd =
HIDClass::new_ep_in(&usb_bus, DeviceCommandReport::desc(), HID_CMD_INTERVAL_MS);
(hid, hid_cmd)
};
let mut usb_dev = usbd_uac2::builder(&usb_bus, UsbVidPid(0x1209, 0xcc1d))
.strings(&[StringDescriptors::default()
.manufacturer("VE7XEN")
.product("Guac Tortilla")])
.unwrap()
.max_packet_size_0(64)
.unwrap()
.build();
#[cfg(feature = "hid")]
let hardware_info = HardwareInfo {
uuid: hal::uuid(),
dac: ConfigString::try_from(uac2.handler().dac.name()).unwrap_or_default(),
version: ConfigString::try_from(env!("GUAC_VERSION")).unwrap_or_default(),
};
#[cfg(feature = "hid")]
defmt::info!(
"hardware: uuid={=[u8]:02x} dac={=str} firmware={=str}",
&hardware_info.uuid[..],
hardware_info.dac.as_str(),
hardware_info.version.as_str()
);
let mut usb_dev = usbd_uac2::builder(
&usb_bus,
UsbVidPid(running_config.usb_vendor_id, running_config.usb_product_id),
)
.strings(&[{
let descriptors = StringDescriptors::default()
.manufacturer(running_config.usb_manufacturer.as_str())
.product(running_config.usb_product.as_str());
match &running_config.usb_serial {
Some(serial) => descriptors.serial_number(serial.as_str()),
None => descriptors,
}
}])
.unwrap()
.max_packet_size_0(64)
.unwrap()
.build();
// 1. Initialize the HID timer conditionally outside the closure
#[cfg(feature = "hid")]
@@ -952,6 +1033,16 @@ fn main() -> ! {
timer
};
#[cfg(feature = "hid")]
let mut config_state = config::ConfigState {
running: running_config.clone(),
active_slot,
};
#[cfg(feature = "hid")]
let mut flash_state = config::FlashState { flash };
#[cfg(feature = "hid")]
let mut command_state = hid_config::CommandState::new();
// 2. Single consolidated poll_all closure
let mut poll_all = move || {
// Poll active USB classes
@@ -975,22 +1066,6 @@ fn main() -> ! {
#[cfg(feature = "hid")]
{
use deku::DekuContainerRead;
use shared::AudioDeviceCommand;
fn handle_cmd(cmd: AudioDeviceCommand) {
use shared::AudioDeviceCommand;
match cmd {
AudioDeviceCommand::Noop => {}
AudioDeviceCommand::Reset => {
cortex_m::interrupt::disable();
cortex_m::peripheral::SCB::sys_reset();
}
AudioDeviceCommand::Bootloader => boot_to_bootloader(),
}
}
if hid_update_timer.wait().is_ok() {
let report = build_telemetry_report(&PERF, uac2.handler());
if let Err(e) = hid.push_input(&report)
@@ -1001,19 +1076,17 @@ fn main() -> ! {
// LPC55 CTIMER is not periodic; restart manually
hid_update_timer.start(Microseconds::new(HID_INTERVAL_MS as u32 * 1000));
}
let mut cmd_report_buf = [0u8; core::mem::size_of::<AudioDeviceCommandReport>()];
match hid_cmd.pull_raw_output(&mut cmd_report_buf) {
Ok(len) => {
if let Ok((_left, cmd)) =
AudioDeviceCommandReport::from_bytes((&cmd_report_buf[0..len], 0))
&& let Ok(cmd) = cmd.try_into()
{
handle_cmd(cmd)
}
}
Err(UsbError::WouldBlock) => {}
Err(e) => defmt::error!("Invalid report: {:?}", e),
if usb_dev.state() != UsbDeviceState::Configured {
command_state.reset();
}
hid_config::poll(
&mut hid_cmd,
&mut command_state,
&mut config_state,
&mut flash_state,
&hardware_info,
);
}
};