• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017-2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "esp_timer_impl.h"
16 #include "esp_err.h"
17 #include "esp_timer.h"
18 #include "esp_attr.h"
19 #include "esp_intr_alloc.h"
20 #include "esp_log.h"
21 #include "soc/periph_defs.h"
22 #include "esp_osal/esp_osal.h"
23 #include "hal/systimer_ll.h"
24 #include "hal/systimer_types.h"
25 #include "hal/systimer_hal.h"
26 
27 /**
28  * @file esp_timer_systimer.c
29  * @brief Implementation of esp_timer using systimer.
30  *
31  * This timer is a 64-bit up-counting timer, with a programmable compare value (called 'alarm' hereafter).
32  * When the timer reaches compare value, interrupt is raised.
33  * The timer can be configured to produce an edge interrupt.
34  *
35  * @note systimer counter0 and alarm2 are adopted to implemented esp_timer
36  */
37 
38 static const char *TAG = "esp_timer_systimer";
39 
40 /* Interrupt handle returned by the interrupt allocator */
41 static intr_handle_t s_timer_interrupt_handle;
42 
43 /* Function from the upper layer to be called when the interrupt happens.
44  * Registered in esp_timer_impl_init.
45  */
46 static intr_handler_t s_alarm_handler = NULL;
47 
48 /* Spinlock used to protect access to the hardware registers. */
49 portMUX_TYPE s_time_update_lock = portMUX_INITIALIZER_UNLOCKED;
50 
esp_timer_impl_lock(void)51 void esp_timer_impl_lock(void)
52 {
53     portENTER_CRITICAL(&s_time_update_lock);
54 }
55 
esp_timer_impl_unlock(void)56 void esp_timer_impl_unlock(void)
57 {
58     portEXIT_CRITICAL(&s_time_update_lock);
59 }
60 
esp_timer_impl_get_counter_reg(void)61 uint64_t IRAM_ATTR esp_timer_impl_get_counter_reg(void)
62 {
63     return systimer_hal_get_counter_value(SYSTIMER_COUNTER_0);
64 }
65 
esp_timer_impl_get_time(void)66 int64_t IRAM_ATTR esp_timer_impl_get_time(void)
67 {
68     if (s_alarm_handler == NULL) {
69         return 0;
70     }
71     return systimer_hal_get_time(SYSTIMER_COUNTER_0);
72 }
73 
74 int64_t esp_timer_get_time(void) __attribute__((alias("esp_timer_impl_get_time")));
75 
esp_timer_impl_set_alarm(uint64_t timestamp)76 void IRAM_ATTR esp_timer_impl_set_alarm(uint64_t timestamp)
77 {
78     portENTER_CRITICAL_SAFE(&s_time_update_lock);
79     systimer_hal_set_alarm_target(SYSTIMER_ALARM_2, timestamp);
80     portEXIT_CRITICAL_SAFE(&s_time_update_lock);
81 }
82 
timer_alarm_isr(void * arg)83 static void IRAM_ATTR timer_alarm_isr(void *arg)
84 {
85     // clear the interrupt
86     systimer_ll_clear_alarm_int(SYSTIMER_ALARM_2);
87     /* Call the upper layer handler */
88     (*s_alarm_handler)(arg);
89 }
90 
esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)91 void IRAM_ATTR esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us)
92 {
93     systimer_hal_on_apb_freq_update(apb_ticks_per_us);
94 }
95 
esp_timer_impl_advance(int64_t time_us)96 void esp_timer_impl_advance(int64_t time_us)
97 {
98     portENTER_CRITICAL_SAFE(&s_time_update_lock);
99     systimer_hal_counter_value_advance(SYSTIMER_COUNTER_0, time_us);
100     portEXIT_CRITICAL_SAFE(&s_time_update_lock);
101 }
102 
esp_timer_impl_init(intr_handler_t alarm_handler)103 esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler)
104 {
105     s_alarm_handler = alarm_handler;
106 #if SOC_SYSTIMER_INT_LEVEL
107     int int_type = 0;
108 #else
109     int int_type = ESP_INTR_FLAG_EDGE;
110 #endif // SOC_SYSTIMER_INT_LEVEL
111     esp_err_t err = esp_intr_alloc(ETS_SYSTIMER_TARGET2_EDGE_INTR_SOURCE,
112                                    ESP_INTR_FLAG_INTRDISABLED | ESP_INTR_FLAG_IRAM | int_type,
113                                    &timer_alarm_isr, NULL, &s_timer_interrupt_handle);
114 
115     if (err != ESP_OK) {
116         ESP_EARLY_LOGE(TAG, "esp_intr_alloc failed (%#x)", err);
117         goto err_intr_alloc;
118     }
119 
120     systimer_hal_init();
121     systimer_hal_enable_counter(SYSTIMER_COUNTER_0);
122     systimer_hal_select_alarm_mode(SYSTIMER_ALARM_2, SYSTIMER_ALARM_MODE_ONESHOT);
123     systimer_hal_connect_alarm_counter(SYSTIMER_ALARM_2, SYSTIMER_COUNTER_0);
124 
125     /* TODO: if SYSTIMER is used for anything else, access to SYSTIMER_INT_ENA_REG has to be
126     * protected by a shared spinlock. Since this code runs as part of early startup, this
127     * is practically not an issue.
128     */
129     systimer_hal_enable_alarm_int(SYSTIMER_ALARM_2);
130 
131     err = esp_intr_enable(s_timer_interrupt_handle);
132     if (err != ESP_OK) {
133         ESP_EARLY_LOGE(TAG, "esp_intr_enable failed (%#x)", err);
134         goto err_intr_en;
135     }
136     return ESP_OK;
137 
138 err_intr_en:
139     systimer_ll_disable_alarm(SYSTIMER_ALARM_2);
140     /* TODO: may need a spinlock, see the note related to SYSTIMER_INT_ENA_REG in systimer_hal_init */
141     systimer_ll_disable_alarm_int(SYSTIMER_ALARM_2);
142     esp_intr_free(s_timer_interrupt_handle);
143 err_intr_alloc:
144     s_alarm_handler = NULL;
145     return err;
146 }
147 
esp_timer_impl_deinit(void)148 void esp_timer_impl_deinit(void)
149 {
150     esp_intr_disable(s_timer_interrupt_handle);
151     systimer_ll_disable_alarm(SYSTIMER_ALARM_2);
152     /* TODO: may need a spinlock, see the note related to SYSTIMER_INT_ENA_REG in systimer_hal_init */
153     systimer_ll_disable_alarm_int(SYSTIMER_ALARM_2);
154     esp_intr_free(s_timer_interrupt_handle);
155     s_timer_interrupt_handle = NULL;
156     s_alarm_handler = NULL;
157 }
158 
esp_timer_impl_get_min_period_us(void)159 uint64_t IRAM_ATTR esp_timer_impl_get_min_period_us(void)
160 {
161     return 50;
162 }
163 
esp_timer_impl_get_alarm_reg(void)164 uint64_t esp_timer_impl_get_alarm_reg(void)
165 {
166     portENTER_CRITICAL_SAFE(&s_time_update_lock);
167     uint64_t val = systimer_hal_get_alarm_value(SYSTIMER_ALARM_2);
168     portEXIT_CRITICAL_SAFE(&s_time_update_lock);
169     return val;
170 }
171 
172 void esp_timer_private_update_apb_freq(uint32_t apb_ticks_per_us) __attribute__((alias("esp_timer_impl_update_apb_freq")));
173 void esp_timer_private_advance(int64_t time_us) __attribute__((alias("esp_timer_impl_advance")));
174 void esp_timer_private_lock(void) __attribute__((alias("esp_timer_impl_lock")));
175 void esp_timer_private_unlock(void) __attribute__((alias("esp_timer_impl_unlock")));
176