Skip to content

Commit

Permalink
[SiVal, usbdev] Add harness to usbdev_test
Browse files Browse the repository at this point in the history
Signed-off-by: Douglas Reis <[email protected]>
  • Loading branch information
engdoreis committed Dec 3, 2024
1 parent 85c3069 commit 02e936f
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
8 changes: 7 additions & 1 deletion sw/device/tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4442,7 +4442,13 @@ opentitan_test(
EARLGREY_TEST_ENVS,
EARLGREY_SILICON_OWNER_ROM_EXT_ENVS,
),
fpga = fpga_params(tags = ["manual"]),
fpga = fpga_params(
tags = ["manual"],
test_cmd = """
--bootstrap="{firmware}"
""",
test_harness = "//sw/host/tests/chip/usb:usbdev_smoketest",
),
verilator = verilator_params(timeout = "long"),
deps = [
"//hw/top_earlgrey/sw/autogen:top_earlgrey",
Expand Down
17 changes: 17 additions & 0 deletions sw/host/tests/chip/usb/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,20 @@ rust_binary(
"@crate_index//:log",
],
)

rust_binary(
name = "usbdev_smoketest",
srcs = [
"usbdev_smoketest.rs",
],
deps = [
":usb",
"//sw/host/opentitanlib",
"@crate_index//:anyhow",
"@crate_index//:clap",
"@crate_index//:humantime",
"@crate_index//:log",
"@crate_index//:rusb",
"@crate_index//:serialport",
],
)
76 changes: 76 additions & 0 deletions sw/host/tests/chip/usb/usbdev_smoketest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

use anyhow::{bail, Result};
use clap::Parser;
use std::time::Duration;

use opentitanlib::execute_test;
use opentitanlib::io::uart::Uart;
use opentitanlib::test_utils::init::InitializeTest;
use opentitanlib::transport::common::uart::SerialPortUart;
use opentitanlib::uart::console::UartConsole;

use usb::UsbOpts;

#[derive(Debug, Parser)]
struct Opts {
#[command(flatten)]
init: InitializeTest,

/// Console/USB timeout.
#[arg(long, value_parser = humantime::parse_duration, default_value = "10s")]
timeout: Duration,

/// USB options.
#[command(flatten)]
usb: UsbOpts,
}

fn usbdev_echo(opts: &Opts, uart: &dyn Uart) -> Result<()> {
log::info!("waiting for device...");
let devices = opts.usb.wait_for_device(opts.timeout)?;
if devices.is_empty() {
bail!("no USB device found");
}

let ports = serialport::available_ports()?;
// In CI it is possible for several devices to match because we don't use a unique
// VID and PID, and all devices are visible (but not accessible) to every program.
// So with filter all vid:pid matches to try to open them: the ones belonging to
// other containers cannot be opened.
let usb_uart = ports
.iter()
.find_map(|port| {
let serialport::SerialPortType::UsbPort(info) = &port.port_type else {
return None;
};
if info.vid != opts.usb.vid || info.pid != opts.usb.pid {
return None;
}
SerialPortUart::open(&port.port_name, 115200).ok()
})
.ok_or(anyhow::anyhow!("Could not open any tty port"))?;

let mut buffer = [0u8; 256];
let len = usb_uart.read(&mut buffer)?;

usb_uart.write(&buffer[0..len])?;

let _ = UartConsole::wait_for(uart, r"PASS!", opts.timeout)?;
Ok(())
}

fn main() -> Result<()> {
let opts = Opts::parse();
opts.init.init_logging();
let transport = opts.init.init_target()?;

let uart = transport.uart("console")?;
let _ = UartConsole::wait_for(&*uart, r"Running [^\r\n]*", opts.timeout)?;

execute_test!(usbdev_echo, &opts, &*uart);

Ok(())
}

0 comments on commit 02e936f

Please sign in to comment.