esp32: Use ESP-IDF abstraction for CPU cycle count (fix ESP32-C5).#19327
Merged
projectgus merged 1 commit intoJun 10, 2026
Merged
Conversation
- ESP32-C3 & C6 use a RISCV vendor custom performance counter for cycle counting. - ESP32-C5 uses the standard RISCV mcycle register. Trying to read the (non-existent) vendor register causes a fault. - ESP32-P4 is documented to also use the standard mcycle register and not support the custom register, but for some reason the tests don't fault or fail) on this CPU... Rather than adding more inline assembly, this commit delegates to ESP-IDF's abstraction layer which will inline the correct implementation here. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
dpgeorge
approved these changes
Jun 10, 2026
dpgeorge
left a comment
Member
There was a problem hiding this comment.
Thanks, this looks good to me. Definitely much better to use the IDF provided function, especially since it's fully inlined.
It seems the relevant SOC option for RISC-V CPUs is SOC_CPU_HAS_CSR_PC which is unset on C5 and P4.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the crash running
tests/extmod/time_res.pyandtests/extmod/time_ms_us.pyreported by @Josverl at #19303 (comment)Root cause:
ESP32-C3 & C6 use a RISCV vendor custom performance counter register for cycle counting.
ESP32-C5 uses the standard RISCV mcycle register. Trying to read the (non-existent) vendor register causes a fault.
ESP32-P4 is documented to also use the standard mcycle register and not support the custom register, but for some reason the tests don't fault or fail on this CPU...
Rather than adding more inline assembly, this PR delegates to ESP-IDF's abstraction layer which will inline the correct implementation here.
This work was funded through GitHub Sponsors.
Testing
Trade-offs and Alternatives
Generative AI
I did not use generative AI tools when creating this PR.