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 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
19 #include <soc/timer_group_reg.h>
20 #else
21 #include <hal/systimer_ll.h>
22 #endif
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define HW_TIMER_BASE_CLK APB_CLK_FREQ
29 
36 /*************************************
37  *
38  * Timer1
39  *
40  * Used to implement HardwareTimer class.
41  *
42  *************************************/
43 
44 // Timer group/index to use: available on all ESP32 variants
45 #define HW_TIMER1_GROUP 0
46 #define HW_TIMER1_INDEX 0
47 
59 #define MAX_HW_TIMER1_INTERVAL 0x7fffffff
60 
67 #define MIN_HW_TIMER1_INTERVAL_US 50U
68 
69 typedef void (*hw_timer_callback_t)(void* arg);
70 
71 typedef enum {
76 
77 typedef enum {
78  TIMER_EDGE_INT = 0, // edge interrupt
79  TIMER_LEVEL_INT = 1, // level interrupt
81 
82 typedef enum {
86 
94 
101 void hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load);
102 
107 void hw_timer1_write(uint32_t ticks);
108 
112 void hw_timer1_disable(void);
113 
117 void hw_timer1_detach_interrupt(void);
118 
123 uint32_t hw_timer1_read(void);
124 
125 /*************************************
126  *
127  * Timer2 uses the idf `esp_timer` component for software-based timers (os_timer.cpp).
128  *
129  *************************************/
130 
131 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
132 #define HW_TIMER2_CLK 2000000U
133 #elif defined(CONFIG_IDF_TARGET_ESP32S2)
134 #define HW_TIMER2_CLK 80000000U
135 #elif defined(CONFIG_IDF_TARGET_ESP32C3)
136 #define HW_TIMER2_CLK 16000000U
137 #elif defined(CONFIG_IDF_TARGET_ESP32S3)
138 #define HW_TIMER2_CLK 16000000U
139 #elif defined(CONFIG_IDF_TARGET_ESP32C2)
140 #define HW_TIMER2_CLK 40000000U
141 #endif
142 
147 __forceinline static uint32_t hw_timer2_read(void)
148 {
149 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
150  REG_WRITE(TIMG_LACTUPDATE_REG(0), 1);
151  return REG_READ(TIMG_LACTLO_REG(0));
152 #elif ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 4, 0)
153  systimer_ll_counter_snapshot(&SYSTIMER, 0);
154  return systimer_ll_get_counter_value_low(&SYSTIMER, 0);
155 #elif defined(CONFIG_IDF_TARGET_ESP32S2)
156  systimer_ll_counter_snapshot();
157  return systimer_ll_get_counter_value_low();
158 #else
159  systimer_ll_counter_snapshot(0);
160  return systimer_ll_get_counter_value_low(0);
161 #endif
162 }
163 
164 #define NOW() hw_timer2_read()
165 
170 void hw_timer_init(void);
171 
174 #ifdef __cplusplus
175 }
176 #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:147
void hw_timer1_detach_interrupt(void)
Detach interrupt from the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:126
hw_timer_intr_type_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:77
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:82
hw_timer_clkdiv_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:71
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:69
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:137
@ TIMER_LEVEL_INT
Definition: Esp32/Components/driver/include/driver/hw_timer.h:79
@ TIMER_EDGE_INT
Definition: Esp32/Components/driver/include/driver/hw_timer.h:78
@ TIMER_FRC1_SOURCE
Definition: Esp32/Components/driver/include/driver/hw_timer.h:83
@ TIMER_NMI_SOURCE
Definition: Esp32/Components/driver/include/driver/hw_timer.h:84
@ TIMER_CLKDIV_1
Definition: Esp32/Components/driver/include/driver/hw_timer.h:72
@ TIMER_CLKDIV_16
Definition: Esp32/Components/driver/include/driver/hw_timer.h:73
@ TIMER_CLKDIV_256
Definition: Esp32/Components/driver/include/driver/hw_timer.h:74