Skip to content

Commit

Permalink
USB route added 'flash' option to API
Browse files Browse the repository at this point in the history
The boot pin state is added to the USB state, as its closely related to
the different roles a given node fulfills.
  • Loading branch information
svenrademakers committed Nov 23, 2023
1 parent ebb4240 commit 22d0139
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
20 changes: 13 additions & 7 deletions src/api/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,16 @@ fn get_encoding_param(query: &Query) -> LegacyResult<Encoding> {
/// switches the USB configuration.
/// API values are mapped to the `UsbConfig` as followed:
///
/// | i32 | Mode | Route |
/// |-----|--------|-------|
/// | 0 | Host | USB-A |
/// | 1 | Device | USB-A |
/// | 2 | Host | BMC |
/// | 3 | Device | BMC |
/// | i32 | Mode | Route |
/// |-----|--------------|-------|
/// | 0 | Host | USB-A |
/// | 1 | Device | USB-A |
/// | 2 | Flash host | USB-A |
/// | 3 | Flash device | USB-A |
/// | 4 | Host | BMC |
/// | 5 | Device | BMC |
/// | 6 | Flash host | BMC |
/// | 7 | Flash device | BMC |
///
async fn set_usb_mode(bmc: &BmcApplication, query: Query) -> LegacyResult<()> {
let node = get_node_param(&query)?;
Expand All @@ -487,7 +491,7 @@ async fn set_usb_mode(bmc: &BmcApplication, query: Query) -> LegacyResult<()> {

let mode = UsbMode::from_api_mode(mode_num);

let route = if (mode_num >> 1) & 0x1 == 1 {
let route = if (mode_num >> 2) & 0x1 == 1 {
UsbRoute::Bmc
} else {
UsbRoute::UsbA
Expand All @@ -497,6 +501,7 @@ async fn set_usb_mode(bmc: &BmcApplication, query: Query) -> LegacyResult<()> {
(UsbMode::Device, UsbRoute::UsbA) => UsbConfig::UsbA(node),
(UsbMode::Device, UsbRoute::Bmc) => UsbConfig::Bmc(node),
(UsbMode::Host, route) => UsbConfig::Node(node, route),
(UsbMode::Flash, route) => UsbConfig::Flashing(node, route),
};

bmc.configure_usb(cfg)
Expand All @@ -513,6 +518,7 @@ async fn get_usb_mode(bmc: &BmcApplication) -> impl Into<LegacyResponse> {
UsbConfig::UsbA(node) => (node, UsbMode::Device, UsbRoute::UsbA),
UsbConfig::Bmc(node) => (node, UsbMode::Device, UsbRoute::Bmc),
UsbConfig::Node(node, route) => (node, UsbMode::Host, route),
UsbConfig::Flashing(node, route) => (node, UsbMode::Flash, route),
};

json!(
Expand Down
11 changes: 5 additions & 6 deletions src/app/bmc_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub enum UsbConfig {
Bmc(NodeId),
/// NodeId is host, [UsbRoute] is configured for device
Node(NodeId, UsbRoute),
Flashing(NodeId, UsbRoute),
}

/// Encodings used when reading from a serial port
Expand Down Expand Up @@ -171,11 +172,13 @@ impl BmcApplication {
let (mode, dest, route) = match config {
UsbConfig::UsbA(device) => (UsbMode::Device, device, UsbRoute::UsbA),
UsbConfig::Bmc(device) => (UsbMode::Device, device, UsbRoute::Bmc),
UsbConfig::Flashing(device, route) => (UsbMode::Flash, device, route),
UsbConfig::Node(host, route) => (UsbMode::Host, host, route),
};

self.pin_controller.set_usb_route(route).await?;
self.pin_controller.select_usb(dest, mode)?;

Ok(())
}

Expand Down Expand Up @@ -218,12 +221,8 @@ impl BmcApplication {

sleep(REBOOT_DELAY).await;

let config = match router {
UsbRoute::Bmc => UsbConfig::Bmc(node),
UsbRoute::UsbA => UsbConfig::UsbA(node),
};
self.usb_boot(node, true).await?;
self.configure_usb_internal(config).await?;
self.configure_usb_internal(UsbConfig::Flashing(node, router))
.await?;

log::info!("Prerequisite settings toggled, powering on...");
self.activate_slot(node.to_bitfield(), node.to_bitfield())
Expand Down
4 changes: 3 additions & 1 deletion src/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ pub enum UsbRoute {
pub enum UsbMode {
Host,
Device,
Flash,
}

impl UsbMode {
pub fn from_api_mode(value: i32) -> Self {
match value & 0x1 {
match value & 0b11 {
0 => UsbMode::Host,
1 => UsbMode::Device,
2 => UsbMode::Flash,
_ => unreachable!(),
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/hal/pin_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,15 @@ impl PinController {

let vbus = match mode {
UsbMode::Host => node.to_inverse_bitfield(),
UsbMode::Device => 0b1111,
UsbMode::Device | UsbMode::Flash => 0b1111,
};
self.usb_vbus.set_values(vbus)
self.usb_vbus.set_values(vbus)?;

if UsbMode::Flash == mode {
self.set_usb_boot(node.to_bitfield(), node.to_bitfield())?;
}

Ok(())
}

/// Set which way the USB is routed: USB-A ↔ PORTx (`UsbRoute::UsbA`) or BMC ↔ PORTx
Expand Down

0 comments on commit 22d0139

Please sign in to comment.