From b9a37ff54d117f108c0de8fe32557e62cc675ae4 Mon Sep 17 00:00:00 2001 From: Jakob Lechner Date: Thu, 3 Apr 2025 01:00:12 +0200 Subject: [PATCH 1/4] Remove serial number It doesn't work with windows. It seems that the USB device goes into low power state and then the serial number string descriptor cannot be read. --- .gitignore | 1 - src/main.rs | 4 +--- src/serial_number.rs.example | 1 - 3 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 src/serial_number.rs.example diff --git a/.gitignore b/.gitignore index 2105e7c..6abfe1b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /target /.direnv -/src/serial_number.rs diff --git a/src/main.rs b/src/main.rs index 9168152..8092a79 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ use usbd_storage::subclass::Command; use usbd_storage::transport::bbb::{BulkOnly, BulkOnlyError}; use usbd_storage::transport::TransportError; -mod serial_number; #[link_section = ".start_block"] #[used] @@ -126,8 +125,7 @@ fn main() -> ! { let mut usb_device = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0xcafe, 0x4002)) .strings(&[StringDescriptors::new(LangID::EN) .manufacturer("jalr") - .product("RAM Mass Storage") - .serial_number(serial_number::SERIAL)]) + .product("RAM Mass Storage")]) .unwrap() .device_class(0x08) .self_powered(false) diff --git a/src/serial_number.rs.example b/src/serial_number.rs.example deleted file mode 100644 index e61eae8..0000000 --- a/src/serial_number.rs.example +++ /dev/null @@ -1 +0,0 @@ -pub const SERIAL: &str = "example-4711"; From 8dd4913dbaa287b0a61c18b377dec7c5293e413c Mon Sep 17 00:00:00 2001 From: Jakob Lechner Date: Thu, 3 Apr 2025 01:01:49 +0200 Subject: [PATCH 2/4] Fix partition table --- src/main.rs | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8092a79..5230f7c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,10 +148,10 @@ 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 FILESYSTEM_LABEL: [u8; 11] = *b"RAM_USB "; - storage[0x1B8..0x1BC].copy_from_slice(&[0xEF, 0xBE, 0xAD, 0xDE]); - storage[0x1BE..0x1C3].copy_from_slice(&[0x00, 0x00, 0x02, 0x00, 0x01]); + storage[0x1B9] = 0x10; + storage[0x1BE..0x1C3].copy_from_slice(&[0x00, 0x00, 0x02, 0x00, 0x0c]); storage[0x1C3] = last_head as u8; storage[0x1C4] = ((last_cyl >> 8) & 0xc0) as u8 | ((last_sect as u8) & 0x3f); storage[0x1C5] = last_cyl as u8; @@ -165,22 +165,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, (DISK_BLOCK_NUM & 0xff) as u8, - (DISK_BLOCK_NUM >> 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, 0xF9, 0x98, - 0x3A, 0x58, 0x3A, 0x58, 0x00, 0x00, 0xF9, 0x98, 0x3A, 0x58 - ]); + 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, From eb4921afce00157960df0b52183188c04f281ae1 Mon Sep 17 00:00:00 2001 From: Jakob Lechner Date: Tue, 23 Sep 2025 20:19:22 +0200 Subject: [PATCH 3/4] Change LED Pin and flash timing --- src/main.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5230f7c..25bd191 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,7 +96,7 @@ fn main() -> ! { &mut pac.RESETS, ); - let mut led_pin = pins.gpio25.into_push_pull_output(); + let mut led_pin = pins.gpio12.into_push_pull_output(); let mut start_time: u64 = 0; let mut count: u64 = 0; @@ -201,10 +201,10 @@ fn main() -> ! { (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, + (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) => 25_000, + (UsbDeviceState::Suspend, true) => 10_000, (UsbDeviceState::Suspend, false) => 2_000_000, }; if timer.get_counter().ticks() - start_time > interval { From 43bec436eb5272594f61710e59717e772486f975 Mon Sep 17 00:00:00 2001 From: Jakob Lechner Date: Wed, 24 Sep 2025 10:46:11 +0200 Subject: [PATCH 4/4] Rework fast blinking after reset --- src/main.rs | 47 +++++++++++++++++++---------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/src/main.rs b/src/main.rs index 25bd191..91e7c2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -98,19 +98,6 @@ fn main() -> ! { let mut led_pin = pins.gpio12.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 usb_bus = UsbBusAllocator::new(hal::usb::UsbBus::new( pac.USB, pac.USB_DPRAM, @@ -131,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; @@ -192,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) => 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, + 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();