From 7819a7684710eb9a6f170d840d97d829a335bc82 Mon Sep 17 00:00:00 2001 From: "Cliff L. Biffle" Date: Thu, 10 Oct 2024 11:44:19 -0700 Subject: [PATCH] Move probe-rs chip name into boards/*.toml Historically, the build system's flash support has contained a big scary match mapping board names to probe-rs chip names. This is super error prone, as I ranted about at length over in #1886. I recently introduced per-board configuration files as a way to (1) check that board names used in apps are not mispeld, and (2) store configuration like this. This change switches the build system to look in the toml files for the chip name, removing the final big scary match statement. Note that this information is _more specific_ than the information in chip.toml, because probe-rs is weirdly fanatical about specifying chip type (up to and including the package!), whereas Hubris's peripheral maps don't care. Fixes #1886. --- boards/donglet-g030.toml | 2 + boards/donglet-g031.toml | 3 ++ boards/gemini-bu-1.toml | 2 + boards/gimlet-b.toml | 2 + boards/gimlet-c.toml | 2 + boards/gimlet-d.toml | 2 + boards/gimlet-e.toml | 2 + boards/gimlet-f.toml | 2 + boards/gimletlet-1.toml | 2 + boards/gimletlet-2.toml | 2 + boards/grapefruit.toml | 2 + boards/lpcxpresso55s69.toml | 2 + boards/medusa-a.toml | 2 + boards/nucleo-h743zi2.toml | 2 + boards/nucleo-h753zi.toml | 2 + boards/oxcon2023g0.toml | 2 + boards/oxide-rot-1-selfsigned.toml | 2 + boards/oxide-rot-1.toml | 2 + boards/psc-b.toml | 2 + boards/psc-c.toml | 2 + boards/rot-carrier-1.toml | 2 + boards/rot-carrier-2.toml | 2 + boards/sidecar-b.toml | 2 + boards/sidecar-c.toml | 2 + boards/sidecar-d.toml | 2 + boards/stm32f3-discovery.toml | 2 + boards/stm32f4-discovery.toml | 2 + boards/stm32g031-nucleo.toml | 2 + boards/stm32g070-nucleo.toml | 2 + build/xtask/src/config.rs | 14 ++++++ build/xtask/src/dist.rs | 3 +- build/xtask/src/flash.rs | 80 ++++++++++++------------------ build/xtask/src/main.rs | 12 ++++- 33 files changed, 117 insertions(+), 51 deletions(-) diff --git a/boards/donglet-g030.toml b/boards/donglet-g030.toml index e69de29bb..48f0277f8 100644 --- a/boards/donglet-g030.toml +++ b/boards/donglet-g030.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32G030F6Px" diff --git a/boards/donglet-g031.toml b/boards/donglet-g031.toml index e69de29bb..379a701cf 100644 --- a/boards/donglet-g031.toml +++ b/boards/donglet-g031.toml @@ -0,0 +1,3 @@ +[probe-rs] +chip-name = "STM32G031F8Px" + diff --git a/boards/gemini-bu-1.toml b/boards/gemini-bu-1.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gemini-bu-1.toml +++ b/boards/gemini-bu-1.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/gimlet-b.toml b/boards/gimlet-b.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gimlet-b.toml +++ b/boards/gimlet-b.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/gimlet-c.toml b/boards/gimlet-c.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gimlet-c.toml +++ b/boards/gimlet-c.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/gimlet-d.toml b/boards/gimlet-d.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gimlet-d.toml +++ b/boards/gimlet-d.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/gimlet-e.toml b/boards/gimlet-e.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gimlet-e.toml +++ b/boards/gimlet-e.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/gimlet-f.toml b/boards/gimlet-f.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gimlet-f.toml +++ b/boards/gimlet-f.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/gimletlet-1.toml b/boards/gimletlet-1.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gimletlet-1.toml +++ b/boards/gimletlet-1.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/gimletlet-2.toml b/boards/gimletlet-2.toml index e69de29bb..eb91eeac9 100644 --- a/boards/gimletlet-2.toml +++ b/boards/gimletlet-2.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/grapefruit.toml b/boards/grapefruit.toml index e69de29bb..eb91eeac9 100644 --- a/boards/grapefruit.toml +++ b/boards/grapefruit.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/lpcxpresso55s69.toml b/boards/lpcxpresso55s69.toml index e69de29bb..d1dd990b1 100644 --- a/boards/lpcxpresso55s69.toml +++ b/boards/lpcxpresso55s69.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "LPC55S69JBD100" diff --git a/boards/medusa-a.toml b/boards/medusa-a.toml index e69de29bb..eb91eeac9 100644 --- a/boards/medusa-a.toml +++ b/boards/medusa-a.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/nucleo-h743zi2.toml b/boards/nucleo-h743zi2.toml index e69de29bb..c84a30e99 100644 --- a/boards/nucleo-h743zi2.toml +++ b/boards/nucleo-h743zi2.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H743ZITx" diff --git a/boards/nucleo-h753zi.toml b/boards/nucleo-h753zi.toml index e69de29bb..eb91eeac9 100644 --- a/boards/nucleo-h753zi.toml +++ b/boards/nucleo-h753zi.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/oxcon2023g0.toml b/boards/oxcon2023g0.toml index e69de29bb..3b11d2a76 100644 --- a/boards/oxcon2023g0.toml +++ b/boards/oxcon2023g0.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32G030J6Mx" diff --git a/boards/oxide-rot-1-selfsigned.toml b/boards/oxide-rot-1-selfsigned.toml index e69de29bb..d1dd990b1 100644 --- a/boards/oxide-rot-1-selfsigned.toml +++ b/boards/oxide-rot-1-selfsigned.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "LPC55S69JBD100" diff --git a/boards/oxide-rot-1.toml b/boards/oxide-rot-1.toml index e69de29bb..d1dd990b1 100644 --- a/boards/oxide-rot-1.toml +++ b/boards/oxide-rot-1.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "LPC55S69JBD100" diff --git a/boards/psc-b.toml b/boards/psc-b.toml index e69de29bb..eb91eeac9 100644 --- a/boards/psc-b.toml +++ b/boards/psc-b.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/psc-c.toml b/boards/psc-c.toml index e69de29bb..eb91eeac9 100644 --- a/boards/psc-c.toml +++ b/boards/psc-c.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/rot-carrier-1.toml b/boards/rot-carrier-1.toml index e69de29bb..f10825885 100644 --- a/boards/rot-carrier-1.toml +++ b/boards/rot-carrier-1.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "LPC55S28JBD100" diff --git a/boards/rot-carrier-2.toml b/boards/rot-carrier-2.toml index e69de29bb..d1dd990b1 100644 --- a/boards/rot-carrier-2.toml +++ b/boards/rot-carrier-2.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "LPC55S69JBD100" diff --git a/boards/sidecar-b.toml b/boards/sidecar-b.toml index e69de29bb..eb91eeac9 100644 --- a/boards/sidecar-b.toml +++ b/boards/sidecar-b.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/sidecar-c.toml b/boards/sidecar-c.toml index e69de29bb..eb91eeac9 100644 --- a/boards/sidecar-c.toml +++ b/boards/sidecar-c.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/sidecar-d.toml b/boards/sidecar-d.toml index e69de29bb..eb91eeac9 100644 --- a/boards/sidecar-d.toml +++ b/boards/sidecar-d.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32H753ZITx" diff --git a/boards/stm32f3-discovery.toml b/boards/stm32f3-discovery.toml index e69de29bb..ec6b7f367 100644 --- a/boards/stm32f3-discovery.toml +++ b/boards/stm32f3-discovery.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32F303VCTx" diff --git a/boards/stm32f4-discovery.toml b/boards/stm32f4-discovery.toml index e69de29bb..2a37e2a01 100644 --- a/boards/stm32f4-discovery.toml +++ b/boards/stm32f4-discovery.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32F407VGTx" diff --git a/boards/stm32g031-nucleo.toml b/boards/stm32g031-nucleo.toml index e69de29bb..f5f89b903 100644 --- a/boards/stm32g031-nucleo.toml +++ b/boards/stm32g031-nucleo.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32G031Y8Yx" diff --git a/boards/stm32g070-nucleo.toml b/boards/stm32g070-nucleo.toml index e69de29bb..766bc22fc 100644 --- a/boards/stm32g070-nucleo.toml +++ b/boards/stm32g070-nucleo.toml @@ -0,0 +1,2 @@ +[probe-rs] +chip-name = "STM32G070KBTx" diff --git a/build/xtask/src/config.rs b/build/xtask/src/config.rs index 8366c84fe..351b515d5 100644 --- a/build/xtask/src/config.rs +++ b/build/xtask/src/config.rs @@ -800,3 +800,17 @@ fn read_and_flatten_toml( merge_toml_documents(&mut original, doc)?; Ok(original) } + +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub struct BoardConfig { + /// Info about how to interact with this board using probe-rs. + pub probe_rs: Option, +} + +#[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub struct ProbeRsBoardConfig { + /// The "chip name" used by probe-rs for flashing. + pub chip_name: String, +} diff --git a/build/xtask/src/dist.rs b/build/xtask/src/dist.rs index 59379356d..8b0ef6ca4 100644 --- a/build/xtask/src/dist.rs +++ b/build/xtask/src/dist.rs @@ -872,9 +872,8 @@ fn build_archive( // any external configuration files, serialize it, and add it to the // archive. // - if let Some(config) = - crate::flash::config(cfg.toml.board.as_str(), &chip_dir)? { + let config = crate::flash::config(&cfg.toml.board)?; archive.text( img_dir.join("flash.ron"), ron::ser::to_string_pretty( diff --git a/build/xtask/src/flash.rs b/build/xtask/src/flash.rs index 1678590c9..08ff5dc0e 100644 --- a/build/xtask/src/flash.rs +++ b/build/xtask/src/flash.rs @@ -2,62 +2,48 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. +use anyhow::Context as _; use serde::Serialize; use std::path::Path; +use crate::config::BoardConfig; + #[derive(Debug, Serialize, Default)] pub struct FlashConfig { /// The name used by probe-rs to identify the chip. chip: Option, } -impl FlashConfig { - // - // Set the chip - // - fn set_chip(&mut self, val: &str) -> &mut Self { - self.chip = Some(val.to_string()); - self - } +pub fn config(board: &str) -> anyhow::Result { + Ok(FlashConfig { + chip: chip_name(board)?, + }) } -pub fn config( - board: &str, - _chip_dir: &Path, -) -> anyhow::Result> { - let mut flash = FlashConfig::default(); - - flash.set_chip(chip_name(board)?); - - Ok(Some(flash)) -} - -pub fn chip_name(board: &str) -> anyhow::Result<&'static str> { - let b = match board { - "lpcxpresso55s69" - | "rot-carrier-2" - | "oxide-rot-1" - | "oxide-rot-1-selfsigned" => "LPC55S69JBD100", - "rot-carrier-1" => "LPC55S28JBD100", - "stm32f3-discovery" => "STM32F303VCTx", - "stm32f4-discovery" => "STM32F407VGTx", - "nucleo-h743zi2" => "STM32H743ZITx", - "nucleo-h753zi" => "STM32H753ZITx", - "gemini-bu-1" | "gimletlet-1" | "gimletlet-2" | "gimlet-b" - | "gimlet-c" | "gimlet-d" | "gimlet-e" | "gimlet-f" | "psc-a" - | "psc-b" | "psc-c" | "sidecar-b" | "sidecar-c" | "sidecar-d" - | "medusa-a" | "grapefruit" => "STM32H753ZITx", - "donglet-g030" => "STM32G030F6Px", - "donglet-g031" => "STM32G031F8Px", - "stm32g031-nucleo" => "STM32G031Y8Yx", - "oxcon2023g0" => "STM32G030J6Mx", - "stm32g070-nucleo" => "STM32G070KBTx", - "stm32g0b1-nucleo" => anyhow::bail!( - "This board is not yet supported by probe-rs, \ - please use OpenOCD directly" - ), - _ => anyhow::bail!("unrecognized board {}", board), - }; - - Ok(b) +pub fn chip_name(board: &str) -> anyhow::Result> { + let board_config_path = Path::new("boards").join(format!("{board}.toml")); + + let board_config_text = std::fs::read_to_string(&board_config_path) + .with_context(|| { + format!( + "can't access board config at: {}", + board_config_path.display() + ) + })?; + + let board_config: BoardConfig = toml::from_str(&board_config_text) + .with_context(|| { + format!( + "can't parse board config at: {}", + board_config_path.display() + ) + })?; + + if let Some(probe_rs) = &board_config.probe_rs { + Ok(Some(probe_rs.chip_name.clone())) + } else { + // tolerate the section missing for new chips, but we can't provide a + // chip name in this case. + Ok(None) + } } diff --git a/build/xtask/src/main.rs b/build/xtask/src/main.rs index c14fe51b9..b35f941c6 100644 --- a/build/xtask/src/main.rs +++ b/build/xtask/src/main.rs @@ -9,7 +9,7 @@ use std::path::PathBuf; -use anyhow::{bail, Context, Result}; +use anyhow::{anyhow, bail, Context, Result}; use clap::Parser; use crate::config::Config; @@ -273,7 +273,15 @@ fn run(xtask: Xtask) -> Result<()> { Xtask::Flash { dirty, mut args } => { dist::package(args.verbose, false, &args.cfg, None, dirty)?; let toml = Config::from_file(&args.cfg)?; - let chip = ["-c", crate::flash::chip_name(&toml.board)?]; + let chipname = + crate::flash::chip_name(&toml.board)?.ok_or_else(|| { + anyhow!( + "can't flash board: chip name missing \ + from boards/{}.toml", + toml.board, + ) + })?; + let chip = ["-c", &chipname]; args.extra_options.push("--force".to_string()); let image_name = if let Some(ref name) = args.image_name {