Skip to content

Dynamic PCLK source IDs#978

Draft
jbeaurivage wants to merge 76 commits into
atsamd-rs:feat/clock-v2from
jbeaurivage:dyn-pclk-src-id
Draft

Dynamic PCLK source IDs#978
jbeaurivage wants to merge 76 commits into
atsamd-rs:feat/clock-v2from
jbeaurivage:dyn-pclk-src-id

Conversation

@jbeaurivage

@jbeaurivage jbeaurivage commented Jan 11, 2026

Copy link
Copy Markdown
Contributor

Summary

After thinking about integrating the thumbv6/thumbv7 peripherals to the v2 clocking system, I'm realizing that every peripheral that requires a PCLK clock will need to take in a new type parameter representing the GCLK source that feeds the PCLK. This is somewhat problematic as it leads to a massive amount of generic bloat, and more importantly further incompatibility between the thumbv6 and thumbv7 peripherals.

This PR allows for two parallel APIs for specifying the PCLK source ID in Pclk structs: either type-checked, or dynamically checked at runtime.

This simplifies the peripherals' type signatures, as they no longer need to carry around the source clock's ID. For example, the Adc struct had a temporary workaround where it accepted a &Pclk reference to maintain compatibility with thumbv6's v1 clocking system. This helps solve these issues, as now each peripheral can opt into either API.

Drawbacks

However this means that when using the dynamic API:

  • Each peripheral instance now uses at least 1 byte of memory
  • We must assert at runtime that disabling a Pclk is decrementing the correct Gclk counter

Note

This PR sits on top of #892. Relevant commit: b1c04c9. I posted it here for visibility, but it should be merged here: ianrrees#13.

@bradleyharden

Copy link
Copy Markdown
Contributor

Yeah, I foresaw this a while ago and was concerned about it. That's why I wanted to implement a parallel, Dyn API for the clock system, like DynPin.

Rather than reduce the number of generics on Pclk, you might consider introducing a new, DynPclk and adding conversions to and from it for all instances of Pclk. Then you can have the peripherals take those instead. Actually, you could have them take anything that impl Into<DynPclk>.

@jbeaurivage jbeaurivage force-pushed the dyn-pclk-src-id branch 5 times, most recently from 18b5148 to 87b30c2 Compare January 17, 2026 23:36
@jbeaurivage

jbeaurivage commented Jan 17, 2026

Copy link
Copy Markdown
Contributor Author

@bradleyharden, I've implemented your suggestions, and I'm quite happy with it. Peripherals can pick whether they want to opt into the dynamic or type-checked PCLK API. I've changed the ADC peripheral to use the dynamic API, and it works nicely.

I guess a discussion topic will come up for each peripheral as we convert them to the v2 API, ie, whether it should be using the static or the dyn APIs, but at least we'll have the option now.

@jbeaurivage jbeaurivage force-pushed the dyn-pclk-src-id branch 2 times, most recently from 77557ad to 2b91312 Compare January 18, 2026 00:18
@jbeaurivage jbeaurivage changed the base branch from master to feat/clock-v2 June 2, 2026 12:25
ianrrees and others added 22 commits June 2, 2026 18:14
Couldn't deduplicate with the feather_m4 and metro_m4 examples, because
the alias used for the LED is different.  Sorting that out seems like a
separate project.
Reading the GCLK GENCTRL register isn't possible on the thumbv6m chips,
on these there is only one GENCTRL register for the whole GCLK
peripheral, in contrast to one GENCTRL per generator on the thumbv7em
chips.  On the thumbv6m chips, modifying GENCTRL is done by atomically
writing the whole register, which has a field for the generator ID to be
modified.

The GENCTRL register had been modified from the GclkToken<> impl,
per-field, but the state required to fill the other fields for the
atomic register write wasn't available in the GclkToken<> scope.  So,
these GENCTRL accesses were moved up in to the Gclk<> impl.

One more bit of state was added to the settings to handle the Output
Enable bit.
PCLK source ID can be either type-checked or
runtime-checked. Also change ADC peripehral to
use dynamic source IDs.

Signed-off-by: Justin Beaurivage <code@beaurivage.io>
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.

4 participants