Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

esp32c3 Unable to do any timing faster than 1ms #52598

Closed
mredp-evos opened this issue Nov 28, 2022 · 2 comments · Fixed by #53453
Closed

esp32c3 Unable to do any timing faster than 1ms #52598

mredp-evos opened this issue Nov 28, 2022 · 2 comments · Fixed by #53453
Assignees
Labels
area: Timer Timer bug The issue is a bug, or the PR is fixing a bug platform: ESP32 Espressif ESP32 priority: low Low impact/importance bug

Comments

@mredp-evos
Copy link

mredp-evos commented Nov 28, 2022

Trying to use any of the kernel timing functions for anything less than 1ms does not work on the ESP32-C3. I would have thought I just need to increase the CONFIG_SYS_CLOCK_TICKS_PER_SEC to 10000 to get 100us ticks. But this doesn't work.

I have tried various combinations of CONFIG_SYS_CLOCK_TICKS_PER_SEC and CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC. But it always ends up wrong/very innacurate.

To be specific, sometimes 100us delay would be closer to 250us. Or sometimes 100us delay would be 10ms. But I could never get 100us to equal 100us.

Is it possible to do any real-time scheduling in the tens or hundreds of microseconds? Perhaps, I am missing something but 1ms (1kHz) seems a bit slow to be the timing limit for a 160MHz processor.

Thanks,

@mredp-evos mredp-evos added the bug The issue is a bug, or the PR is fixing a bug label Nov 28, 2022
@henrikbrixandersen henrikbrixandersen added the platform: ESP32 Espressif ESP32 label Nov 28, 2022
@mredp-evos
Copy link
Author

mredp-evos commented Nov 29, 2022

Narrowed the issue down to esp32c3_sys_timer.c

A quick workaround was to set #define MIN_DELAY to 1. Then when you increase the CONFIG_SYS_CLOCK_TICKS_PER_SEC it won't be capped at 1ms.

I guess MIN_DELAY might be left over from the esp32s2 driver where the HW CYCLES are faster perhaps? Whereas in the esp32c3 HW CYCLE is only 1us which makes MIN_DELAY of 1000 equal 1ms.

I also set to CONFIG_TICKLESS_KERNEL=n but I never tested if this had any effect on the issue. I suspect that it did not.

This lead me to the other issue which is that the CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC is fixed at 1MHz when the clock is a 16MHz clock. This is because the esp32c3_sys_timer.c file uses systimer_hal functions which use SYSTIMER_TICKS_PER_US. And because the systimer_alarm function uses systimer_hal_get_time.

Instead, replace all the systimer_hal functions with systimer_ll functions and systimer_hal_get_time with systimer_hal_get_counter_value instead. Then the CYCLES will be based off 16MHz (62.5ns) instead of 1MHz (1us).

Fixing either the MIN_DELAY issue or the CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC issue will allow you to get faster timings.

@sylvioalves
Copy link
Collaborator

Trying to use any of the kernel timing functions for anything less than 1ms does not work on the ESP32-C3. I would have thought I just need to increase the CONFIG_SYS_CLOCK_TICKS_PER_SEC to 10000 to get 100us ticks. But this doesn't work.

I have tried various combinations of CONFIG_SYS_CLOCK_TICKS_PER_SEC and CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC. But it always ends up wrong/very innacurate.

To be specific, sometimes 100us delay would be closer to 250us. Or sometimes 100us delay would be 10ms. But I could never get 100us to equal 100us.

Is it possible to do any real-time scheduling in the tens or hundreds of microseconds? Perhaps, I am missing something but 1ms (1kHz) seems a bit slow to be the timing limit for a 160MHz processor.

Thanks,

Hi, can you describe which kernel timing functions you have been using? And how are you measuring the clock in this scenario?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Timer Timer bug The issue is a bug, or the PR is fixing a bug platform: ESP32 Espressif ESP32 priority: low Low impact/importance bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants