diff --git a/src/main.rs b/src/main.rs index 73f38ba..91e7c2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,6 +16,7 @@ use usbd_storage::subclass::Command; use usbd_storage::transport::bbb::{BulkOnly, BulkOnlyError}; use usbd_storage::transport::TransportError; + #[link_section = ".start_block"] #[used] pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe(); @@ -95,20 +96,7 @@ fn main() -> ! { &mut pac.RESETS, ); - let mut led_pin = pins.gpio25.into_push_pull_output(); - - let mut start_time: u64 = 0; - let mut count: u64 = 0; - loop { - if timer.get_counter().ticks() - start_time > 25_000 { - count += 1; - let _ = led_pin.toggle(); - start_time = timer.get_counter().ticks(); - if count == 125 { - break; - } - } - } + let mut led_pin = pins.gpio12.into_push_pull_output(); let usb_bus = UsbBusAllocator::new(hal::usb::UsbBus::new( pac.USB, @@ -130,8 +118,6 @@ fn main() -> ! { .self_powered(false) .build(); - let mut start_time: u64 = 0; - let mut storage: [u8; (DISK_BLOCK_SIZE * DISK_BLOCK_NUM) as usize] = [0u8; (DISK_BLOCK_SIZE * DISK_BLOCK_NUM) as usize]; const LBA: u32 = DISK_BLOCK_NUM - 1; @@ -147,8 +133,7 @@ fn main() -> ! { const OEM: [u8; 8] = *b"mkfs.fat"; const FILESYSTEM_ID: [u8; 4] = [0xDE, 0xAD, 0xBE, 0xEF]; - const FILESYSTEM_NAME: [u8; 11] = *b"RAM_USB\0\0\0\0"; - const MEDIA_SECTORS: u32 = DISK_BLOCK_NUM - 256; + const FILESYSTEM_LABEL: [u8; 11] = *b"RAM_USB "; storage[0x1B9] = 0x10; storage[0x1BE..0x1C3].copy_from_slice(&[0x00, 0x00, 0x02, 0x00, 0x0c]); @@ -165,26 +150,23 @@ fn main() -> ! { // FAT12 Partition storage[0x200..0x203].copy_from_slice(&[0xEB, 0x3C, 0x90]); storage[0x203..0x20B].copy_from_slice(&OEM); - storage[0x20B..0x227].copy_from_slice(&[0x00, 0x02, 0x04, 0x01, 0x00, - 0x02, 0x00, 0x02, (MEDIA_SECTORS & 0xff) as u8, - (MEDIA_SECTORS >> 8) as u8, - 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29]); + storage[0x20B..0x21d].copy_from_slice(&[0x00, 0x02, 0x04, 0x01, 0x00, + 0x02, 0x00, 0x02, (LBA & 0xff) as u8, + (LBA >> 8) as u8, + 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01]); + storage[0x224..0x227].copy_from_slice(&[0x80, 0x00, 0x29]); storage[0x227..0x22B].copy_from_slice(&FILESYSTEM_ID); - storage[0x22B..0x236].copy_from_slice(&FILESYSTEM_NAME); + storage[0x22B..0x236].copy_from_slice(&FILESYSTEM_LABEL); storage[0x236..0x23E].copy_from_slice(b"FAT12 "); storage[0x3FE..0x400].copy_from_slice(&[0x55, 0xAA]); storage[0x400..0x403].copy_from_slice(&[0xF8, 0xFF, 0xFF]); storage[0x600..0x603].copy_from_slice(&[0xF8, 0xFF, 0xFF]); - storage[0x800..0x80B].copy_from_slice(&FILESYSTEM_NAME); - storage[0x80B..0x81A].copy_from_slice(&[0x08, 0x00, 0x00, 0x00, 0x00, - 0x21, 0x00, 0x21, 0x00, 0x00, 0x00, 0xFA, 0x01, 0x83, 0x5A - ]); - storage[0x5000..0x5010].copy_from_slice(&[ - 0x2E, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, - 0x20, 0x20, 0x20, 0x10, 0x00, 0x00, 0xA5, 0xBD - ]); + storage[0x800..0x80B].copy_from_slice(&FILESYSTEM_LABEL); + storage[0x80B] = 0x08; // file attribute 0x08 = volume label + + // https://de.wikipedia.org/wiki/File_Allocation_Table + // Remove bootloader code: dd if=/dev/zero conv=notrunc of=foo bs=1 count=420 seek=602 let mut state: State = State { storage_offset: 0, @@ -195,20 +177,26 @@ fn main() -> ! { defmt::info!("entering main loop"); + let mut start_time: u64 = 0; loop { - let interval = match (usb_device.state(), led_pin.is_set_high().unwrap_or_else(|_| false)) { - // The USB device has just been created or reset. - (UsbDeviceState::Default, true) => 100_000, - (UsbDeviceState::Default, false) => 100_000, - // The USB device has received an address from the host. - (UsbDeviceState::Addressed, true) => 250_000, - (UsbDeviceState::Addressed, false) => 250_000, - // The USB device has been configured and is fully functional. - (UsbDeviceState::Configured, true) => 1_000_000, - (UsbDeviceState::Configured, false) => 0, - // The USB device has been suspended by the host or it has been unplugged from the USB bus. - (UsbDeviceState::Suspend, true) => 25_000, - (UsbDeviceState::Suspend, false) => 2_000_000, + let interval = if timer.get_counter().ticks() < 2_000_000 { + 25_000 + } + else { + match (usb_device.state(), led_pin.is_set_high().unwrap_or_else(|_| false)) { + // The USB device has just been created or reset. + (UsbDeviceState::Default, true) => 100_000, + (UsbDeviceState::Default, false) => 100_000, + // The USB device has received an address from the host. + (UsbDeviceState::Addressed, true) => 250_000, + (UsbDeviceState::Addressed, false) => 250_000, + // The USB device has been configured and is fully functional. + (UsbDeviceState::Configured, true) => 8, + (UsbDeviceState::Configured, false) => 16, + // The USB device has been suspended by the host or it has been unplugged from the USB bus. + (UsbDeviceState::Suspend, true) => 10_000, + (UsbDeviceState::Suspend, false) => 2_000_000, + } }; if timer.get_counter().ticks() - start_time > interval { let _ = led_pin.toggle();