systimer: refactor hal to accomodate more xtal choices

This commit is contained in:
morris
2022-07-21 19:05:21 +08:00
parent 0f08d4050d
commit d94432fea8
26 changed files with 200 additions and 156 deletions

View File

@@ -4,7 +4,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
#include "sys/param.h"
#include <sys/param.h>
#include "sdkconfig.h"
#include "esp_timer_impl.h"
#include "esp_err.h"
#include "esp_timer.h"
@@ -15,6 +16,8 @@
#include "soc/periph_defs.h"
#include "soc/soc_caps.h"
#include "esp_private/esp_clk.h"
#include "esp_private/systimer.h"
#include "esp_private/periph_ctrl.h"
#include "freertos/FreeRTOS.h"
#include "hal/systimer_ll.h"
#include "hal/systimer_types.h"
@@ -64,7 +67,9 @@ uint64_t IRAM_ATTR esp_timer_impl_get_counter_reg(void)
int64_t IRAM_ATTR esp_timer_impl_get_time(void)
{
return systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK) * SYSTIMER_LL_TICKS_PER_US_DIV / SYSTIMER_LL_TICKS_PER_US;
// we hope the execution time of this function won't > 1us
// thus, to save one function call, we didn't use the existing `systimer_hal_get_time`
return systimer_hal.ticks_to_us(systimer_hal_get_counter_value(&systimer_hal, SYSTIMER_LL_COUNTER_CLOCK));
}
int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time")));
@@ -96,7 +101,7 @@ static void IRAM_ATTR timer_alarm_isr(void *arg)
void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
{
#if !SOC_SYSTIMER_HAS_FIXED_TICKS_PER_US
#if !SOC_SYSTIMER_FIXED_DIVIDER
systimer_hal_on_apb_freq_update(&systimer_hal, apb_ticks_per_us);
#endif
}
@@ -105,7 +110,7 @@ void esp_timer_impl_set(uint64_t new_us)
{
portENTER_CRITICAL_SAFE(&s_time_update_lock);
systimer_counter_value_t new_count = {
.val = new_us * SYSTIMER_LL_TICKS_PER_US / SYSTIMER_LL_TICKS_PER_US_DIV
.val = systimer_hal.us_to_ticks(new_us),
};
systimer_ll_set_counter_value(systimer_hal.dev, SYSTIMER_LL_COUNTER_CLOCK, new_count.val);
systimer_ll_apply_counter_value(systimer_hal.dev, SYSTIMER_LL_COUNTER_CLOCK);
@@ -121,11 +126,17 @@ void esp_timer_impl_advance(int64_t time_diff_us)
esp_err_t esp_timer_impl_early_init(void)
{
periph_module_enable(PERIPH_SYSTIMER_MODULE);
systimer_hal_tick_rate_ops_t ops = {
.ticks_to_us = systimer_ticks_to_us,
.us_to_ticks = systimer_us_to_ticks,
};
systimer_hal_init(&systimer_hal);
systimer_hal_set_tick_rate_ops(&systimer_hal, &ops);
#if !SOC_SYSTIMER_HAS_FIXED_TICKS_PER_US
#if !SOC_SYSTIMER_FIXED_DIVIDER
assert(esp_clk_xtal_freq() == (40 * 1000000) &&
"update the step for xtal to support other XTAL:APB frequency ratios");
"update the step for xtal to support other XTAL:APB frequency ratios");
systimer_hal_set_steps_per_tick(&systimer_hal, 0, 2); // for xtal
systimer_hal_set_steps_per_tick(&systimer_hal, 1, 1); // for pll
#endif