2021-09-27 00:46:51 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-11-05 23:00:07 -05:00
|
|
|
|
|
|
|
#include "test_utils.h"
|
2021-10-25 05:13:46 -04:00
|
|
|
#include "esp_private/periph_ctrl.h"
|
2020-11-05 23:00:07 -05:00
|
|
|
#include "soc/periph_defs.h"
|
|
|
|
#include "hal/timer_hal.h"
|
2021-09-27 00:46:51 -04:00
|
|
|
#include "hal/timer_ll.h"
|
2020-11-05 23:00:07 -05:00
|
|
|
|
|
|
|
#define TIMER_GROUP_ID (1)
|
|
|
|
#define TIMER_ID (0)
|
|
|
|
static timer_hal_context_t timer_hal;
|
|
|
|
|
|
|
|
void ref_clock_init(void)
|
|
|
|
{
|
|
|
|
periph_module_enable(PERIPH_TIMG1_MODULE);
|
|
|
|
timer_hal_init(&timer_hal, TIMER_GROUP_ID, TIMER_ID);
|
2021-09-27 00:46:51 -04:00
|
|
|
timer_ll_set_clock_source(timer_hal.dev, TIMER_ID, GPTIMER_CLK_SRC_XTAL); // Select XTAL, so ref_clock is independent of the APL clock
|
|
|
|
timer_ll_enable_counter(timer_hal.dev, TIMER_ID, false); // stop timer from running
|
|
|
|
timer_ll_set_clock_prescale(timer_hal.dev, TIMER_ID, 40); // Resolution is configured to 1MHz
|
|
|
|
timer_ll_set_count_direction(timer_hal.dev, timer_hal.timer_id, GPTIMER_COUNT_UP); // increase mode
|
2020-11-05 23:00:07 -05:00
|
|
|
timer_hal_set_counter_value(&timer_hal, 0); // initial count value to zero
|
2021-09-27 00:46:51 -04:00
|
|
|
timer_ll_enable_intr(timer_hal.dev, TIMER_LL_EVENT_ALARM(TIMER_ID), false); // disable interrupt
|
|
|
|
timer_ll_enable_alarm(timer_hal.dev, TIMER_ID, false); // alarm event is not needed
|
|
|
|
timer_ll_enable_auto_reload(timer_hal.dev, TIMER_ID, false);
|
|
|
|
timer_ll_enable_counter(timer_hal.dev, TIMER_ID, true); // start counter
|
2020-11-05 23:00:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void ref_clock_deinit(void)
|
|
|
|
{
|
2021-09-27 00:46:51 -04:00
|
|
|
timer_ll_enable_counter(timer_hal.dev, TIMER_ID, false); // stop timer from running
|
2020-11-05 23:00:07 -05:00
|
|
|
periph_module_disable(PERIPH_TIMG1_MODULE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t ref_clock_get(void)
|
|
|
|
{
|
2021-09-27 00:46:51 -04:00
|
|
|
return timer_ll_get_counter_value(timer_hal.dev, timer_hal.timer_id);
|
2020-11-05 23:00:07 -05:00
|
|
|
}
|