From cd44e736db7f1b6f235a42435ad085df15bc4abc Mon Sep 17 00:00:00 2001 From: Jakob Lechner Date: Thu, 6 Mar 2025 23:41:58 +0100 Subject: [PATCH] Use RP2350 --- .cargo/config.toml | 19 ++--- Cargo.lock | 199 +++++++++++++++++++++++++-------------------- Cargo.toml | 29 ++----- Makefile | 12 --- build.rs | 13 +++ memory.x | 80 ++++++++++++++++-- shell.nix | 3 +- src/main.rs | 54 ++++++++---- 8 files changed, 252 insertions(+), 157 deletions(-) delete mode 100644 Makefile create mode 100644 build.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index 01c7fca..9b886b5 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,15 +1,12 @@ -[target.'cfg(all(target_arch = "arm", target_os = "none"))'] -runner = "probe-rs run --chip RP2040 --protocol swd" -# runner = "elf2uf2-rs -d" +[build] +target = "thumbv8m.main-none-eabihf" +[target.thumbv8m.main-none-eabihf] rustflags = [ - "-C", "linker=flip-link", - "-C", "link-arg=--nmagic", - "-C", "link-arg=-Tlink.x", - "-C", "link-arg=-Tdefmt.x", - "-Z", "trap-unreachable=no", - "-C", "no-vectorize-loops", + "-C", "link-arg=--nmagic", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", + "-C", "target-cpu=cortex-m33", ] -[build] -target = "thumbv6m-none-eabi" +runner = "picotool load -u -v -x -t elf" diff --git a/Cargo.lock b/Cargo.lock index ddcb754..56ed097 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,16 +70,7 @@ checksum = "e37549a379a9e0e6e576fd208ee60394ccb8be963889eebba3ffe0980364f472" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", -] - -[[package]] -name = "crc-any" -version = "2.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a62ec9ff5f7965e4d7280bd5482acd20aadb50d632cf6c1d74493856b011fa73" -dependencies = [ - "debug-helper", + "syn 2.0.100", ] [[package]] @@ -88,12 +79,6 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" -[[package]] -name = "debug-helper" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f578e8e2c440e7297e008bb5486a3a8a194775224bbc23729b0dbdfaeebf162e" - [[package]] name = "defmt" version = "0.3.10" @@ -114,7 +99,7 @@ dependencies = [ "proc-macro-error2", "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.100", ] [[package]] @@ -138,9 +123,9 @@ dependencies = [ [[package]] name = "either" -version = "1.13.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "embedded-dma" @@ -216,7 +201,7 @@ checksum = "e99b8b3c28ae0e84b604c75f721c21dc77afb3706076af5e8216d15fd1deaae3" dependencies = [ "frunk_proc_macro_helpers", "quote", - "syn 2.0.89", + "syn 2.0.100", ] [[package]] @@ -228,7 +213,7 @@ dependencies = [ "frunk_core", "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.100", ] [[package]] @@ -267,9 +252,9 @@ dependencies = [ [[package]] name = "itertools" -version = "0.10.5" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" dependencies = [ "either", ] @@ -326,9 +311,15 @@ checksum = "96667db765a921f7b295ffee8b60472b686a51d4f21c2ee4ffdb94c7013b65a6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.100", ] +[[package]] +name = "panic-halt" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11" + [[package]] name = "panic-probe" version = "0.3.2" @@ -336,7 +327,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4047d9235d1423d66cc97da7d07eddb54d4f154d6c13805c6d0793956f4f25b0" dependencies = [ "cortex-m", - "defmt", ] [[package]] @@ -358,9 +348,9 @@ dependencies = [ [[package]] name = "portable-atomic" -version = "1.10.0" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" [[package]] name = "proc-macro-error-attr2" @@ -381,23 +371,23 @@ dependencies = [ "proc-macro-error-attr2", "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.100", ] [[package]] name = "proc-macro2" -version = "1.0.92" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d3544b3f2748c54e147655edb5025752e2303145b5aefb3c3ea2c78b973bb0" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.37" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801" dependencies = [ "proc-macro2", ] @@ -409,36 +399,79 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" [[package]] -name = "rp-pico" -version = "0.9.0" +name = "riscv" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9342d3ac7011ac688300979e9b52a81f0add1d05feb02868cf94bfee0705b28" +checksum = "2f5c1b8bf41ea746266cdee443d1d1e9125c86ce1447e1a2615abd34330d33a9" dependencies = [ - "cortex-m-rt", - "fugit", - "rp2040-boot2", - "rp2040-hal", - "usb-device", + "critical-section", + "embedded-hal 1.0.0", ] [[package]] -name = "rp2040-boot2" +name = "riscv-rt" +version = "0.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0d35e32cf1383183e8885d8a9aa4402a087fd094dc34c2cb6df6687d0229dfe" +dependencies = [ + "riscv", + "riscv-rt-macros", +] + +[[package]] +name = "riscv-rt-macros" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30f19a85fe107b65031e0ba8ec60c34c2494069fe910d6c297f5e7cb5a6f76d0" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.100", +] + +[[package]] +name = "rp-binary-info" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534e2a451671116f5b9391cb15fae43b9abdc56817bcaca9a95ed32c3e4c6b38" + +[[package]] +name = "rp-hal-common" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8288358786b1458fb2caac8c4b40fb529ef4200d6c46467e2695b7a8ba573ae8" +dependencies = [ + "fugit", +] + +[[package]] +name = "rp2350-usb-ramdisk" +version = "0.1.0" +dependencies = [ + "cortex-m", + "cortex-m-rt", + "critical-section", + "defmt", + "defmt-rtt", + "embedded-hal 1.0.0", + "panic-halt", + "panic-probe", + "rp235x-hal", + "usb-device", + "usbd-storage", +] + +[[package]] +name = "rp235x-hal" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c92f344f63f950ee36cf4080050e4dce850839b9175da38f9d2ffb69b4dbb21" -dependencies = [ - "crc-any", -] - -[[package]] -name = "rp2040-hal" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d11e711940087f2cdff8aeae9f4b902e2014c06a00b39a1092686b81ec973d6f" +checksum = "e2939c82776b0b4ae110168b4298b5adf831e6cff249b057bf2a2187453b959c" dependencies = [ "bitfield 0.14.0", "cortex-m", + "cortex-m-rt", "critical-section", + "defmt", "embedded-dma", "embedded-hal 0.2.7", "embedded-hal 1.0.0", @@ -447,35 +480,40 @@ dependencies = [ "embedded-io", "frunk", "fugit", + "gcd", "itertools", "nb 1.1.0", "paste", "pio", "rand_core", - "rp2040-hal-macros", - "rp2040-pac", + "riscv", + "riscv-rt", + "rp-binary-info", + "rp-hal-common", + "rp235x-hal-macros", + "rp235x-pac", + "sha2-const-stable", "usb-device", "vcell", "void", ] [[package]] -name = "rp2040-hal-macros" +name = "rp235x-hal-macros" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86479063e497efe1ae81995ef9071f54fd1c7427e04d6c5b84cde545ff672a5e" +checksum = "74edd7a5979e9763bbb98e9746e711bac7464ee3397af7288e6c288ff0d3c764" dependencies = [ - "cortex-m-rt", "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.100", ] [[package]] -name = "rp2040-pac" -version = "0.6.0" +name = "rp235x-pac" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83cbcd3f7a0ca7bbe61dc4eb7e202842bee4e27b769a7bf3a4a72fa399d6e404" +checksum = "5ffcb6931deee4242886b5a1df62db5e2555b0eb6ae1e8be101f3ea3e58e65c6" dependencies = [ "cortex-m", "cortex-m-rt", @@ -483,23 +521,6 @@ dependencies = [ "vcell", ] -[[package]] -name = "rp2040-usb-ramdisk" -version = "0.1.0" -dependencies = [ - "cortex-m", - "cortex-m-rt", - "defmt", - "defmt-rtt", - "embedded-hal 1.0.0", - "panic-probe", - "rp-pico", - "rp2040-boot2", - "rp2040-hal", - "usb-device", - "usbd-storage", -] - [[package]] name = "rustc_version" version = "0.2.3" @@ -524,6 +545,12 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +[[package]] +name = "sha2-const-stable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f179d4e11094a893b82fff208f74d448a7512f99f5a0acbd5c679b705f83ed9" + [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -543,9 +570,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.89" +version = "2.0.100" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" +checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0" dependencies = [ "proc-macro2", "quote", @@ -554,29 +581,29 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.3" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "2.0.3" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" dependencies = [ "proc-macro2", "quote", - "syn 2.0.89", + "syn 2.0.100", ] [[package]] name = "unicode-ident" -version = "1.0.14" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "usb-device" diff --git a/Cargo.toml b/Cargo.toml index 0799c3b..9f4b78e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,30 +1,17 @@ [package] -name = "rp2040-usb-ramdisk" +name = "rp2350-usb-ramdisk" version = "0.1.0" -edition = "2024" +edition = "2021" [dependencies] cortex-m = "0.7.7" -cortex-m-rt = { version = "0.7.5", optional = true } +cortex-m-rt = "0.7.5" +critical-section = "1.2.0" defmt = "0.3.10" defmt-rtt = "0.4.1" embedded-hal = "1.0.0" -panic-probe = { version = "0.3.2", features = ["print-defmt"] } -rp-pico = "0.9.0" -rp2040-boot2 = { version = "0.3.0", optional = true } -rp2040-hal = "0.10.2" +panic-halt = "1.0.0" +panic-probe = "0.3.2" +rp235x-hal = { version = "0.3.0", features = ["binary-info", "critical-section-impl", "rt", "defmt"] } usb-device = "0.3.2" -usbd-storage = { version = "1.0.0", features = ["scsi", "bbb", "defmt"] } - -[features] -default = ["boot2", "rt", "critical-section-impl"] -boot2 = ["rp2040-boot2"] -rt = ["cortex-m-rt","rp2040-hal/rt"] -critical-section-impl = ["rp2040-hal/critical-section-impl"] -cortex-m-rt = ["dep:cortex-m-rt"] - -[profile.dev] -panic = "abort" - -[profile.release] -panic = "abort" +usbd-storage = { version = "1.0.0", features = ["bbb", "defmt", "scsi"] } diff --git a/Makefile b/Makefile deleted file mode 100644 index de09fbc..0000000 --- a/Makefile +++ /dev/null @@ -1,12 +0,0 @@ -BIN=rp2040-usb-ramdisk - -.PHONY: flash -flash: target/thumbv6m-none-eabi/release/$(BIN) - elf2uf2-rs --deploy $< - -target/thumbv6m-none-eabi/release/$(BIN): src/main.rs src/serial_number.rs Cargo.toml - cargo build --target thumbv6m-none-eabi --release - -target/thumbv6m-none-eabi/release/$(BIN).uf2: target/thumbv6m-none-eabi/release/$(BIN) - elf2uf2-rs $< $@ - diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..90bade9 --- /dev/null +++ b/build.rs @@ -0,0 +1,13 @@ +use std::{fs::File, io::Write, path::PathBuf}; + +fn main() { + let out = PathBuf::from(std::env::var("OUT_DIR").unwrap()); + println!("cargo:rustc-link-search={}", out.display()); + + let memory_x = include_bytes!("memory.x"); + let mut f = File::create(out.join("memory.x")).unwrap(); + f.write_all(memory_x).unwrap(); + println!("cargo:rerun-if-changed=memory.x"); + + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/memory.x b/memory.x index 070eac7..7d409ba 100644 --- a/memory.x +++ b/memory.x @@ -1,15 +1,77 @@ MEMORY { - BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100 - FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100 - RAM : ORIGIN = 0x20000000, LENGTH = 256K + /* + * The RP2350 has either external or internal flash. + * + * 2 MiB is a safe default here, although a Pico 2 has 4 MiB. + */ + FLASH : ORIGIN = 0x10000000, LENGTH = 2048K + /* + * RAM consists of 8 banks, SRAM0-SRAM7, with a striped mapping. + * This is usually good for performance, as it distributes load on + * those banks evenly. + */ + RAM : ORIGIN = 0x20000000, LENGTH = 512K + /* + * RAM banks 8 and 9 use a direct mapping. They can be used to have + * memory areas dedicated for some specific job, improving predictability + * of access times. + * Example: Separate stacks for core0 and core1. + */ + SRAM4 : ORIGIN = 0x20080000, LENGTH = 4K + SRAM5 : ORIGIN = 0x20081000, LENGTH = 4K } -EXTERN(BOOT2_FIRMWARE) +SECTIONS { + /* ### Boot ROM info + * + * Goes after .vector_table, to keep it in the first 4K of flash + * where the Boot ROM (and picotool) can find it + */ + .start_block : ALIGN(4) + { + __start_block_addr = .; + KEEP(*(.start_block)); + KEEP(*(.boot_info)); + } > FLASH + +} INSERT AFTER .vector_table; + +/* move .text to start /after/ the boot info */ +_stext = ADDR(.start_block) + SIZEOF(.start_block); SECTIONS { - /* ### Boot loader */ - .boot2 ORIGIN(BOOT2) : + /* ### Picotool 'Binary Info' Entries + * + * Picotool looks through this block (as we have pointers to it in our + * header) to find interesting information. + */ + .bi_entries : ALIGN(4) { - KEEP(*(.boot2)); - } > BOOT2 -} INSERT BEFORE .text; \ No newline at end of file + /* We put this in the header */ + __bi_entries_start = .; + /* Here are the entries */ + KEEP(*(.bi_entries)); + /* Keep this block a nice round size */ + . = ALIGN(4); + /* We put this in the header */ + __bi_entries_end = .; + } > FLASH +} INSERT AFTER .text; + +SECTIONS { + /* ### Boot ROM extra info + * + * Goes after everything in our program, so it can contain a signature. + */ + .end_block : ALIGN(4) + { + __end_block_addr = .; + KEEP(*(.end_block)); + } > FLASH + +} INSERT AFTER .uninit; + +PROVIDE(start_to_end = __end_block_addr - __start_block_addr); +PROVIDE(end_to_start = __start_block_addr - __end_block_addr); + + diff --git a/shell.nix b/shell.nix index d866bbc..882a501 100644 --- a/shell.nix +++ b/shell.nix @@ -1,10 +1,9 @@ { pkgs ? import {} }: pkgs.mkShell { nativeBuildInputs = with pkgs; [ - elf2uf2-rs + picotool gcc gnumake - probe-rs rustup ]; } diff --git a/src/main.rs b/src/main.rs index 42a1cb9..9168152 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,14 @@ #![no_std] #![no_main] -use rp_pico::entry; - use defmt as _; use defmt_rtt as _; use panic_probe as _; +use rp235x_hal as hal; + use embedded_hal::digital::StatefulOutputPin; -use rp_pico::hal::pac; - -use rp_pico::hal; - use usb_device::{class_prelude::*, prelude::*}; use usbd_storage::subclass::scsi::{Scsi, ScsiCommand}; @@ -22,8 +18,14 @@ use usbd_storage::transport::TransportError; mod serial_number; +#[link_section = ".start_block"] +#[used] +pub static IMAGE_DEF: hal::block::ImageDef = hal::block::ImageDef::secure_exe(); + +const XTAL_FREQ_HZ: u32 = 12_000_000u32; + const DISK_BLOCK_SIZE: u32 = 512; -const DISK_BLOCK_NUM: u32 = 256; +const DISK_BLOCK_NUM: u32 = 1000; const USB_PACKET_SIZE: u16 = 64; // 8,16,32,64 const MAX_LUN: u8 = 0; // max 0x0F @@ -68,14 +70,14 @@ impl State { } } -#[entry] +#[hal::entry] fn main() -> ! { - let mut pac = pac::Peripherals::take().unwrap(); + let mut pac = hal::pac::Peripherals::take().unwrap(); let mut watchdog = hal::Watchdog::new(pac.WATCHDOG); let clocks = hal::clocks::init_clocks_and_plls( - rp_pico::XOSC_CRYSTAL_FREQ, + XTAL_FREQ_HZ, pac.XOSC, pac.CLOCKS, pac.PLL_SYS, @@ -83,24 +85,36 @@ fn main() -> ! { &mut pac.RESETS, &mut watchdog, ) - .ok() .unwrap(); - let timer = hal::timer::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks); + let timer = hal::Timer::new_timer0(pac.TIMER0, &mut pac.RESETS, &clocks); let sio = hal::Sio::new(pac.SIO); - let pins = rp_pico::Pins::new( + let pins = hal::gpio::Pins::new( pac.IO_BANK0, pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS, ); - let mut led_pin = pins.led.into_push_pull_output(); + 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 usb_bus = UsbBusAllocator::new(hal::usb::UsbBus::new( - pac.USBCTRL_REGS, - pac.USBCTRL_DPRAM, + pac.USB, + pac.USB_DPRAM, clocks.usb_clock, true, &mut pac.RESETS, @@ -344,3 +358,11 @@ fn process_command( Ok(()) } + +#[link_section = ".bi_entries"] +#[used] +pub static PICOTOOL_ENTRIES: [hal::binary_info::EntryAddr; 3] = [ + hal::binary_info::rp_program_name!(c"rp2350-usb-ramdisk"), + hal::binary_info::rp_cargo_version!(), + hal::binary_info::rp_program_build_attribute!(), +];