Skip to content

hil: time: move Counter::set_overflow_client to new CounterOverflow trait#4867

Merged
bradjc merged 7 commits into
tock:masterfrom
stanciugabriel:fix-counter-overflow-hil
Jun 12, 2026
Merged

hil: time: move Counter::set_overflow_client to new CounterOverflow trait#4867
bradjc merged 7 commits into
tock:masterfrom
stanciugabriel:fix-counter-overflow-hil

Conversation

@stanciugabriel

@stanciugabriel stanciugabriel commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Pull Request Overview

This pull request resolves issue #4866 by moving hardware timer overflow support into a separate extension trait called CounterOverflow.

Previously, the main Counter trait forced unsupported hardware to include dummy overflow functions that could fail silently, like we saw in the earlgrey chip. Instead of using a runtime Result to patch this, we are now using the type system to guarantee support at compile time.

The main Counter trait is now completely free of overflow logic. The nrf5x chip explicitly implements the new CounterOverflow trait to preserve its hardware support. Also, all the dummy overflow code has been completely deleted from unsupported chips like esp32, apollo3, sam4l, msp432, stm32, and earlgrey. Finally, the trd105 documentation has been updated to reflect this new design.

Testing Strategy

Verified multiple boards using make check(including hail, apollo3, esp32-c3, earlgrey-cw310, and other relevant boards).

TODO or Help Wanted

n/a

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

AI Use

  • The PR description details my use of AI in the production of the
    code in this PR, if any, and I have manually checked and
    personally certify the entire contents of this PR.
    No AI was used. AI was used for the documentation changes and commit message(wanted something more explicit because of the breaking changes).

@github-actions github-actions Bot added HIL This affects a Tock HIL interface. WG-OpenTitan In the purview of the OpenTitan working group. stm32 nrf sam4l labels Jun 8, 2026
@alexandruradovici

Copy link
Copy Markdown
Contributor

Thank you for submitting this, first of all please detail how and if AI was used. Before we can merge this, I think we need to have a discussion about how we want to proceed with the issue.

@alexandruradovici alexandruradovici added the blocked Waiting on something, like a different PR or a dependency. label Jun 8, 2026
@alexandruradovici alexandruradovici requested a review from bradjc June 8, 2026 16:34
@bradjc

bradjc commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Do we have any examples of where the overflow client is actually used?

One fix would just be to delete the API. Perhaps it was a good idea at the time, but, in practice it isn't easy to support and we shouldn't build capsules on an API that most boards can't support.

Another fix would be to add a new CounterOverflow trait that is only implemented by chips that can support it.

I think this option, making a runtime check out of something that should never have compiled successfully in the first place, is something we should try to avoid. I can't think of any reasonable recourse to getting this error at runtime.

@alexandruradovici

Copy link
Copy Markdown
Contributor

Do we have any examples of where the overflow client is actually used?

Based on this search https://github.com/search?q=repo%3Atock%2Ftock%20set_overflow_client&type=code, the earlgray and nrf5x chips seem to at least set a client.

Another fix would be to add a new CounterOverflow trait that is only implemented by chips that can support it.

Agree.

@stanciugabriel

Copy link
Copy Markdown
Contributor Author

It seems like the API is not dead, at least not for the nrf5x chip:

tock/chips/nrf5x/src/rtc.rs

Lines 105 to 118 in dbdd349

pub fn handle_interrupt(&self) {
if self.registers.events_ovrflw.is_set(Event::READY) {
self.registers.events_ovrflw.write(Event::READY::CLEAR);
self.overflow_client.map(|client| client.overflow());
}
if self.registers.events_compare[0].is_set(Event::READY) {
self.registers.intenclr.write(Inte::COMPARE0::SET);
self.registers.events_compare[0].write(Event::READY::CLEAR);
self.alarm_client.map(|client| {
client.alarm();
});
}
}
}

tock/chips/nrf5x/src/rtc.rs

Lines 130 to 133 in dbdd349

fn set_overflow_client(&self, client: &'a dyn time::OverflowClient) {
self.overflow_client.set(client);
self.registers.intenset.write(Inte::OVRFLW::SET);
}

It stores an OverflowClient, enables the overflow interrupt in set_overflow_client(), and calls overflow() from handle_interrupt(). The earlgrey chip is a partial implementation that accepts a client, returns success, and then never uses it.

I don’t think deleting the API is the right move if we want to keep nrf5x functionality. I can pivot this PR to split overflow support into a separate trait, implemented only by chips that can actually generate overflow notifications, so support is explicit at compile time.

Previously, `set_overflow_client` was part of the base `Counter`
trait, leading to dummy implementations or runtime `Result` checks
for unsupported hardware.

Based on maintainer feedback, this commit splits overflow
notification support into a separate extension trait, `CounterOverflow`.

This provides a compile-time guarantee for hardware support and
eliminates dead API surface from unsupported chips.

- Removed `set_overflow_client` from `Counter`.
- Added `CounterOverflow` trait to `hil::time`.
- Updated `nrf5x` to implement the new trait.
- Removed dummy overflow code from `earlgrey`, `sam4l`, `esp32`,
  `apollo3`, `msp432`, and `stm32` variants.
- Cleaned up unused `OverflowClient` imports across all boards.
- Updated TRD 105 to reflect the new architecture.
@stanciugabriel

Copy link
Copy Markdown
Contributor Author

I've pushed the updates based on the feedback! The base Counter trait is now clean, nrf5x implements the new CounterOverflow extension trait, and all the dummy overflow code has been removed from the unsupported chips. Let me know if its all good.

@alexandruradovici alexandruradovici removed the blocked Waiting on something, like a different PR or a dependency. label Jun 9, 2026
Comment thread doc/reference/trd105-time.md Outdated
@bradjc

bradjc commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

It makes sense we have something implement it, since we always try to have an example of any API we add. But if nothing is using overflow notifications, and something in the future could only use it on a single MCU family, I'm not sure what the value is of keeping it.

But, I do agree that what is implemented right now is a viable fix.

@bradjc bradjc changed the title hil: time: return Result from Counter::set_overflow_client hil: time: move Counter::set_overflow_client to new CounterOverflow trait Jun 10, 2026
@bradjc bradjc added the P-Significant This is a substancial change that requires review from all core developers. label Jun 10, 2026
@alevy alevy force-pushed the fix-counter-overflow-hil branch from 7b29002 to beb0118 Compare June 10, 2026 17:45

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

We decided making a deprecated new trait is the right fix.

I don't like the TRD process (because this will break the book and just commits us to more work to ratify the TRD) but that isn't this PR's issue.

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

We decided making a deprecated new trait is the right fix.

I don't like the TRD process (because this will break the book and just commits us to more work to ratify the TRD) but that isn't this PR's issue.

@alevy alevy added the last-call Final review period for a pull request. label Jun 11, 2026
@bradjc bradjc added this pull request to the merge queue Jun 12, 2026
Merged via the queue into tock:master with commit 236be95 Jun 12, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

HIL This affects a Tock HIL interface. last-call Final review period for a pull request. nrf P-Significant This is a substancial change that requires review from all core developers. sam4l stm32 WG-OpenTitan In the purview of the OpenTitan working group.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants