Skip to content

esp32: Use ESP-IDF abstraction for CPU cycle count (fix ESP32-C5).#19327

Merged
projectgus merged 1 commit into
micropython:masterfrom
projectgus:bugfix/esp32c5_cycle_count
Jun 10, 2026
Merged

esp32: Use ESP-IDF abstraction for CPU cycle count (fix ESP32-C5).#19327
projectgus merged 1 commit into
micropython:masterfrom
projectgus:bugfix/esp32c5_cycle_count

Conversation

@projectgus

@projectgus projectgus commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes the crash running tests/extmod/time_res.py and tests/extmod/time_ms_us.py reported 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

  • Ran the full test suite on my ESP32_GENERIC_C5 board (pass, including the time tests).
  • Re-ran just the two time tests on C3, C6 and P4 and S3.

Trade-offs and Alternatives

  • For boards apart from ESP32-C5 and possibly ESP32-P4 this should compile to exactly the same binary code, as the ESP-IDF function expands inline to the correct assembly code.
  • Generally better to use these kind of Espressif hardware abstractions where available (probably this abstraction wasn't available at the time the first implementations were written).

Generative AI

I did not use generative AI tools when creating this PR.

- 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 dpgeorge left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@projectgus projectgus merged commit 2f6f9e3 into micropython:master Jun 10, 2026
13 checks passed
@projectgus projectgus deleted the bugfix/esp32c5_cycle_count branch June 10, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants