Skip to content

changed(pacs)!: Build PACs on demand and update PAC APIs - #1013

Merged
rnd-ash merged 7 commits into
atsamd-rs:masterfrom
jbeaurivage:pacs-on-demand
Sep 28, 2026
Merged

rnd-ash merged 7 commits into
atsamd-rs:masterfrom
jbeaurivage:pacs-on-demand

Conversation

@jbeaurivage

@jbeaurivage jbeaurivage commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The pre-generated PACs sitting in the repo have always been a pain point. This PR enables generating the PAC code at build-time using a build.rs scripts, which enormously reduces the number of files in the PAC crates. It also means we no longer need to run an update-pacs script whenever we update SVDs or svd2rust version, which is now a build-time dependency.

Currently the svd2rust dependency is at 0.37.1, which is the latest version.

I created a new helper library, atsamd-hal-pacs-builder, which contains the PAC generation code. It also uses svdtools, and much nicer YAML-based SVD patch templates rather than XSLT, which don't have good, pure-rust library offerings. The individual build.rs scripts just contain glue to call the codegen function. The PACs' lib.rs is now just this:

#![no_std]
#![allow(clippy::all)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(unsafe_op_in_unsafe_fn)]

include!(concat!(env!("OUT_DIR"), "/pac.rs"));

pub use __pac_impl::generic::*;
pub use __pac_impl::*;

I also updated the SVDs to the latest version provided by Microchip. As of right now, SAMD21 and SAMD51 devices are using official SVD releases from Microchip. The others use unofficial releases provided to us by Microchip while waiting for them to be officially released.

The new SVD+svd2rust combo brings in relatively significant API changes, so this will be a breaking change for PACs and the HAL.

Notes

  1. Deleting the generated PAC code from the repo obviously creates an absolutely monstrous git diff. I'll try to contain the file deletions and SVD updates to their own commits, so we can make a more palatable diff from the other commits in the PR for review.
  2. We are also waiting for svd-parser to make a new release. We need these changes to be released to crates.io for the new PACs to be accepted by svd2rust. Currently I'm patching the dependency to the git version in the workspace Cargo.toml, but I don't think that's allowed if we want to release atsamd-hal-pacs-builder to crates.io. svd-parser released!
  3. Removing the generated PAC code and updating SVDs obviously results in a HUGE diff. I tried to break the commits down in more digestible chunks. The interesting stuff (changes to code) can be found here.

Checklist

  • SAMD11 SVD v2.8.208, unofficial from Microchip
  • SAMD21 SVD v3.8.270, 2026-02-11
  • SAME51 SVD v3.9.276, 2026-06-11
  • SAME51 SVD v3.10.279, unofficial from Microchip
  • SAME53 SVD v3.11.259, unofficial from Microchip
  • SAME54 SVD v3.12.276, unofficial from Microchip
  • svd-parser release > 0.14.10

@jbeaurivage
jbeaurivage force-pushed the pacs-on-demand branch 3 times, most recently from 167d35a to 04d03bc Compare September 17, 2026 20:10
@jbeaurivage
jbeaurivage marked this pull request as ready for review September 17, 2026 20:14
@jbeaurivage
jbeaurivage force-pushed the pacs-on-demand branch 3 times, most recently from c9a0eea to fd2ec96 Compare September 17, 2026 21:01
Comment thread atsamd-hal-pac-builder/src/lib.rs Outdated

/// Get the chip name from the PAC package name
fn get_chip_name(pkg_name: impl AsRef<str>) -> Option<String> {
let re = regex::Regex::new(r"atsam[a-z]\d{2}[a-z]+").unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to allow for easier addition of future chips (PIC series), we should have another way of expressing this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, the chip names are now specified in the Cargo.toml package metadata instead of trying to parse them with regex.

Comment thread atsamd-hal-pac-builder/Cargo.toml Outdated

@rnd-ash rnd-ash left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall really happy with this, just a couple minor thoughts I've left, but nothing major.

Its also great to see all peripheral register writes are far more clearer, and are now correct with how the datasheets say to modify certain registers (For example interrupt registers)

The only outstanding question I have is what is with the feature flags being changed on all the PACs to include <PAC>/critical-section feature flag?

@jbeaurivage

Copy link
Copy Markdown
Contributor Author

Great! To answer your question, the newer versions of svd2rust rely on critical-section for the Peripherals::take API. If the HAL doesn't use that feature, Peripherals::take is not available, and we would be forced to only rely on Peripherals::steal. atsamd-hal already uses critical-section as a non-optional dependency, so this change is transparent to the user.

@jbeaurivage
jbeaurivage force-pushed the pacs-on-demand branch 3 times, most recently from ef7e9e1 to f9ab938 Compare September 18, 2026 15:36
Comment thread hal/src/dmac/async_api.rs
@rnd-ash
rnd-ash merged commit 5dce02b into atsamd-rs:master Sep 28, 2026
82 of 109 checks passed
@jbeaurivage
jbeaurivage deleted the pacs-on-demand branch September 28, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants