Compare commits

...

4 commits

Author SHA1 Message Date
Jakob Lechner
43bec436eb Rework fast blinking after reset 2025-09-24 10:46:11 +02:00
Jakob Lechner
eb4921afce Change LED Pin and flash timing 2025-09-23 20:19:22 +02:00
Jakob Lechner
8dd4913dba Fix partition table 2025-04-11 02:47:38 +02:00
Jakob Lechner
b9a37ff54d 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.
2025-04-11 02:47:29 +02:00
3 changed files with 36 additions and 48 deletions

1
.gitignore vendored
View file

@ -1,3 +1,2 @@
/target
/.direnv
/src/serial_number.rs

View file

@ -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]
@ -97,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,
@ -126,15 +112,12 @@ 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)
.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;
@ -150,10 +133,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;
@ -167,22 +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, (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,
@ -193,8 +177,13 @@ 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)) {
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,
@ -202,11 +191,12 @@ 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 {
let _ = led_pin.toggle();

View file

@ -1 +0,0 @@
pub const SERIAL: &str = "example-4711";