add auto-trng feature to the USB stack

for compact/quick testing of TRNG in a harness
This commit is contained in:
bunnie 2024-06-13 00:37:07 +08:00
parent 61ed5aa145
commit 82e5594da4
2 changed files with 51 additions and 2 deletions

View file

@ -71,6 +71,7 @@ cramium-soc = [
]
minimal = ["mass-storage"]
mass-storage = ["usbd_scsi", "usbd_mass_storage"]
auto-trng = []
mjolnir = [
] # the big hammer for debugging Spinal USB issues. A raw memory dump of config and descriptor space. Use with care.
default = []

View file

@ -10,6 +10,7 @@ use cram_hal_service::api::KeyMap;
use cram_hal_service::keyboard;
#[cfg(feature = "cramium-soc")]
use cram_hal_service::trng;
use cram_hal_service::trng::api::TrngTestMode;
#[cfg(feature = "cramium-soc")]
use cramium_hal::usb::driver::CorigineUsb;
#[cfg(feature = "cramium-soc")]
@ -401,7 +402,10 @@ pub(crate) fn main_hw() -> ! {
let mut serial_listen_mode: SerialListenMode = SerialListenMode::NoListener;
let mut serial_buf = Vec::<u8>::new();
let mut serial_rx_trigger = false; // when true, the condition was met to pass data to the listener (but the listener was not yet installed)
#[cfg(not(feature = "cramium-soc"))]
let trng = trng::Trng::new(&xns).unwrap();
#[cfg(feature = "cramium-soc")]
let mut trng = trng::Trng::new(&xns).unwrap();
let mut serial_trng_buf = Vec::<u8>::new();
let serial_trng_interval = Arc::new(AtomicU32::new(0));
let mut serial_trng_cid: Option<xous::CID> = None;
@ -427,10 +431,54 @@ pub(crate) fn main_hw() -> ! {
100, // 100 * 64 bytes = 6.4kb, quite the backlog
);
// track which view is visible on the device core
#[cfg(not(feature = "minimal"))]
#[cfg(all(not(feature = "minimal"), not(feature = "cramium-soc")))]
let mut view = Views::FidoWithKbd;
#[cfg(feature = "cramium-soc")]
usb_dev.force_reset().ok(); // hard-coded to match the view specifier above
let mut view = Views::Serial;
#[cfg(feature = "cramium-soc")]
serial_device.force_reset().ok(); // hard-coded to match the view specifier above
#[cfg(feature = "auto-trng")]
std::thread::spawn({
let conn = cid;
move || {
let tt = ticktimer_server::Ticktimer::new().unwrap();
loop {
tt.sleep_ms(2_000).ok();
let state = match xous::send_message(
conn,
xous::Message::new_blocking_scalar(Opcode::LinkStatus.to_usize().unwrap(), 0, 0, 0, 0),
) {
Ok(xous::Result::Scalar1(code)) => match code {
0 => UsbDeviceState::Default,
1 => UsbDeviceState::Addressed,
2 => UsbDeviceState::Configured,
3 => UsbDeviceState::Suspend,
_ => panic!("Internal error: illegal status code"),
},
_ => panic!("Internal error: illegal return type"),
};
if state == UsbDeviceState::Configured {
log::info!("Core connected");
break;
}
}
tt.sleep_ms(3_000).ok();
log::info!("Starting serial sender");
xous::send_message(
conn,
xous::Message::new_scalar(
Opcode::SerialHookTrngSender.to_usize().unwrap(),
TrngTestMode::Raw.to_usize().unwrap(),
0,
0,
0,
),
)
.unwrap();
}
});
let mut led_state: KeyboardLedsReport = KeyboardLedsReport::default();
let mut fido_listener: Option<xous::MessageEnvelope> = None;