add an ensure usb connection

ensure connection will not reset the core if it's already connected,
unlike the switch version which always resets the core even if it is
connected.
This commit is contained in:
bunnie 2022-06-23 02:26:12 +08:00
parent e2f335ec3a
commit e6e44d3dfb
3 changed files with 39 additions and 0 deletions

View file

@ -12,6 +12,8 @@ pub(crate) enum Opcode {
GetLedState,
/// Switch to a specified device core
SwitchCores,
/// Makes sure a given core is selected
EnsureCore,
/// Check which core is connected
WhichCore,
/// Restrict the debug core

View file

@ -29,6 +29,7 @@ impl UsbHid {
conn
}
}
/// this will always trigger a reset, even if it's the same core we're switching to
pub fn switch_to_core(&self, core: UsbDeviceType) -> Result<(), xous::Error> {
match send_message(
self.conn,
@ -50,6 +51,28 @@ impl UsbHid {
_ => panic!("Internal error: illegal return type"),
}
}
/// this will not trigger a reset if we're already on the requested core
pub fn ensure_core(&self, core: UsbDeviceType) -> Result<(), xous::Error> {
match send_message(
self.conn,
Message::new_blocking_scalar(
Opcode::EnsureCore.to_usize().unwrap(),
match core {
UsbDeviceType::Debug => 0,
UsbDeviceType::Hid => 1,
},
0, 0, 0
)
) {
Ok(xous::Result::Scalar1(code)) => {
match code {
0 => Ok(()),
_ => Err(xous::Error::InternalError)
}
}
_ => panic!("Internal error: illegal return type"),
}
}
pub fn get_current_core(&self) -> Result<UsbDeviceType, xous::Error> {
match send_message(
self.conn,

View file

@ -318,6 +318,20 @@ fn main() -> ! {
}
xous::return_scalar(msg.sender, 0).unwrap();
}),
Some(Opcode::EnsureCore) => msg_blocking_scalar_unpack!(msg, core, _, _, _, {
if core == 1 {
if !usbmgmt.is_device_connected() {
log::info!("Connecting USB device core; disconnecting debug USB core");
usbmgmt.connect_device_core(true);
}
} else {
if usbmgmt.is_device_connected() {
log::info!("Connecting debug core; disconnecting USB device core");
usbmgmt.connect_device_core(false);
}
}
xous::return_scalar(msg.sender, 0).unwrap();
}),
Some(Opcode::WhichCore) => msg_blocking_scalar_unpack!(msg, _, _, _, _, {
if usbmgmt.is_device_connected() {
xous::return_scalar(msg.sender, 1).unwrap();