1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 /**
10  * @file private_include/esp_timer_impl.h
11  *
12  * @brief Interface between common and platform-specific parts of esp_timer.
13  *
14  * The functions in this header file are implemented for each supported SoC.
15  * High level functions defined in esp_timer.c call the functions here to
16  * interact with the hardware.
17  */
18 
19 #include <stdint.h>
20 #include "esp_err.h"
21 
22 #ifndef __ZEPHYR__
23 #include "esp_intr_alloc.h"
24 #else
25 typedef void (*intr_handler_t)(void *arg);
26 #endif
27 
28 /**
29  * @brief Minimal initialization of platform specific layer of esp_timer
30  * This function can be called very early in startup process, after this call
31  * only esp_timer_get_time function can be used.
32  * esp_timer_impl_init has to be called after this function to initialize the
33  * rest of esp_timer implementation.
34  * @return ESP_OK
35  */
36 esp_err_t esp_timer_impl_early_init(void);
37 
38 /**
39  * @brief Initialize platform specific layer of esp_timer
40  * @param alarm_handler function to call on timer interrupt
41  * Before calling this function, esp_timer_impl_early_init must be called.
42  * @return ESP_OK, ESP_ERR_NO_MEM, or one of the errors from interrupt allocator
43  */
44 esp_err_t esp_timer_impl_init(intr_handler_t alarm_handler);
45 
46 /**
47  * @brief Deinitialize platform specific layer of esp_timer
48  */
49 void esp_timer_impl_deinit(void);
50 
51 /**
52  * @brief Set up the timer interrupt to fire at a particular time
53  *
54  * If the alarm time is too close in the future, implementation should set the
55  * alarm to the earliest time possible.
56  *
57  * @param timestamp time in microseconds when interrupt should fire (relative to
58  *                  boot time, i.e. as returned by esp_timer_impl_get_time)
59  */
60 void esp_timer_impl_set_alarm(uint64_t timestamp);
61 
62 /**
63  * @brief Set up the timer interrupt to fire at a particular time for a particular alarm module.
64  *
65  * If the alarm time is too close in the future, implementation should set the
66  * alarm to the earliest time possible.
67  *
68  * @param timestamp time in microseconds when interrupt should fire (relative to
69  *                  boot time, i.e. as returned by esp_timer_impl_get_time)
70  *
71  * @param alarm_id Id alarm:
72  *                 0 - alarm_0 for the ESP_TIMER_TASK dispatch method,
73  *                 1 - alarm_1 for the ESP_TIMER_ISR dispatch method.
74  */
75 void esp_timer_impl_set_alarm_id(uint64_t timestamp, unsigned alarm_id);
76 
77 /**
78  * @brief Notify esp_timer implementation that APB frequency has changed
79  *
80  * Called by the frequency switching code.
81  *
82  * @param apb_ticks_per_us new number of APB clock ticks per microsecond
83  */
84 void esp_timer_impl_update_apb_freq(uint32_t apb_ticks_per_us);
85 
86 /**
87  * @brief Adjust current esp_timer time by a certain value
88  *
89  * Called from light sleep code to synchronize esp_timer time with RTC time.
90  *
91  * @param time_us  adjustment to apply to esp_timer time, in microseconds
92  */
93 void esp_timer_impl_advance(int64_t time_us);
94 
95 /**
96  * @brief Get time, in microseconds, since esp_timer_impl_init was called
97  * @return timestamp in microseconds
98  */
99 int64_t esp_timer_impl_get_time(void);
100 
101 /**
102  * @brief Get minimal timer period, in microseconds
103  * Periods shorter than the one returned may not be possible to achieve due to
104  * interrupt latency and context switch time. Short period of periodic timer may
105  * cause the system to spend all the time servicing the interrupt and timer
106  * callback, preventing other tasks from running.
107  * @return minimal period of periodic timer, in microseconds
108  */
109 uint64_t esp_timer_impl_get_min_period_us(void);
110 
111 /**
112  * @brief obtain internal critical section used esp_timer implementation
113  * This can be used when a sequence of calls to esp_timer has to be made,
114  * and it is necessary that the state of the timer is consistent between
115  * the calls. Should be treated in the same way as a spinlock.
116  * Call esp_timer_impl_unlock to release the lock
117  */
118 void esp_timer_impl_lock(void);
119 
120 
121 /**
122  * @brief counterpart of esp_timer_impl_lock
123  */
124 void esp_timer_impl_unlock(void);
125 
126 /**
127  * @brief Get counting register
128  *
129  * Bit depth dependents on implementation and can be 32-bit or 64-bit.
130  *
131  * @return the value of the counting register
132  */
133 uint64_t esp_timer_impl_get_counter_reg(void);
134 
135 /**
136  * @brief Get alarm register
137  *
138  * Bit depth dependents on implementation and can be 32-bit or 64-bit.
139  *
140  * @return the value of the alarm register
141  */
142 uint64_t esp_timer_impl_get_alarm_reg(void);
143 
144 #if CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER
145 /**
146  * @brief Initialize esp_timer as system time provider.
147  */
148 void esp_timer_impl_init_system_time(void);
149 #endif
150