Skip to content

Commit

Permalink
Make VID:PID of Nordic configurable (#748)
Browse files Browse the repository at this point in the history
Also use all available flash for native applets.
  • Loading branch information
ia0 authored Feb 17, 2025
1 parent 4d0adf5 commit eda5840
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
11 changes: 5 additions & 6 deletions crates/runner-nordic/memory.x
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@ RAM:

Flash:
- 0x00000000 bootloader (64KiB)
- 0x00010000 platform A (320KiB)
- 0x00060000 platform B (320KiB)
- 0x000b0000 applet (256KiB)
- 0x00010000 platform A (320KiB + 128KiB)
- 0x00060000 platform B (320KiB + 128KiB)
- 0x000b0000 applet (256KiB - 256KiB)
- 0x000f0000 persistent storage (64KiB)
- 0x00100000

*/

/* Keep those values in sync with the header crate used by the bootloader. */
__bootloader_size = 0x00010000;
__platform_size = 0x00050000;
__applet_size = 0x00040000;
ASSERT(__bootloader_size + __platform_size == 0x00060000, "bad layout");
__platform_size = 0x00050000 + RUNNER_NATIVE * 0x00020000;
__applet_size = (1 - RUNNER_NATIVE) * 0x00040000;
ASSERT(__bootloader_size + 2 * __platform_size + __applet_size == 0x000f0000, "bad layout");
__header_origin = __bootloader_size + RUNNER_SIDE * __platform_size;
__header_length = 0x00000100;
Expand Down
30 changes: 29 additions & 1 deletion crates/runner-nordic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ fn main() -> ! {
let ctap = Ctap::new(HIDClass::new(usb_bus, CtapReport::desc(), 255));
#[cfg(feature = "usb-serial")]
let serial = Serial::new(SerialPort::new(usb_bus));
let usb_dev = UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x16c0, 0x27dd))
const VID_PID: UsbVidPid = vid_pid();
let usb_dev = UsbDeviceBuilder::new(usb_bus, VID_PID)
.strings(&[StringDescriptors::new(usb_device::LangID::EN).product("Wasefire")])
.unwrap()
.build();
Expand Down Expand Up @@ -326,3 +327,30 @@ fn usbd() {
state.serial.tick(polled, |event| state.events.push(event.into()));
});
}

const fn vid_pid() -> UsbVidPid {
let vidpid = match option_env!("RUNNER_VIDPID") {
None => b"16c0:27dd",
Some(x) => x.as_bytes(),
};
assert!(vidpid.len() == 9);
assert!(vidpid[4] == b':');
let vid = u16_from_hex(vidpid, 0);
let pid = u16_from_hex(vidpid, 5);
UsbVidPid(vid, pid)
}

const fn u16_from_hex(x: &[u8], mut i: usize) -> u16 {
let mut r = 0;
let n = i + 4;
while i < n {
r *= 16;
r += match x[i] {
b'0' ..= b'9' => (x[i] - b'0') as u16,
b'a' ..= b'f' => 10 + (x[i] - b'a') as u16,
_ => panic!(),
};
i += 1;
}
r
}
12 changes: 12 additions & 0 deletions crates/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,13 @@ struct RunnerOptions {
#[clap(long)]
serial: Option<String>,

/// Nordic platform VID:PID.
///
/// This is only used for the nordic runner. It must be 8 hexadecimal bytes with a colon in the
/// middle, e.g. `--vid-pid=1915:521f`.
#[clap(long)]
vid_pid: Option<String>,

/// Cargo no-default-features.
#[clap(long)]
no_default_features: bool,
Expand Down Expand Up @@ -471,7 +478,12 @@ impl RunnerOptions {
.await?;
}
if self.name == "nordic" {
let native = main.is_native() as u8;
rustflags.push(format!("-C link-arg=--defsym=RUNNER_NATIVE={native}"));
rustflags.push(format!("-C link-arg=--defsym=RUNNER_SIDE={step}"));
if let Some(vidpid) = &self.vid_pid {
cargo.env("RUNNER_VIDPID", vidpid);
}
let version = match &self.version {
Some(x) => x.parse()?,
None => 0,
Expand Down

0 comments on commit eda5840

Please sign in to comment.