Skip to content

Commit 1e08b56

Browse files
committed
Add interface for hard resetting USB test class
1 parent 4dbeb92 commit 1e08b56

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/test_class.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::device::{StringDescriptors, UsbDevice, UsbDeviceBuilder, UsbVidPid};
55
use crate::Result;
66
use core::cell::UnsafeCell;
77
use core::cmp;
8+
use core::marker::PhantomData;
89

910
#[cfg(feature = "test-class-high-speed")]
1011
mod sizes {
@@ -24,9 +25,14 @@ mod sizes {
2425

2526
static mut CONTROL_BUFFER: UnsafeCell<[u8; 256]> = UnsafeCell::new([0; 256]);
2627

28+
pub trait HardwareSupport {
29+
/// Hard reset the test device
30+
fn hard_reset() -> !;
31+
}
32+
2733
/// Test USB class for testing USB driver implementations. Supports various endpoint types and
2834
/// requests for testing USB peripheral drivers on actual hardware.
29-
pub struct TestClass<'a, B: UsbBus> {
35+
pub struct TestClass<'a, B: UsbBus, H: HardwareSupport> {
3036
custom_string: StringIndex,
3137
interface_string: StringIndex,
3238
iface: InterfaceNumber,
@@ -45,6 +51,7 @@ pub struct TestClass<'a, B: UsbBus> {
4551
expect_bulk_out: bool,
4652
expect_interrupt_in_complete: bool,
4753
expect_interrupt_out: bool,
54+
hardware: PhantomData<H>,
4855
}
4956

5057
pub const VID: u16 = 0x16c0;
@@ -60,13 +67,14 @@ pub const REQ_READ_BUFFER: u8 = 2;
6067
pub const REQ_WRITE_BUFFER: u8 = 3;
6168
pub const REQ_SET_BENCH_ENABLED: u8 = 4;
6269
pub const REQ_READ_LONG_DATA: u8 = 5;
70+
pub const REQ_HARD_RESET: u8 = 6;
6371
pub const REQ_UNKNOWN: u8 = 42;
6472

6573
pub const LONG_DATA: &[u8] = &[0x17; 257];
6674

67-
impl<B: UsbBus> TestClass<'_, B> {
75+
impl<B: UsbBus, H: HardwareSupport> TestClass<'_, B, H> {
6876
/// Creates a new TestClass.
69-
pub fn new(alloc: &UsbBusAllocator<B>) -> TestClass<'_, B> {
77+
pub fn new(alloc: &UsbBusAllocator<B>) -> TestClass<'_, B, H> {
7078
TestClass {
7179
custom_string: alloc.string(),
7280
interface_string: alloc.string(),
@@ -91,6 +99,7 @@ impl<B: UsbBus> TestClass<'_, B> {
9199
expect_bulk_out: false,
92100
expect_interrupt_in_complete: false,
93101
expect_interrupt_out: false,
102+
hardware: PhantomData,
94103
}
95104
}
96105

@@ -214,7 +223,7 @@ impl<B: UsbBus> TestClass<'_, B> {
214223
}
215224
}
216225

217-
impl<B: UsbBus> UsbClass<B> for TestClass<'_, B> {
226+
impl<B: UsbBus, H: HardwareSupport> UsbClass<B> for TestClass<'_, B, H> {
218227
fn reset(&mut self) {
219228
self.len = 0;
220229
self.i = 0;
@@ -335,6 +344,9 @@ impl<B: UsbBus> UsbClass<B> for TestClass<'_, B> {
335344
xfer.accept()
336345
.expect("control_out REQ_SET_BENCH_ENABLED failed");
337346
}
347+
REQ_HARD_RESET => {
348+
H::hard_reset();
349+
}
338350
_ => xfer.reject().expect("control_out reject failed"),
339351
}
340352
}

0 commit comments

Comments
 (0)