esp_timer, hal: add support for non-integer systimer frequency

When ESP32-C2 is paired with a 26 MHz XTAL, the systimer tick
frequency becomes equal to 26 / 2.5 = 10.4 MHz. Previously we always
assumed that systimer tick frequency is integer (and 1 MHz * power of
two, above that!).
This commit introduces a new LL macro, SYSTIMER_LL_TICKS_PER_US_DIV.
It should be set in such a way that:

1. SYSTIMER_LL_TICKS_PER_US / SYSTIMER_LL_TICKS_PER_US_DIV equals the
   actual systimer tick frequency,
2. and SYSTIMER_LL_TICKS_PER_US is integer.

For ESP32-C2 this means that SYSTIMER_LL_TICKS_PER_US = 52 and
SYSTIMER_LL_TICKS_PER_US_DIV = 5.

This introduced two possible issues:

1. Overflow when multiplying systimer counter by 5
   - Should not be an issue, since systimer counter is 52-bit, so
     counter * 5 is no more than 55-bit.
2. The code needs to perform:
   - divide by 5: when converting from microseconds to ticks
   - divide by 52: when converting from ticks to microseconds
   The latter potentially introduces a performance issue for the
   esp_timer_get_time function.
This commit is contained in:
Ivan Grokhotkov
2022-07-01 19:58:23 +02:00
committed by songruojing
parent f0f9890096
commit 5b54ae76d4
17 changed files with 102 additions and 113 deletions

View File

@@ -9,13 +9,20 @@
#include <stdbool.h>
#include "soc/systimer_struct.h"
#include "hal/assert.h"
#include "sdkconfig.h"
#define SYSTIMER_LL_COUNTER_CLOCK (0) // Counter used for "wallclock" time
#define SYSTIMER_LL_COUNTER_OS_TICK (1) // Counter used for OS tick
#define SYSTIMER_LL_ALARM_OS_TICK_CORE0 (0) // Alarm used for OS tick of CPU core 0
#define SYSTIMER_LL_ALARM_CLOCK (2) // Alarm used for "wallclock" time
#ifdef CONFIG_ESP32C2_XTAL_FREQ_26
#define SYSTIMER_LL_TICKS_PER_US (52) // (26 / 2.5) = 10.4 = 52/5 systimer ticks per us
#define SYSTIMER_LL_TICKS_PER_US_DIV (5)
#else
#define SYSTIMER_LL_TICKS_PER_US (16) // 16 systimer ticks == 1us
#define SYSTIMER_LL_TICKS_PER_US_DIV (1)
#endif // ESP32C2_XTAL_FREQ_*
#ifdef __cplusplus
extern "C" {