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_systemapi.h>
14 #include <soc/frc_timer_reg.h>
15 
16 #define HW_TIMER_BASE_CLK APB_CLK_FREQ
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
28 /*************************************
29  *
30  * FRC1 timer
31  *
32  * This is a 23-bit countdown timer
33  *
34  *************************************/
35 
44 #define MAX_HW_TIMER1_INTERVAL 0x7fffff
45 
52 #define MIN_HW_TIMER1_INTERVAL_US 50U
53 
54 typedef void (*hw_timer_callback_t)(void* arg);
55 
56 typedef enum {
61 
62 typedef enum {
63  TIMER_EDGE_INT = 0, // edge interrupt
64  TIMER_LEVEL_INT = 1, // level interrupt
66 
67 typedef enum {
71 
78 void IRAM_ATTR hw_timer1_attach_interrupt(hw_timer_source_type_t source_type, hw_timer_callback_t callback, void* arg);
79 
86 inline void IRAM_ATTR hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load)
87 {
88  uint32_t ctrl = (div & 0x0C) | (intr_type & 0x01) | FRC_TIMER_ENABLE;
89  if(auto_load) {
90  ctrl |= FRC_TIMER_AUTOLOAD;
91  }
92 
93  REG_WRITE(FRC_TIMER_CTRL_REG(0), ctrl);
94  // TM1_EDGE_INT_ENABLE();
95  // ETS_FRC1_INTR_ENABLE();
96 }
97 
102 __forceinline void IRAM_ATTR hw_timer1_write(uint32_t ticks)
103 {
104  REG_WRITE(FRC_TIMER_LOAD_REG(0), ticks);
105 }
106 
110 __forceinline void IRAM_ATTR hw_timer1_disable(void)
111 {
112  // TM1_EDGE_INT_DISABLE();
113  // ETS_FRC1_INTR_DISABLE();
114 }
115 
119 __forceinline void IRAM_ATTR hw_timer1_detach_interrupt(void)
120 {
122  // ETS_FRC_TIMER1_NMI_INTR_ATTACH(NULL);
123  // ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL);
124 }
125 
130 __forceinline uint32_t hw_timer1_read(void)
131 {
132  return REG_READ(FRC_TIMER_COUNT_REG(0));
133 }
134 
135 /*************************************
136  *
137  * FRC2 timer
138  *
139  * This is a 32-bit count-up timer
140  *
141  * See idf components/esp32/esp_timer_esp32.c
142  *
143  *************************************/
144 
145 constexpr uint32_t HW_TIMER2_CLKDIV = TIMER_CLKDIV_1;
147 
152 __forceinline uint32_t hw_timer2_read(void)
153 {
154  return REG_READ(FRC_TIMER_COUNT_REG(1));
155 }
156 
157 #define NOW() hw_timer2_read()
158 
163 void hw_timer_init(void);
164 
167 #ifdef __cplusplus
168 }
169 #endif
void hw_timer1_write(uint32_t ticks)
Set the timer interval.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:104
uint32_t hw_timer2_read(void)
Read current timer2 value.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:157
#define __forceinline
Definition: sming_attr.h:13
constexpr uint32_t HW_TIMER2_CLK
Definition: Esp32/Components/driver/include/driver/hw_timer.h:146
Definition: Esp32/Components/driver/include/driver/hw_timer.h:58
Definition: Esp32/Components/driver/include/driver/hw_timer.h:64
hw_timer_source_type_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:67
void hw_timer1_disable(void)
Disable the timer.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:112
Definition: Esp32/Components/driver/include/driver/hw_timer.h:59
#define HW_TIMER_BASE_CLK
Definition: Esp32/Components/driver/include/driver/hw_timer.h:16
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:63
hw_timer_clkdiv_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:56
void hw_timer_init(void)
Initialise hardware timers.
void(* hw_timer_callback_t)(void *arg)
Definition: Esp32/Components/driver/include/driver/hw_timer.h:54
hw_timer_intr_type_t
Definition: Esp32/Components/driver/include/driver/hw_timer.h:62
Definition: Esp32/Components/driver/include/driver/hw_timer.h:57
constexpr uint32_t HW_TIMER2_CLKDIV
Definition: Esp32/Components/driver/include/driver/hw_timer.h:145
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:68
Definition: Esp32/Components/driver/include/driver/hw_timer.h:69