Esp8266/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 
15 #define HW_TIMER_BASE_CLK APB_CLK_FREQ
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
27 /*************************************
28  *
29  * FRC1 timer
30  *
31  * This is a 23-bit countdown timer
32  *
33  *************************************/
34 
43 #define MAX_HW_TIMER1_INTERVAL 0x7fffff
44 
51 #define MIN_HW_TIMER1_INTERVAL_US 50U
52 
53 typedef void (*hw_timer_callback_t)(void* arg);
54 
55 typedef enum {
60 
61 typedef enum {
62  TIMER_EDGE_INT = 0, // edge interrupt
63  TIMER_LEVEL_INT = 1, // level interrupt
65 
66 typedef enum {
70 
77 void IRAM_ATTR hw_timer1_attach_interrupt(hw_timer_source_type_t source_type, hw_timer_callback_t callback, void* arg);
78 
90 inline void IRAM_ATTR hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load)
91 {
92 #define FRC1_ENABLE_TIMER BIT7
93 #define FRC1_AUTO_LOAD BIT6
94 
95  uint32_t ctrl = (div & 0x0C) | (intr_type & 0x01) | FRC1_ENABLE_TIMER;
96  if(auto_load) {
97  ctrl |= FRC1_AUTO_LOAD;
98  }
99 
100  WRITE_PERI_REG(FRC1_CTRL_ADDRESS, ctrl);
101  TM1_EDGE_INT_ENABLE();
102  ETS_FRC1_INTR_ENABLE();
103 }
104 
109 __forceinline void IRAM_ATTR hw_timer1_write(uint32_t ticks)
110 {
111  WRITE_PERI_REG(FRC1_LOAD_ADDRESS, ticks);
112 }
113 
117 __forceinline void IRAM_ATTR hw_timer1_disable(void)
118 {
119  TM1_EDGE_INT_DISABLE();
120  ETS_FRC1_INTR_DISABLE();
121 }
122 
126 __forceinline void IRAM_ATTR hw_timer1_detach_interrupt(void)
127 {
129  ETS_FRC_TIMER1_NMI_INTR_ATTACH(NULL);
130  ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL);
131 }
132 
137 __forceinline uint32_t hw_timer1_read(void)
138 {
139  return READ_PERI_REG(FRC1_COUNT_ADDRESS);
140 }
141 
142 /*************************************
143  *
144  * FRC2 timer
145  *
146  * This is a 32-bit count-up timer
147  *
148  *************************************/
149 
150 #ifdef USE_US_TIMER
151 #define HW_TIMER2_CLKDIV TIMER_CLKDIV_16
152 #else
153 #define HW_TIMER2_CLKDIV TIMER_CLKDIV_256
154 #endif
155 
156 #define HW_TIMER2_CLK (HW_TIMER_BASE_CLK >> HW_TIMER2_CLKDIV)
157 
162 __forceinline uint32_t hw_timer2_read(void)
163 {
164  return READ_PERI_REG(FRC2_COUNT_ADDRESS);
165 }
166 
172 __forceinline void hw_timer2_set_alarm(uint32_t ticks)
173 {
174  WRITE_PERI_REG(FRC2_ALARM_ADDRESS, ticks);
175 }
176 
181 void hw_timer_init(void);
182 
185 #ifdef __cplusplus
186 }
187 #endif
#define FRC1_AUTO_LOAD
#define FRC1_ENABLE_TIMER
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_timer2_set_alarm(uint32_t ticks)
Set timer2 alarm count value.
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:172
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: Esp8266/Components/driver/include/driver/hw_timer.h:61
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: Esp8266/Components/driver/include/driver/hw_timer.h:66
hw_timer_clkdiv_t
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:55
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: Esp8266/Components/driver/include/driver/hw_timer.h:53
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: Esp8266/Components/driver/include/driver/hw_timer.h:63
@ TIMER_EDGE_INT
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:62
@ TIMER_FRC1_SOURCE
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:67
@ TIMER_NMI_SOURCE
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:68
@ TIMER_CLKDIV_1
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:56
@ TIMER_CLKDIV_16
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:57
@ TIMER_CLKDIV_256
Definition: Esp8266/Components/driver/include/driver/hw_timer.h:58