Skip to content

Commit a5633bf

Browse files
committed
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.
1 parent ac9379f commit a5633bf

33 files changed

+112
-49
lines changed

boards/donglet-g030.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32G030F6Px"

boards/donglet-g031.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[probe-rs]
2+
chip-name = "STM32G031F8Px"
3+

boards/gemini-bu-1.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/gimlet-b.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/gimlet-c.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/gimlet-d.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/gimlet-e.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/gimlet-f.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/gimletlet-1.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/gimletlet-2.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/grapefruit.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/lpcxpresso55s69.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "LPC55S69JBD100"

boards/medusa-a.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/nucleo-h743zi2.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H743ZITx"

boards/nucleo-h753zi.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/oxcon2023g0.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32G030J6Mx"

boards/oxide-rot-1-selfsigned.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "LPC55S69JBD100"

boards/oxide-rot-1.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "LPC55S69JBD100"

boards/psc-b.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/psc-c.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/rot-carrier-1.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "LPC55S28JBD100"

boards/rot-carrier-2.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "LPC55S69JBD100"

boards/sidecar-b.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/sidecar-c.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/sidecar-d.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32H753ZITx"

boards/stm32f3-discovery.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32F303VCTx"

boards/stm32f4-discovery.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32F407VGTx"

boards/stm32g031-nucleo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32G031Y8Yx"

boards/stm32g070-nucleo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[probe-rs]
2+
chip-name = "STM32G070KBTx"

build/xtask/src/config.rs

+14
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,17 @@ fn read_and_flatten_toml(
800800
merge_toml_documents(&mut original, doc)?;
801801
Ok(original)
802802
}
803+
804+
#[derive(Clone, Debug, Deserialize)]
805+
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
806+
pub struct BoardConfig {
807+
/// Info about how to interact with this board using probe-rs.
808+
pub probe_rs: Option<ProbeRsBoardConfig>,
809+
}
810+
811+
#[derive(Clone, Debug, Deserialize)]
812+
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
813+
pub struct ProbeRsBoardConfig {
814+
/// The "chip name" used by probe-rs for flashing.
815+
pub chip_name: String,
816+
}

build/xtask/src/dist.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -872,9 +872,8 @@ fn build_archive(
872872
// any external configuration files, serialize it, and add it to the
873873
// archive.
874874
//
875-
if let Some(config) =
876-
crate::flash::config(cfg.toml.board.as_str(), &chip_dir)?
877875
{
876+
let config = crate::flash::config(&cfg.toml.board)?;
878877
archive.text(
879878
img_dir.join("flash.ron"),
880879
ron::ser::to_string_pretty(

build/xtask/src/flash.rs

+28-45
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,45 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
44

5+
use anyhow::Context as _;
56
use serde::Serialize;
67
use std::path::Path;
78

9+
use crate::config::BoardConfig;
10+
811
#[derive(Debug, Serialize, Default)]
912
pub struct FlashConfig {
1013
/// The name used by probe-rs to identify the chip.
1114
chip: Option<String>,
1215
}
1316

14-
impl FlashConfig {
15-
//
16-
// Set the chip
17-
//
18-
fn set_chip(&mut self, val: &str) -> &mut Self {
19-
self.chip = Some(val.to_string());
20-
self
21-
}
22-
}
23-
2417
pub fn config(
2518
board: &str,
26-
_chip_dir: &Path,
27-
) -> anyhow::Result<Option<FlashConfig>> {
28-
let mut flash = FlashConfig::default();
29-
30-
flash.set_chip(chip_name(board)?);
31-
32-
Ok(Some(flash))
19+
) -> anyhow::Result<FlashConfig> {
20+
Ok(FlashConfig {
21+
chip: chip_name(board)?,
22+
})
3323
}
3424

35-
pub fn chip_name(board: &str) -> anyhow::Result<&'static str> {
36-
let b = match board {
37-
"lpcxpresso55s69"
38-
| "rot-carrier-2"
39-
| "oxide-rot-1"
40-
| "oxide-rot-1-selfsigned" => "LPC55S69JBD100",
41-
"rot-carrier-1" => "LPC55S28JBD100",
42-
"stm32f3-discovery" => "STM32F303VCTx",
43-
"stm32f4-discovery" => "STM32F407VGTx",
44-
"nucleo-h743zi2" => "STM32H743ZITx",
45-
"nucleo-h753zi" => "STM32H753ZITx",
46-
"gemini-bu-1" | "gimletlet-1" | "gimletlet-2" | "gimlet-b"
47-
| "gimlet-c" | "gimlet-d" | "gimlet-e" | "gimlet-f" | "psc-a"
48-
| "psc-b" | "psc-c" | "sidecar-b" | "sidecar-c" | "sidecar-d"
49-
| "medusa-a" | "grapefruit" => "STM32H753ZITx",
50-
"donglet-g030" => "STM32G030F6Px",
51-
"donglet-g031" => "STM32G031F8Px",
52-
"stm32g031-nucleo" => "STM32G031Y8Yx",
53-
"oxcon2023g0" => "STM32G030J6Mx",
54-
"stm32g070-nucleo" => "STM32G070KBTx",
55-
"stm32g0b1-nucleo" => anyhow::bail!(
56-
"This board is not yet supported by probe-rs, \
57-
please use OpenOCD directly"
58-
),
59-
_ => anyhow::bail!("unrecognized board {}", board),
60-
};
61-
62-
Ok(b)
25+
pub fn chip_name(board: &str) -> anyhow::Result<Option<String>> {
26+
let board_config_path = Path::new("boards")
27+
.join(format!("{board}.toml"));
28+
29+
let board_config_text = std::fs::read_to_string(&board_config_path)
30+
.with_context(|| {
31+
format!("can't access board config at: {}", board_config_path.display())
32+
})?;
33+
34+
let board_config: BoardConfig = toml::from_str(&board_config_text)
35+
.with_context(|| {
36+
format!("can't parse board config at: {}", board_config_path.display())
37+
})?;
38+
39+
if let Some(probe_rs) = &board_config.probe_rs {
40+
Ok(Some(probe_rs.chip_name.clone()))
41+
} else {
42+
// tolerate the section missing for new chips, but we can't provide a
43+
// chip name in this case.
44+
Ok(None)
45+
}
6346
}

build/xtask/src/main.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
use std::path::PathBuf;
1111

12-
use anyhow::{bail, Context, Result};
12+
use anyhow::{anyhow, bail, Context, Result};
1313
use clap::Parser;
1414

1515
use crate::config::Config;
@@ -273,7 +273,15 @@ fn run(xtask: Xtask) -> Result<()> {
273273
Xtask::Flash { dirty, mut args } => {
274274
dist::package(args.verbose, false, &args.cfg, None, dirty)?;
275275
let toml = Config::from_file(&args.cfg)?;
276-
let chip = ["-c", crate::flash::chip_name(&toml.board)?];
276+
let chipname =
277+
crate::flash::chip_name(&toml.board)?.ok_or_else(|| {
278+
anyhow!(
279+
"can't flash board: chip name missing \
280+
from boards/{}.toml",
281+
toml.board,
282+
)
283+
})?;
284+
let chip = ["-c", &chipname];
277285
args.extra_options.push("--force".to_string());
278286

279287
let image_name = if let Some(ref name) = args.image_name {

0 commit comments

Comments
 (0)