|
|
@@ -19,13 +19,6 @@
|
|
|
|
#include "hal/systimer_types.h"
|
|
|
|
#include "hal/systimer_types.h"
|
|
|
|
#include "hal/clk_gate_ll.h"
|
|
|
|
#include "hal/clk_gate_ll.h"
|
|
|
|
|
|
|
|
|
|
|
|
// Number of timer ticks per microsecond
|
|
|
|
|
|
|
|
#if SOC_SYSTIMER_FIXED_TICKS_US
|
|
|
|
|
|
|
|
#define SYSTIMER_TICKS_PER_US (SOC_SYSTIMER_FIXED_TICKS_US)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define SYSTIMER_TICKS_PER_US (80)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void systimer_hal_init(systimer_hal_context_t *hal)
|
|
|
|
void systimer_hal_init(systimer_hal_context_t *hal)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
hal->dev = &SYSTIMER;
|
|
|
|
hal->dev = &SYSTIMER;
|
|
|
@@ -61,13 +54,13 @@ uint64_t systimer_hal_get_counter_value(systimer_hal_context_t *hal, uint32_t co
|
|
|
|
|
|
|
|
|
|
|
|
uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id)
|
|
|
|
uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
return systimer_hal_get_counter_value(hal, counter_id) / SYSTIMER_TICKS_PER_US;
|
|
|
|
return systimer_hal_get_counter_value(hal, counter_id) / SYSTIMER_LL_TICKS_PER_US;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if SOC_SYSTIMER_ALARM_MISS_COMPENSATE
|
|
|
|
#if SOC_SYSTIMER_ALARM_MISS_COMPENSATE
|
|
|
|
void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target)
|
|
|
|
void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
systimer_counter_value_t alarm = { .val = target * SYSTIMER_TICKS_PER_US};
|
|
|
|
systimer_counter_value_t alarm = { .val = target * SYSTIMER_LL_TICKS_PER_US};
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, false);
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, false);
|
|
|
|
systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
|
|
|
|
systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
|
|
|
|
systimer_ll_apply_alarm_value(hal->dev, alarm_id);
|
|
|
|
systimer_ll_apply_alarm_value(hal->dev, alarm_id);
|
|
|
@@ -76,9 +69,9 @@ void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_i
|
|
|
|
#else
|
|
|
|
#else
|
|
|
|
void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t timestamp)
|
|
|
|
void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t timestamp)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int64_t offset = SYSTIMER_TICKS_PER_US * 2;
|
|
|
|
int64_t offset = SYSTIMER_LL_TICKS_PER_US * 2;
|
|
|
|
uint64_t now_time = systimer_hal_get_counter_value(hal, 0);
|
|
|
|
uint64_t now_time = systimer_hal_get_counter_value(hal, 0);
|
|
|
|
systimer_counter_value_t alarm = { .val = MAX(timestamp * SYSTIMER_TICKS_PER_US, now_time + offset) };
|
|
|
|
systimer_counter_value_t alarm = { .val = MAX(timestamp * SYSTIMER_LL_TICKS_PER_US, now_time + offset) };
|
|
|
|
do {
|
|
|
|
do {
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, false);
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, false);
|
|
|
|
systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
|
|
|
|
systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val);
|
|
|
@@ -87,7 +80,7 @@ void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_i
|
|
|
|
int64_t delta = (int64_t)alarm.val - (int64_t)now_time;
|
|
|
|
int64_t delta = (int64_t)alarm.val - (int64_t)now_time;
|
|
|
|
if (delta <= 0 && !systimer_ll_is_alarm_int_fired(hal->dev, alarm_id)) {
|
|
|
|
if (delta <= 0 && !systimer_ll_is_alarm_int_fired(hal->dev, alarm_id)) {
|
|
|
|
// new alarm is less than the counter and the interrupt flag is not set
|
|
|
|
// new alarm is less than the counter and the interrupt flag is not set
|
|
|
|
offset += -1 * delta + SYSTIMER_TICKS_PER_US * 2;
|
|
|
|
offset += -1 * delta + SYSTIMER_LL_TICKS_PER_US * 2;
|
|
|
|
alarm.val = now_time + offset;
|
|
|
|
alarm.val = now_time + offset;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// finish if either (alarm > counter) or the interrupt flag is already set.
|
|
|
|
// finish if either (alarm > counter) or the interrupt flag is already set.
|
|
|
@@ -100,7 +93,7 @@ void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_i
|
|
|
|
void systimer_hal_set_alarm_period(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t period)
|
|
|
|
void systimer_hal_set_alarm_period(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t period)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, false);
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, false);
|
|
|
|
systimer_ll_set_alarm_period(hal->dev, alarm_id, period * SYSTIMER_TICKS_PER_US);
|
|
|
|
systimer_ll_set_alarm_period(hal->dev, alarm_id, period * SYSTIMER_LL_TICKS_PER_US);
|
|
|
|
systimer_ll_apply_alarm_value(hal->dev, alarm_id);
|
|
|
|
systimer_ll_apply_alarm_value(hal->dev, alarm_id);
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, true);
|
|
|
|
systimer_ll_enable_alarm(hal->dev, alarm_id, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -117,7 +110,7 @@ void systimer_hal_enable_alarm_int(systimer_hal_context_t *hal, uint32_t alarm_i
|
|
|
|
|
|
|
|
|
|
|
|
void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us)
|
|
|
|
void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(hal, counter_id) + time_us * SYSTIMER_TICKS_PER_US };
|
|
|
|
systimer_counter_value_t new_count = { .val = systimer_hal_get_counter_value(hal, counter_id) + time_us * SYSTIMER_LL_TICKS_PER_US };
|
|
|
|
systimer_ll_set_counter_value(hal->dev, counter_id, new_count.val);
|
|
|
|
systimer_ll_set_counter_value(hal->dev, counter_id, new_count.val);
|
|
|
|
systimer_ll_apply_counter_value(hal->dev, counter_id);
|
|
|
|
systimer_ll_apply_counter_value(hal->dev, counter_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
@@ -180,9 +173,9 @@ void systimer_hal_on_apb_freq_update(systimer_hal_context_t *hal, uint32_t apb_t
|
|
|
|
* If this was called when switching APB clock to XTAL, need to adjust
|
|
|
|
* If this was called when switching APB clock to XTAL, need to adjust
|
|
|
|
* XTAL_STEP value accordingly.
|
|
|
|
* XTAL_STEP value accordingly.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
if (apb_ticks_per_us != SYSTIMER_TICKS_PER_US) {
|
|
|
|
if (apb_ticks_per_us != SYSTIMER_LL_TICKS_PER_US) {
|
|
|
|
assert((SYSTIMER_TICKS_PER_US % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)");
|
|
|
|
assert((SYSTIMER_LL_TICKS_PER_US % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)");
|
|
|
|
systimer_ll_set_step_for_xtal(hal->dev, SYSTIMER_TICKS_PER_US / apb_ticks_per_us);
|
|
|
|
systimer_ll_set_step_for_xtal(hal->dev, SYSTIMER_LL_TICKS_PER_US / apb_ticks_per_us);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|