Skip to content

Conversation

@jbeaurivage
Copy link
Contributor

@jbeaurivage jbeaurivage commented Jan 11, 2026

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.

ianrrees and others added 30 commits November 15, 2025 18:53
Manually replicated from Bradley Harden's work, rebased as
4b94fb0
This was the first "stash" in Bradley Harden's clock_v2_thumbv6m branch
The smaller chips use a simpler digital phase-locked oop, which doesn't
support the DCO filter options that the larger chips do.
This could use further work - the macro now takes two booleans to
indicate whether the type should be in the ApbTokens and ApbClocks
structs, however each type should be in exactly one of those so it would
be better for the macro to take a single boolean.
There's still some stuff to do, GclkIo etc.
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.
@bradleyharden
Copy link
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
Copy link
Contributor Author

jbeaurivage commented Jan 17, 2026

@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.

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