| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-09-13 19:13:38 +08:00
										 |  |  |  * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD | 
					
						
							| 
									
										
										
										
											2021-11-06 17:24:45 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  | #include <stddef.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | #include <sys/param.h>
 | 
					
						
							|  |  |  | #include "soc/soc_caps.h"
 | 
					
						
							|  |  |  | #include "hal/systimer_hal.h"
 | 
					
						
							|  |  |  | #include "hal/systimer_ll.h"
 | 
					
						
							|  |  |  | #include "hal/systimer_types.h"
 | 
					
						
							| 
									
										
										
										
											2021-05-19 10:53:21 +08:00
										 |  |  | #include "hal/assert.h"
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-13 19:13:38 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | void systimer_hal_init(systimer_hal_context_t *hal) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     hal->dev = &SYSTIMER; | 
					
						
							|  |  |  |     systimer_ll_enable_clock(hal->dev, true); | 
					
						
							| 
									
										
										
										
											2022-08-29 17:59:11 +08:00
										 |  |  | #if SOC_SYSTIMER_SUPPORT_ETM
 | 
					
						
							|  |  |  |     systimer_ll_enable_etm(&SYSTIMER, true); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  | void systimer_hal_deinit(systimer_hal_context_t *hal) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-08-29 17:59:11 +08:00
										 |  |  | #if SOC_SYSTIMER_SUPPORT_ETM
 | 
					
						
							|  |  |  |     systimer_ll_enable_etm(&SYSTIMER, false); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |     systimer_ll_enable_clock(hal->dev, false); | 
					
						
							|  |  |  |     hal->dev = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 21:29:16 +08:00
										 |  |  | void systimer_hal_set_clock_source(systimer_hal_context_t *hal, systimer_clock_source_t clk_src) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void)hal; | 
					
						
							|  |  |  |     systimer_ll_set_clock_source(clk_src); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | systimer_clock_source_t systimer_hal_get_clock_source(systimer_hal_context_t *hal) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void)hal; | 
					
						
							|  |  |  |     return systimer_ll_get_clock_source(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  | void systimer_hal_set_tick_rate_ops(systimer_hal_context_t *hal, systimer_hal_tick_rate_ops_t *ops) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     hal->ticks_to_us = ops->ticks_to_us; | 
					
						
							|  |  |  |     hal->us_to_ticks = ops->us_to_ticks; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | uint64_t systimer_hal_get_counter_value(systimer_hal_context_t *hal, uint32_t counter_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     uint32_t lo, lo_start, hi; | 
					
						
							|  |  |  |     /* Set the "update" bit and wait for acknowledgment */ | 
					
						
							|  |  |  |     systimer_ll_counter_snapshot(hal->dev, counter_id); | 
					
						
							|  |  |  |     while (!systimer_ll_is_counter_value_valid(hal->dev, counter_id)); | 
					
						
							|  |  |  |     /* Read LO, HI, then LO again, check that LO returns the same value.
 | 
					
						
							|  |  |  |      * This accounts for the case when an interrupt may happen between reading | 
					
						
							|  |  |  |      * HI and LO values, and this function may get called from the ISR. | 
					
						
							|  |  |  |      * In this case, the repeated read will return consistent values. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     lo_start = systimer_ll_get_counter_value_low(hal->dev, counter_id); | 
					
						
							|  |  |  |     do { | 
					
						
							|  |  |  |         lo = lo_start; | 
					
						
							|  |  |  |         hi = systimer_ll_get_counter_value_high(hal->dev, counter_id); | 
					
						
							|  |  |  |         lo_start = systimer_ll_get_counter_value_low(hal->dev, counter_id); | 
					
						
							|  |  |  |     } while (lo_start != lo); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     systimer_counter_value_t result = { | 
					
						
							|  |  |  |         .lo = lo, | 
					
						
							|  |  |  |         .hi = hi | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return result.val; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint64_t systimer_hal_get_time(systimer_hal_context_t *hal, uint32_t counter_id) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |     return hal->ticks_to_us(systimer_hal_get_counter_value(hal, counter_id)); | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if SOC_SYSTIMER_ALARM_MISS_COMPENSATE
 | 
					
						
							|  |  |  | void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t target) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-01 19:58:23 +02:00
										 |  |  |     systimer_counter_value_t alarm = { | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |         .val = hal->us_to_ticks(target), | 
					
						
							| 
									
										
										
										
											2022-07-01 19:58:23 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  |     systimer_ll_enable_alarm(hal->dev, alarm_id, false); | 
					
						
							|  |  |  |     systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val); | 
					
						
							|  |  |  |     systimer_ll_apply_alarm_value(hal->dev, alarm_id); | 
					
						
							|  |  |  |     systimer_ll_enable_alarm(hal->dev, alarm_id, true); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-01 19:58:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #else // SOC_SYSTIMER_ALARM_MISS_COMPENSATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | void systimer_hal_set_alarm_target(systimer_hal_context_t *hal, uint32_t alarm_id, uint64_t timestamp) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |     int64_t offset = hal->us_to_ticks(1) * 2; | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  |     uint64_t now_time = systimer_hal_get_counter_value(hal, 0); | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |     systimer_counter_value_t alarm = { .val = MAX(hal->us_to_ticks(timestamp), now_time + offset) }; | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  |     do { | 
					
						
							|  |  |  |         systimer_ll_enable_alarm(hal->dev, alarm_id, false); | 
					
						
							|  |  |  |         systimer_ll_set_alarm_target(hal->dev, alarm_id, alarm.val); | 
					
						
							|  |  |  |         systimer_ll_enable_alarm(hal->dev, alarm_id, true); | 
					
						
							|  |  |  |         now_time = systimer_hal_get_counter_value(hal, 0); | 
					
						
							|  |  |  |         int64_t delta = (int64_t)alarm.val - (int64_t)now_time; | 
					
						
							|  |  |  |         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
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |             offset += -1 * delta + hal->us_to_ticks(1) * 2; | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  |             alarm.val = now_time + offset; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             // finish if either (alarm > counter) or the interrupt flag is already set.
 | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } while (1); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-01 19:58:23 +02:00
										 |  |  | #endif // SOC_SYSTIMER_ALARM_MISS_COMPENSATE
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 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); | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |     systimer_ll_set_alarm_period(hal->dev, alarm_id, hal->us_to_ticks(period)); | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  |     systimer_ll_apply_alarm_value(hal->dev, alarm_id); | 
					
						
							|  |  |  |     systimer_ll_enable_alarm(hal->dev, alarm_id, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | uint64_t systimer_hal_get_alarm_value(systimer_hal_context_t *hal, uint32_t alarm_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return systimer_ll_get_alarm_target(hal->dev, alarm_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void systimer_hal_enable_alarm_int(systimer_hal_context_t *hal, uint32_t alarm_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     systimer_ll_enable_alarm_int(hal->dev, alarm_id, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void systimer_hal_counter_value_advance(systimer_hal_context_t *hal, uint32_t counter_id, int64_t time_us) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-01 19:58:23 +02:00
										 |  |  |     systimer_counter_value_t new_count = { | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |         .val = systimer_hal_get_counter_value(hal, counter_id) + hal->us_to_ticks(time_us), | 
					
						
							| 
									
										
										
										
											2022-07-01 19:58:23 +02:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  |     systimer_ll_set_counter_value(hal->dev, counter_id, new_count.val); | 
					
						
							|  |  |  |     systimer_ll_apply_counter_value(hal->dev, counter_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void systimer_hal_enable_counter(systimer_hal_context_t *hal, uint32_t counter_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     systimer_ll_enable_counter(hal->dev, counter_id, true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void systimer_hal_select_alarm_mode(systimer_hal_context_t *hal, uint32_t alarm_id, systimer_alarm_mode_t mode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     switch (mode) { | 
					
						
							|  |  |  |     case SYSTIMER_ALARM_MODE_ONESHOT: | 
					
						
							|  |  |  |         systimer_ll_enable_alarm_oneshot(hal->dev, alarm_id); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case SYSTIMER_ALARM_MODE_PERIOD: | 
					
						
							|  |  |  |         systimer_ll_enable_alarm_period(hal->dev, alarm_id); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void systimer_hal_connect_alarm_counter(systimer_hal_context_t *hal, uint32_t alarm_id, uint32_t counter_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     systimer_ll_connect_alarm_counter(hal->dev, alarm_id, counter_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void systimer_hal_counter_can_stall_by_cpu(systimer_hal_context_t *hal, uint32_t counter_id, uint32_t cpu_id, bool can) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     systimer_ll_counter_can_stall_by_cpu(hal->dev, counter_id, cpu_id, can); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  | #if !SOC_SYSTIMER_FIXED_DIVIDER
 | 
					
						
							| 
									
										
										
										
											2022-07-01 19:58:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  | void systimer_hal_set_steps_per_tick(systimer_hal_context_t *hal, int clock_source, uint32_t steps) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* Configure the counter:
 | 
					
						
							|  |  |  |      * - increment by 1 when running from PLL (80 ticks per microsecond), | 
					
						
							|  |  |  |      * - increment by 2 when running from XTAL (40 ticks per microsecond). | 
					
						
							|  |  |  |      * Note that if the APB frequency is derived from XTAL with divider != 1, | 
					
						
							|  |  |  |      * XTAL_STEP needs to be adjusted accordingly. For example, if | 
					
						
							|  |  |  |      * the APB frequency is XTAL/4 = 10 MHz, then XTAL_STEP should be set to 8. | 
					
						
							|  |  |  |      * This is handled in systimer_hal_on_apb_freq_update function. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     switch (clock_source) { | 
					
						
							|  |  |  |     case 0: | 
					
						
							|  |  |  |         systimer_ll_set_step_for_xtal(hal->dev, steps); | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     case 1: | 
					
						
							|  |  |  |         systimer_ll_set_step_for_pll(hal->dev, steps); | 
					
						
							|  |  |  |     default: | 
					
						
							|  |  |  |         break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void systimer_hal_on_apb_freq_update(systimer_hal_context_t *hal, uint32_t apb_ticks_per_us) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /* If this function was called when switching APB clock to PLL, don't need
 | 
					
						
							|  |  |  |     * do anything: the SYSTIMER_TIMER_PLL_STEP is already correct. | 
					
						
							|  |  |  |     * If this was called when switching APB clock to XTAL, need to adjust | 
					
						
							|  |  |  |     * XTAL_STEP value accordingly. | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  |     if (apb_ticks_per_us != hal->us_to_ticks(1)) { | 
					
						
							|  |  |  |         HAL_ASSERT((hal->us_to_ticks(1) % apb_ticks_per_us) == 0 && "TICK_PER_US should be divisible by APB frequency (in MHz)"); | 
					
						
							|  |  |  |         systimer_ll_set_step_for_xtal(hal->dev, hal->us_to_ticks(1) / apb_ticks_per_us); | 
					
						
							| 
									
										
										
										
											2021-04-02 12:41:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-07-21 19:05:21 +08:00
										 |  |  | #endif // !SOC_SYSTIMER_FIXED_DIVIDER
 |