add USB unplug/replug handling & stack resetting

This commit is contained in:
bunnie 2025-01-03 04:06:48 +08:00
parent 71ac106ee7
commit af6289b8e4
3 changed files with 56 additions and 25 deletions

View file

@ -67,6 +67,10 @@ pub(crate) enum Opcode {
#[cfg(feature = "mass-storage")]
ResetBlockDevice = 1026,
/// Platform-specific messages for callback handlers
#[cfg(feature = "cramium-soc")]
PmicIrq = 1536,
// HIDv2
/// Read a HID report
HIDReadReport = 1027,

View file

@ -320,4 +320,31 @@ impl<'a> CramiumUsb<'a> {
self.irq_req = Some(request_type);
self.irq_csr.wfo(utra::irqarray1::EV_SOFT_TRIGGER, SW_IRQ_MASK);
}
/// Process an unplug event
pub fn unplug(&mut self) {
// disable all interrupts so we can safely go through initialization routines
self.irq_csr.wo(utra::irqarray1::EV_ENABLE, 0);
self.wrapper.hw.lock().unwrap().reset();
self.wrapper.hw.lock().unwrap().init();
self.wrapper.hw.lock().unwrap().start();
self.wrapper.hw.lock().unwrap().update_current_speed();
// reset all shared data structures
self.device.force_reset().ok();
self.fido_tx_queue = RefCell::new(VecDeque::new());
self.kbd_tx_queue = RefCell::new(VecDeque::new());
self.irq_req = None;
self.wrapper.event = None;
self.wrapper.address_is_set.store(false, Ordering::SeqCst);
self.wrapper.ep_out_ready = (0..cramium_hal::usb::driver::CRG_EP_NUM + 1)
.map(|_| core::sync::atomic::AtomicBool::new(false))
.collect::<Vec<_>>()
.into_boxed_slice();
// re-enable IRQs
self.irq_csr.wo(utra::irqarray1::EV_PENDING, 0xFFFF_FFFF);
self.irq_csr.wo(utra::irqarray1::EV_ENABLE, CORIGINE_IRQ_MASK | SW_IRQ_MASK);
}
}

View file

@ -240,31 +240,16 @@ pub(crate) fn main_hw() -> ! {
}
});
std::thread::spawn({
move || {
log::info!("Connecting to I2C");
let mut i2c = cram_hal_service::I2c::new();
let mut pmic = cramium_hal::axp2101::Axp2101::new(&mut i2c).expect("couldn't open PMIC");
let iox = cram_hal_service::IoxHal::new();
log::info!("AXP2101 config: {:?}", pmic);
let tt = ticktimer::Ticktimer::new().unwrap();
pmic.setup_vbus_irq(&mut i2c, cramium_hal::axp2101::VbusIrq::InsertAndRemove)
.expect("couldn't setup IRQ");
// PB13 is the IRQ input
loop {
tt.sleep_ms(500).ok();
let status = pmic.get_vbus_irq_status(&mut i2c).unwrap();
log::info!(
"Status: {:?}, {:?}",
status,
iox.get_gpio_pin_value(cramium_hal::iox::IoxPort::PB, 13)
);
if status != VbusIrq::None {
pmic.clear_vbus_irq_pending(&mut i2c).expect("couldn't clear pending VBUS IRQ");
}
}
}
});
log::info!("Registering PMIC handler to detect USB plug/unplug events");
let iox = cram_hal_service::iox_lib::IoxHal::new();
cramium_hal::board::setup_pmic_irq(
&iox,
api::SERVER_NAME_USB_DEVICE,
Opcode::PmicIrq.to_usize().unwrap(),
);
let mut i2c = cram_hal_service::I2c::new();
let mut pmic = cramium_hal::axp2101::Axp2101::new(&mut i2c).expect("couldn't open PMIC");
pmic.setup_vbus_irq(&mut i2c, cramium_hal::axp2101::VbusIrq::Remove).expect("couldn't setup IRQ");
log::info!("Entering main loop");
@ -275,6 +260,21 @@ pub(crate) fn main_hw() -> ! {
let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()).unwrap_or(Opcode::InvalidCall);
log::info!("{:?}", opcode);
match opcode {
Opcode::PmicIrq => match pmic.get_vbus_irq_status(&mut i2c).unwrap() {
VbusIrq::Insert => {
log::error!("VBUS insert reported by PMIC, but we didn't ask for the event!");
}
VbusIrq::Remove => {
log::info!("VBUS removed. Resetting stack.");
cu.unplug();
}
VbusIrq::InsertAndRemove => {
panic!("Unexpected report from vbus_irq status");
}
VbusIrq::None => {
log::warn!("Received an interrupt but no actual event reported");
}
},
Opcode::U2fRxDeferred => {
// notify the event listener, if any
if observer_conn.is_some() && observer_op.is_some() {