fixes for USB HS in baremetal

This commit is contained in:
bunnie 2025-09-08 09:50:17 +08:00
parent a4f1960e34
commit d07f74ab99
5 changed files with 42 additions and 16 deletions

View file

@ -54,7 +54,15 @@ nto-bio = [
# selects features for EVB target (defaults to baosec)
nto-evb = []
nto-gfx = ["ux-api/loader-baosec"]
nto-usb = ["nto-gfx", "simple-fatfs", "sha2", "digest", "ed25519-dalek"]
nto-usb = [
"nto-gfx",
"simple-fatfs",
"sha2",
"digest",
"ed25519-dalek",
"cramium-hal/debug-print-usb",
"cramium-hal/verbose-debug",
]
verilator-only = []
default = ["nto-usb"]

View file

@ -395,7 +395,7 @@ pub fn clk_to_fd(fd_in_mhz: u32, desired_mhz: u32) -> Option<(u32, i32)> {
/// (min cycle, fd, actual freq)
/// *tested*
pub fn clk_to_per(top_in_mhz: u32, perclk_in_mhz: u32) -> Option<(u8, u8, u32)> {
let fd_platonic = ((256 * perclk_in_mhz) / (top_in_mhz / 2)).max(256);
let fd_platonic = ((256 * perclk_in_mhz) / (top_in_mhz / 2)).min(256);
if fd_platonic > 0 {
let fd = fd_platonic - 1;
let min_cycle = (2 * (256 / (fd + 1))).max(1);

View file

@ -55,12 +55,10 @@ pub unsafe fn init_usb() {
disk.fill(0);
// setup MBR
disk[..MBR_TEMPLATE.len()].copy_from_slice(&MBR_TEMPLATE);
/*
//set block size
disk[0xb..0xd].copy_from_slice(&SECTOR_SIZE.to_le_bytes());
//set storage size
disk[0x20..0x24].copy_from_slice(&(RAMDISK_LEN as u32 / SECTOR_SIZE as u32).to_le_bytes());
*/
// populate an initial "windows-friendly" FAT layout, generated by plugging the empty disk
// into windows and letting it...do its thing, then snapshotting the data.
disk[TABLE_OFFSET_FAT1..TABLE_OFFSET_FAT1 + FAT_TABLE.len()].copy_from_slice(&FAT_TABLE);

View file

@ -123,8 +123,8 @@ pub fn do_update() {
let new_portsc = usb.portsc_val();
// alternately, break out of the loop when USB is disconnected
if new_portsc != portsc {
crate::println!("PP: {:x}", portsc);
portsc = new_portsc;
crate::println!("PP: {:x}", portsc);
if portsc == DISCONNECT_STATE && new_usb_state == UsbDeviceState::Configured {
break;
}

View file

@ -117,9 +117,12 @@ pub(crate) const EP1_OUT_BUF_LEN: usize = 1024;
pub(crate) const MASS_STORAGE_EPADDR_IN: u8 = 0x81;
pub(crate) const MASS_STORAGE_EPADDR_OUT: u8 = 0x01;
const FS_MAX_PKT_SIZE: usize = 64;
const HS_MAX_PKT_SIZE: usize = 512;
pub(crate) fn enable_mass_storage_eps(this: &mut CorigineUsb, ep_num: u8) {
this.ep_enable(ep_num, USB_RECV, 64, EpType::BulkOutbound);
this.ep_enable(ep_num, USB_SEND, 64, EpType::BulkInbound);
this.ep_enable(ep_num, USB_RECV, HS_MAX_PKT_SIZE as _, EpType::BulkOutbound);
this.ep_enable(ep_num, USB_SEND, HS_MAX_PKT_SIZE as _, EpType::BulkInbound);
}
pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize, length: usize) {
@ -135,7 +138,7 @@ pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize,
crate::println!("USB_DT_DEVICE");
let mut device_descriptor = DeviceDescriptor::default_mass_storage();
device_descriptor.b_max_packet_size0 = 64;
device_descriptor.b_cd_usb = 0x0210;
// device_descriptor.b_cd_usb = 0x0210;
let len = length.min(core::mem::size_of::<DeviceDescriptor>());
ep0_buf[..len].copy_from_slice(&device_descriptor.as_ref()[..len]);
@ -153,10 +156,6 @@ pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize,
this.ep0_send(this.ep0_buf.load(Ordering::SeqCst) as usize, len, 0);
}
USB_DT_OTHER_SPEED_CONFIG => {
crate::println!("USB_DT_OTHER_SPEED_CONFIG\r\n");
crate::println!("*** UNHANDLED ***");
}
USB_DT_CONFIG => {
crate::println!("USB_DT_CONFIG");
let total_length = size_of::<ConfigDescriptor>()
@ -164,8 +163,29 @@ pub fn get_descriptor_request(this: &mut CorigineUsb, value: u16, _index: usize,
+ size_of::<EndpointDescriptor>() * 2;
let config = ConfigDescriptor::default_mass_storage(total_length as u16);
let interface = InterfaceDescriptor::default_mass_storage();
let ep_in = EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_IN, 64);
let ep_out = EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_OUT, 64);
let ep_in =
EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_IN, HS_MAX_PKT_SIZE as _);
let ep_out =
EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_OUT, HS_MAX_PKT_SIZE as _);
let response: [&[u8]; 4] = [config.as_ref(), interface.as_ref(), ep_in.as_ref(), ep_out.as_ref()];
let flattened = response.iter().flat_map(|slice| slice.iter().copied());
for (dst, src) in ep0_buf.iter_mut().zip(flattened) {
*dst = src
}
let buffsize = total_length.min(length);
this.ep0_send(this.ep0_buf.load(Ordering::SeqCst) as usize, buffsize, 0);
}
USB_DT_OTHER_SPEED_CONFIG => {
crate::println!("USB_DT_OTHER_SPEED_CONFIG\r\n");
let total_length = size_of::<ConfigDescriptor>()
+ size_of::<InterfaceDescriptor>()
+ size_of::<EndpointDescriptor>() * 2;
let config = ConfigDescriptor::default_mass_storage(total_length as u16);
let interface = InterfaceDescriptor::default_mass_storage();
let ep_in =
EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_IN, FS_MAX_PKT_SIZE as _);
let ep_out =
EndpointDescriptor::default_mass_storage(MASS_STORAGE_EPADDR_OUT, FS_MAX_PKT_SIZE as _);
let response: [&[u8]; 4] = [config.as_ref(), interface.as_ref(), ep_in.as_ref(), ep_out.as_ref()];
let flattened = response.iter().flat_map(|slice| slice.iter().copied());
for (dst, src) in ep0_buf.iter_mut().zip(flattened) {