Skip to content

Commit

Permalink
Move probe-rs chip name into boards/*.toml
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbiffle committed Oct 10, 2024
1 parent 721b6b6 commit e5afc05
Show file tree
Hide file tree
Showing 33 changed files with 117 additions and 51 deletions.
2 changes: 2 additions & 0 deletions boards/donglet-g030.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32G030F6Px"
3 changes: 3 additions & 0 deletions boards/donglet-g031.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[probe-rs]
chip-name = "STM32G031F8Px"

2 changes: 2 additions & 0 deletions boards/gemini-bu-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/gimlet-b.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/gimlet-c.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/gimlet-d.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/gimlet-e.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/gimlet-f.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/gimletlet-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/gimletlet-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/grapefruit.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/lpcxpresso55s69.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "LPC55S69JBD100"
2 changes: 2 additions & 0 deletions boards/medusa-a.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/nucleo-h743zi2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H743ZITx"
2 changes: 2 additions & 0 deletions boards/nucleo-h753zi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/oxcon2023g0.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32G030J6Mx"
2 changes: 2 additions & 0 deletions boards/oxide-rot-1-selfsigned.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "LPC55S69JBD100"
2 changes: 2 additions & 0 deletions boards/oxide-rot-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "LPC55S69JBD100"
2 changes: 2 additions & 0 deletions boards/psc-b.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/psc-c.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/rot-carrier-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "LPC55S28JBD100"
2 changes: 2 additions & 0 deletions boards/rot-carrier-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "LPC55S69JBD100"
2 changes: 2 additions & 0 deletions boards/sidecar-b.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/sidecar-c.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/sidecar-d.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32H753ZITx"
2 changes: 2 additions & 0 deletions boards/stm32f3-discovery.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32F303VCTx"
2 changes: 2 additions & 0 deletions boards/stm32f4-discovery.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32F407VGTx"
2 changes: 2 additions & 0 deletions boards/stm32g031-nucleo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32G031Y8Yx"
2 changes: 2 additions & 0 deletions boards/stm32g070-nucleo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[probe-rs]
chip-name = "STM32G070KBTx"
14 changes: 14 additions & 0 deletions build/xtask/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProbeRsBoardConfig>,
}

#[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,
}
3 changes: 1 addition & 2 deletions build/xtask/src/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
80 changes: 33 additions & 47 deletions build/xtask/src/flash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
}

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<FlashConfig> {
Ok(FlashConfig {
chip: chip_name(board)?,
})
}

pub fn config(
board: &str,
_chip_dir: &Path,
) -> anyhow::Result<Option<FlashConfig>> {
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<Option<String>> {
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)
}
}
12 changes: 10 additions & 2 deletions build/xtask/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e5afc05

Please sign in to comment.