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 
17 #ifdef CONFIG_ESP_TIMER_IMPL_TG0_LAC
18 #include <soc/timer_group_reg.h>
19 #else
20 #include <hal/systimer_ll.h>
21 #endif
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define HW_TIMER_BASE_CLK APB_CLK_FREQ
28 
35 /*************************************
36  *
37  * Timer1
38  *
39  * Used to implement HardwareTimer class.
40  *
41  *************************************/
42 
43 // Timer group/index to use: available on all ESP32 variants
44 #define HW_TIMER1_GROUP TIMER_GROUP_0
45 #define HW_TIMER1_INDEX TIMER_0
46 
58 #define MAX_HW_TIMER1_INTERVAL 0x7fffffff
59 
66 #define MIN_HW_TIMER1_INTERVAL_US 50U
67 
68 typedef void (*hw_timer_callback_t)(void* arg);
69 
70 typedef enum {
75 
76 typedef enum {
77  TIMER_EDGE_INT = 0, // edge interrupt
78  TIMER_LEVEL_INT = 1, // level interrupt
80 
81 typedef enum {
85 
92 void IRAM_ATTR hw_timer1_attach_interrupt(hw_timer_source_type_t source_type, hw_timer_callback_t callback, void* arg);
93 
100 void IRAM_ATTR hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load);
101 
106 void IRAM_ATTR hw_timer1_write(uint32_t ticks);
107 
111 void IRAM_ATTR hw_timer1_disable(void);
112 
116 void IRAM_ATTR hw_timer1_detach_interrupt(void);
117 
122 uint32_t hw_timer1_read(void);
123 
124 /*************************************
125  *
126  * Timer2 uses the idf `esp_timer` component for software-based timers (os_timer.cpp).
127  *
128  *************************************/
129 
130 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
131 #define HW_TIMER2_CLK 2000000U
132 #define HW_TIMER2_INDEX 0
133 #elif defined(CONFIG_IDF_TARGET_ESP32S2)
134 #define HW_TIMER2_CLK 80000000U
135 #define HW_TIMER2_INDEX
136 #elif defined(CONFIG_IDF_TARGET_ESP32C3)
137 #define HW_TIMER2_CLK 16000000U
138 #define HW_TIMER2_INDEX 0
139 #elif defined(CONFIG_IDF_TARGET_ESP32S3)
140 #define HW_TIMER2_CLK 16000000U
141 #define HW_TIMER2_INDEX 0
142 #else
143 _Static_assert(false, "ESP32 Unsupported timer");
144 #endif
145 
150 __forceinline static uint32_t hw_timer2_read(void)
151 {
152 #if CONFIG_ESP_TIMER_IMPL_TG0_LAC
153  return REG_READ(TIMG_LACTLO_REG(HW_TIMER2_INDEX));
154 #else
155  systimer_ll_counter_snapshot(HW_TIMER2_INDEX);
156  return systimer_ll_get_counter_value_low(HW_TIMER2_INDEX);
157 #endif
158 }
159 
160 #define NOW() hw_timer2_read()
161 
166 void hw_timer_init(void);
167 
170 #ifdef __cplusplus
171 }
172 #endif
void hw_timer1_write(uint32_t ticks)
Set the timer interval.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:104
Definition: Esp32/Components/driver/include/driver/hw_timer.h:72
Definition: Esp32/Components/driver/include/driver/hw_timer.h:78
hw_timer_source_type_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:81
void hw_timer1_disable(void)
Disable the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:112
uint32_t hw_timer2_read(void)
Read current timer2 value.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:157
Definition: Esp32/Components/driver/include/driver/hw_timer.h:73
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:85
Definition: Esp32/Components/driver/include/driver/hw_timer.h:77
hw_timer_clkdiv_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:70
void hw_timer_init(void)
Initialise hardware timers.
void(* hw_timer_callback_t)(void *arg)
Definition: Esp32/Components/driver/include/driver/hw_timer.h:68
hw_timer_intr_type_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:76
_Static_assert(false, "ESP32 Unsupported timer")
Definition: Esp32/Components/driver/include/driver/hw_timer.h:71
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.
void hw_timer1_detach_interrupt(void)
Detach interrupt from the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:121
uint32_t hw_timer1_read(void)
Get timer1 count.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:132
Definition: Esp32/Components/driver/include/driver/hw_timer.h:82
Definition: Esp32/Components/driver/include/driver/hw_timer.h:83