add command channel and reset/bootloader command

This commit is contained in:
ktims committed 2026-11-04 16:29:26 -07:00
1 parent 93a20cf8e1
commit 245c45c682
7 files changed
+197 -45

No files matched your search

+6 -5
View File
@@ -9,18 +9,19 @@ ak4490 = []
cs4398 = []
nodac = []
wm8904 = []
hid = [ "dep:usbd-hid" ]
evk = [ "wm8904" ]
hid = ["dep:usbd-hid", "dep:deku"]
evk = ["wm8904"]
[dependencies]
shared = { path="../shared" }
shared = { path = "../shared" }
atomic = "0.6.1"
bbqueue = "0.7.0"
bytemuck = { version = "1.25.0", features = ["derive"] }
# bytemuck = { version = "1.25.0", features = ["derive"] }
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7.5"
defmt = "1.0.1"
defmt-rtt = "1.1.0"
deku = { version = "0.20.3", optional = true, default-features = false }
embedded-hal = "0.2.7"
embedded-io = "0.7.1"
log-to-defmt = "0.1.0"
@@ -32,7 +33,7 @@ panic-probe = { version = "1.0.0", features = ["print-defmt"] }
static_cell = "2.1.1"
usb-device = { version = "0.3", features = ["control-buffer-256"] }
usbd-hid = { version = "0.10.0", optional = true }
usbd-uac2 = { version = "0.1.2", features = ["defmt"]}
usbd-uac2 = { version = "0.1.2", features = ["defmt"] }
[profile.release]
opt-level = "z"
+67 -11
View File
@@ -10,7 +10,7 @@ fn panic() -> ! {
use atomic::Atomic;
use core::ptr;
use core::sync::atomic::AtomicPtr;
use core::sync::atomic::{AtomicBool, AtomicI32, AtomicUsize, Ordering};
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use cortex_m_rt::entry;
use defmt;
use defmt::debug;
@@ -44,7 +44,7 @@ use crate::hw::led1;
use crate::hw::led2;
use crate::traits::Dac;
use shared::AudioState;
use shared::hid::AudioTelemetryReport;
use shared::hid::{AudioDeviceCommandReport, AudioTelemetryReport};
#[cfg(feature = "ak4490")]
pub mod dac {
@@ -107,6 +107,7 @@ 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;
struct CodecPins {
reset: Pin<pins::Pio0_3, Gpio<Output>>,
@@ -174,6 +175,19 @@ static PERF: PerfCounters = PerfCounters {
// fb: AtomicI32::new(0),
};
fn boot_to_bootloader() -> ! {
cortex_m::interrupt::disable();
unsafe {
let pmc = &*pac::PMC::ptr();
// https://community.nxp.com/t5/LPC-Microcontrollers/Configure-LPC55-to-enter-re-invoke-ROM-Bootloader-from/m-p/966990?profile.language=en
// If you write the following register, it will be set to boot from the ROM:
// PMC->AOREG1 |= (0x0A << 16); //Set to boot from ROM
// NVIC_SystemReset(); //Reset‍‍
pmc.aoreg1.modify(|r, w| w.bits(r.bits() | (0x0a << 16)));
}
cortex_m::peripheral::SCB::sys_reset()
}
fn build_telemetry_report<D: Dac<I>, I>(
perf: &PerfCounters,
audio: &Audio<D, I>,
@@ -907,7 +921,17 @@ fn main() -> ! {
);
let mut uac2 = config.build(&usb_bus).unwrap();
#[cfg(feature = "hid")]
let mut hid = HIDClass::new_ep_in(&usb_bus, AudioTelemetryReport::desc(), HID_INTERVAL_MS);
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,
);
(hid, hid_cmd)
};
let mut usb_dev = usbd_uac2::builder(&usb_bus, UsbVidPid(0x1209, 0xcc1d))
.strings(&[StringDescriptors::default()
@@ -934,7 +958,7 @@ fn main() -> ! {
let mut poll_all = move || {
// Poll active USB classes
#[cfg(feature = "hid")]
usb_dev.poll(&mut [&mut uac2, &mut hid]);
usb_dev.poll(&mut [&mut uac2, &mut hid, &mut hid_cmd]);
#[cfg(not(feature = "hid"))]
usb_dev.poll(&mut [&mut uac2]);
@@ -948,15 +972,47 @@ fn main() -> ! {
}
#[cfg(feature = "hid")]
if hid_update_timer.wait().is_ok() {
let report = build_telemetry_report(&PERF, uac2.handler());
if let Err(e) = hid.push_input(&report) {
if e != UsbError::WouldBlock {
defmt::error!("Failed to send HID report: {:?}", e);
{
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(),
}
}
// LPC55 CTIMER is not periodic; restart manually
hid_update_timer.start(Microseconds::new(HID_INTERVAL_MS as u32 * 1000));
if hid_update_timer.wait().is_ok() {
let report = build_telemetry_report(&PERF, uac2.handler());
if let Err(e) = hid.push_input(&report) {
if e != UsbError::WouldBlock {
defmt::error!("Failed to send HID report: {:?}", e);
}
}
// 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))
{
if let Ok(cmd) = cmd.try_into() {
handle_cmd(cmd)
}
}
}
Err(UsbError::WouldBlock) => {}
Err(e) => defmt::error!("Invalid report: {:?}", e),
}
}
};