Moved the bootloader out of the pico_p1 project

This commit is contained in:
Dreaded_X 2023-09-15 19:14:38 +02:00
commit 43ceddb563
Signed by: Dreaded_X
GPG Key ID: FA5F485356B0D2D4
11 changed files with 1580 additions and 0 deletions

6
.cargo/config.toml Normal file
View File

@ -0,0 +1,6 @@
[build]
target = "thumbv6m-none-eabi"
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# This needs to be put in a seperate script because the log arguments do not get parsed properly
runner = ".cargo/runner.sh"

3
.cargo/runner.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
probe-run --chip RP2040 --log-format="{L} {s}
└─ [{t}] {m} @ {F}:{l}" $@

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target/

View File

@ -0,0 +1,6 @@
[unstable]
build-std = ["core"]
build-std-features = ["panic_immediate_abort"]
[build]
target = "thumbv6m-none-eabi"

1418
bootloader/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

37
bootloader/Cargo.toml Normal file
View File

@ -0,0 +1,37 @@
[package]
name = "bootloader"
version = "0.1.0"
edition = "2021"
[dependencies]
defmt = { version = "0.3", optional = true }
defmt-rtt = { version = "0.4", optional = true }
embassy-boot = { version = "0.1", features = ["nightly"] }
embassy-boot-rp = { version = "0.1", features = ["nightly"] }
embassy-rp = { version = "0.1", features = ["nightly"] }
embassy-sync = { version = "0.3", features = ["nightly"] }
cortex-m = { version = "0.7", features = [
"inline-asm",
"critical-section-single-core",
] }
cortex-m-rt = { version = "0.7" }
embedded-storage = "0.3.0"
[patch.crates-io]
# This is needed as embassy-rp is not on crates.io
embassy-boot = { git = "https://github.com/embassy-rs/embassy" }
embassy-boot-rp = { git = "https://github.com/embassy-rs/embassy" }
embassy-rp = { git = "https://github.com/embassy-rs/embassy" }
embassy-sync = { git = "https://github.com/embassy-rs/embassy" }
[features]
defmt = ["dep:defmt", "embassy-boot/defmt", "embassy-boot-rp/defmt", "embassy-rp/defmt", "embassy-sync/defmt"]
debug = ["defmt-rtt", "defmt"]
[profile.release]
debug = true
opt-level = 's'
codegen-units = 1
lto = true

2
bootloader/Embed.toml Normal file
View File

@ -0,0 +1,2 @@
[default.general]
chip = "rp2040"

36
bootloader/build.rs Normal file
View File

@ -0,0 +1,36 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
fn main() {
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
// By default cortex-m-rt expects memory.x, however this causes issues with workspaces as it
// will pick the first file that is found.
// In order to get around this we make a dummy memory.x file
File::create(out.join("memory.x")).unwrap();
// Use memory.x.in as a template for the actual memory.x
let memory = include_str!("memory.x.in")
.replace("{BOOTLOADER}", "FLASH")
.replace("{ACTIVE}", "ACTIVE");
// And then include it with a unique name
File::create(out.join("memory_boot.x"))
.unwrap()
.write_all(memory.as_bytes())
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=../memory.x.in");
// And link with that one
println!("cargo:rustc-link-arg-bins=-Tmemory_boot.x");
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");
println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");
if env::var("CARGO_FEATURE_DEFMT").is_ok() {
println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
}
}

22
bootloader/memory.x.in Normal file
View File

@ -0,0 +1,22 @@
MEMORY {
BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
{BOOTLOADER} : ORIGIN = 0x10000100, LENGTH = 24k - 0x100
BOOTLOADER_STATE : ORIGIN = 0x10006000, LENGTH = 4k
{ACTIVE} : ORIGIN = 0x10007000, LENGTH = 876k
DFU : ORIGIN = 0x100E2000, LENGTH = 880k
FW : ORIGIN = 0x101BE000, LENGTH = 256k
CLM : ORIGIN = 0x101FE000, LENGTH = 8k
RAM : ORIGIN = 0x20000000, LENGTH = 264K
}
__bootloader_state_start = ORIGIN(BOOTLOADER_STATE) - ORIGIN(BOOT2);
__bootloader_state_end = ORIGIN(BOOTLOADER_STATE) + LENGTH(BOOTLOADER_STATE) - ORIGIN(BOOT2);
__bootloader_active_start = ORIGIN({ACTIVE}) - ORIGIN(BOOT2);
__bootloader_active_end = ORIGIN({ACTIVE}) + LENGTH({ACTIVE}) - ORIGIN(BOOT2);
__bootloader_dfu_start = ORIGIN(DFU) - ORIGIN(BOOT2);
__bootloader_dfu_end = ORIGIN(DFU) + LENGTH(DFU) - ORIGIN(BOOT2);
__fw_start = ORIGIN(FW);
__clm_start = ORIGIN(CLM);

46
bootloader/src/main.rs Normal file
View File

@ -0,0 +1,46 @@
#![no_std]
#![no_main]
use core::cell::RefCell;
use cortex_m_rt::{entry, exception};
#[cfg(feature = "defmt")]
use defmt_rtt as _;
use embassy_boot_rp::*;
use embassy_rp::flash::{Blocking, Flash};
use embassy_sync::blocking_mutex::Mutex;
const FLASH_SIZE: usize = 2 * 1024 * 1024;
#[entry]
fn main() -> ! {
let p = embassy_rp::init(Default::default());
let flash = Flash::<_, Blocking, FLASH_SIZE>::new_blocking(p.FLASH);
let flash = Mutex::new(RefCell::new(flash));
let config = BootLoaderConfig::from_linkerfile_blocking(&flash);
let active_offset = config.active.offset();
let bl: BootLoader = BootLoader::prepare(config);
unsafe { bl.load(embassy_rp::flash::FLASH_BASE as u32 + active_offset) }
}
#[no_mangle]
#[cfg_attr(target_os = "none", link_section = ".HardFault.user")]
unsafe extern "C" fn HardFault() {
cortex_m::peripheral::SCB::sys_reset();
}
#[exception]
unsafe fn DefaultHandler(_: i16) -> ! {
const SCB_ICSR: *const u32 = 0xE000_ED04 as *const u32;
let irqn = core::ptr::read_volatile(SCB_ICSR) as u8 as i16 - 16;
panic!("DefaultHandler #{:?}", irqn);
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
cortex_m::asm::udf();
}

3
rust-toolchain.toml Normal file
View File

@ -0,0 +1,3 @@
[toolchain]
channel = "nightly"
targets = ["thumbv6m-none-eabi"]