1 /* 2 * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 #include "esp_timer_impl.h" 7 #include "esp_timer.h" 8 #include "esp_err.h" 9 #include "esp_task.h" 10 #include "esp_attr.h" 11 #include <zephyr/kernel.h> 12 13 /* Spinlock used to protect access to the hardware registers. */ 14 unsigned int s_time_update_lock; 15 16 /* Alarm values to generate interrupt on match 17 * [0] - for ESP_TIMER_TASK alarms, 18 * [1] - for ESP_TIMER_ISR alarms. 19 */ 20 uint64_t timestamp_id[2] = { UINT64_MAX, UINT64_MAX }; 21 esp_timer_impl_lock(void)22void esp_timer_impl_lock(void) 23 { 24 s_time_update_lock = irq_lock(); 25 } 26 esp_timer_impl_unlock(void)27void esp_timer_impl_unlock(void) 28 { 29 irq_unlock(s_time_update_lock); 30 } 31 32 void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock"))); 33 void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock"))); 34 esp_timer_impl_set_alarm(uint64_t timestamp)35void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp) 36 { 37 esp_timer_impl_set_alarm_id(timestamp, 0); 38 } 39 40 #ifdef CONFIG_ESP_TIMER_SUPPORTS_ISR_DISPATCH_METHOD esp_timer_impl_try_to_set_next_alarm(void)41void IRAM_ATTR esp_timer_impl_try_to_set_next_alarm(void) { 42 s_time_update_lock = irq_lock(); 43 unsigned now_alarm_idx; // ISR is called due to this current alarm 44 unsigned next_alarm_idx; // The following alarm after now_alarm_idx 45 if (timestamp_id[0] < timestamp_id[1]) { 46 now_alarm_idx = 0; 47 next_alarm_idx = 1; 48 } else { 49 now_alarm_idx = 1; 50 next_alarm_idx = 0; 51 } 52 53 if (timestamp_id[next_alarm_idx] != UINT64_MAX) { 54 // The following alarm is valid and can be used. 55 // Remove the current alarm from consideration. 56 esp_timer_impl_set_alarm_id(UINT64_MAX, now_alarm_idx); 57 } else { 58 // There is no the following alarm. 59 // Remove the current alarm from consideration as well. 60 timestamp_id[now_alarm_idx] = UINT64_MAX; 61 } 62 irq_unlock(s_time_update_lock); 63 } 64 #endif 65 66 /* FIXME: This value is safe for 80MHz APB frequency, should be modified to depend on clock frequency. */ esp_timer_impl_get_min_period_us(void)67uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us(void) 68 { 69 return 50; 70 } 71