Esp32/Components/driver/include/driver/hw_timer.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * hw_timer.h
8  *
9  ****/
10 
11 #pragma once
12 
13 #include <esp_attr.h>
14 #include <sming_attr.h>
15 #include <stdint.h>
16 #include <esp_idf_version.h>
17 
18 #pragma GCC diagnostic push
19 #pragma GCC diagnostic ignored "-Wunused-parameter"
20 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
21 #include <soc/timer_group_reg.h>
22 #else
23 #include <hal/systimer_ll.h>
24 #endif
25 #pragma GCC diagnostic pop
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #define HW_TIMER_BASE_CLK APB_CLK_FREQ
32 
39 /*************************************
40  *
41  * Timer1
42  *
43  * Used to implement HardwareTimer class.
44  *
45  *************************************/
46 
47 // Timer group/index to use: available on all ESP32 variants
48 #define HW_TIMER1_GROUP 0
49 #define HW_TIMER1_INDEX 0
50 
62 #define MAX_HW_TIMER1_INTERVAL 0x7fffffff
63 
70 #define MIN_HW_TIMER1_INTERVAL_US 50U
71 
72 typedef void (*hw_timer_callback_t)(void* arg);
73 
74 typedef enum {
79 
80 typedef enum {
81  TIMER_EDGE_INT = 0, // edge interrupt
82  TIMER_LEVEL_INT = 1, // level interrupt
84 
85 typedef enum {
89 
97 
104 void hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load);
105 
110 void hw_timer1_write(uint32_t ticks);
111 
115 void hw_timer1_disable(void);
116 
121 
126 uint32_t hw_timer1_read(void);
127 
128 /*************************************
129  *
130  * Timer2 uses the idf `esp_timer` component for software-based timers (os_timer.cpp).
131  *
132  *************************************/
133 
134 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
135 #define HW_TIMER2_CLK 2000000U
136 #elif defined(CONFIG_IDF_TARGET_ESP32S2)
137 #define HW_TIMER2_CLK 80000000U
138 #elif defined(CONFIG_IDF_TARGET_ESP32C3)
139 #define HW_TIMER2_CLK 16000000U
140 #elif defined(CONFIG_IDF_TARGET_ESP32S3)
141 #define HW_TIMER2_CLK 16000000U
142 #elif defined(CONFIG_IDF_TARGET_ESP32C2)
143 #define HW_TIMER2_CLK 40000000U
144 #endif
145 
150 __forceinline static uint32_t hw_timer2_read(void)
151 {
152 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
153  REG_WRITE(TIMG_LACTUPDATE_REG(0), 1);
154  return REG_READ(TIMG_LACTLO_REG(0));
155 #elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
156  systimer_ll_counter_snapshot(&SYSTIMER, 0);
157  return systimer_ll_get_counter_value_low(&SYSTIMER, 0);
158 #elif defined(CONFIG_IDF_TARGET_ESP32S2)
159  systimer_ll_counter_snapshot();
160  return systimer_ll_get_counter_value_low();
161 #else
162  systimer_ll_counter_snapshot(0);
163  return systimer_ll_get_counter_value_low(0);
164 #endif
165 }
166 
167 #define NOW() hw_timer2_read()
168 
173 void hw_timer_init(void);
174 
177 #ifdef __cplusplus
178 }
179 #endif
void hw_timer1_attach_interrupt(hw_timer_source_type_t source_type, hw_timer_callback_t callback, void *arg)
Attach an interrupt for the timer.
static uint32_t hw_timer2_read(void)
Read current timer2 value.
Definition: Esp32/Components/driver/include/driver/hw_timer.h:150
void hw_timer1_detach_interrupt(void)
Detach interrupt from the timer.
hw_timer_intr_type_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:80
void hw_timer1_disable(void)
Disable the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:117
void hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load)
Enable the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:90
hw_timer_source_type_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:85
hw_timer_clkdiv_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:74
void hw_timer1_write(uint32_t ticks)
Set the timer interval.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:109
void(* hw_timer_callback_t)(void *arg)
Definition: Esp32/Components/driver/include/driver/hw_timer.h:72
void hw_timer_init(void)
Initialise hardware timers.
uint32_t hw_timer1_read(void)
Get timer1 count.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:132
@ TIMER_LEVEL_INT
Definition: Esp32/Components/driver/include/driver/hw_timer.h:82
@ TIMER_EDGE_INT
Definition: Esp32/Components/driver/include/driver/hw_timer.h:81
@ TIMER_FRC1_SOURCE
Definition: Esp32/Components/driver/include/driver/hw_timer.h:86
@ TIMER_NMI_SOURCE
Definition: Esp32/Components/driver/include/driver/hw_timer.h:87
@ TIMER_CLKDIV_1
Definition: Esp32/Components/driver/include/driver/hw_timer.h:75
@ TIMER_CLKDIV_16
Definition: Esp32/Components/driver/include/driver/hw_timer.h:76
@ TIMER_CLKDIV_256
Definition: Esp32/Components/driver/include/driver/hw_timer.h:77