Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions gateware/src/top/polysyn/fw/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use tiliqua_hal::embedded_graphics::prelude::*;

use opts::persistence::*;
use hal::pca9635::Pca9635Driver;
use hal::tusb322::{TUSB322Driver, TUSB322Mode, AttachedState};
use hal::tusb322::{TUSB322Driver, TUSB322Mode, AttachedState, AccessoryType};

use tiliqua_fw::wavetable;

Expand Down Expand Up @@ -365,6 +365,7 @@ fn main() -> ! {
let mut last_jack = pmod.jack();

let mut usb_cc_attached_as_src = false;
let mut last_attached_state = AttachedState::NotAttached;

loop {

Expand All @@ -380,16 +381,28 @@ fn main() -> ! {

// Type-C hotplugging detection
//
// Only enable VBUS if the TUSB322 Type-C controller says we are attached as a source.
// This is an extra safeguard against applying VBUS if we're accidentally connecting host<->host.
// Only enable VBUS if the TUSB322 Type-C controller says we are attached as:
// - Attached.SRC
// - Attached.ACCESSORY && AccessoryType.DebugDfp
//
// It seems some USB-C adapters will enumerate as a DebugDfp accessory. Enabling VBUS
// is still safe in this situation, as we can only land in DebugDfp if the CC
// controller is sure we are a DFP. It's not sufficient to just check
// Attached.ACCESSORY as we could also enumerate as an UFP accessory against a host!
//
// This is essentially a safeguard against applying VBUS if we're accidentally connecting host<->host.
//

if let Ok(status) = tusb322.read_connection_status_control() {
// Only update on valid reads to reduce risk of unintended toggling mid-stream
let new_state = status.attached_state == AttachedState::AttachedSrc;
if new_state != usb_cc_attached_as_src {
info!("USB CC hotplug: {:?}", status);
usb_cc_attached_as_src = new_state;
if let Ok(ctrl) = tusb322.read_connection_status_control() {
if let Ok(conn) = tusb322.read_connection_status() {
// Only update on valid reads to reduce risk of unintended toggling mid-stream
if ctrl.attached_state != last_attached_state {
info!("USB hotplug: {:?} | {:?}", ctrl, conn);
last_attached_state = ctrl.attached_state;
}
let debug_dfp = (ctrl.attached_state == AttachedState::AttachedAccessory) &&
(conn.accessory == AccessoryType::DebugDfp);
usb_cc_attached_as_src = ctrl.attached_state == AttachedState::AttachedSrc || debug_dfp;
}
}
let usb_vbus_enabled = usb_cc_attached_as_src && (app.ui.opts.misc.usb_host.value == UsbHost::On);
Expand Down
Loading