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 
85 inline void IRAM_ATTR hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load)
86 {
87  constexpr uint32_t FRC1_ENABLE_TIMER = BIT7;
88  constexpr uint32_t FRC1_AUTO_LOAD = BIT6;
89 
90  uint32_t ctrl = (div & 0x0C) | (intr_type & 0x01) | FRC1_ENABLE_TIMER;
91  if(auto_load) {
92  ctrl |= FRC1_AUTO_LOAD;
93  }
94 
95  WRITE_PERI_REG(FRC1_CTRL_ADDRESS, ctrl);
96  TM1_EDGE_INT_ENABLE();
97  ETS_FRC1_INTR_ENABLE();
98 }
99 
104 __forceinline void IRAM_ATTR hw_timer1_write(uint32_t ticks)
105 {
106  WRITE_PERI_REG(FRC1_LOAD_ADDRESS, ticks);
107 }
108 
112 __forceinline void IRAM_ATTR hw_timer1_disable(void)
113 {
114  TM1_EDGE_INT_DISABLE();
115  ETS_FRC1_INTR_DISABLE();
116 }
117 
122 {
124  ETS_FRC_TIMER1_NMI_INTR_ATTACH(NULL);
125  ETS_FRC_TIMER1_INTR_ATTACH(NULL, NULL);
126 }
127 
133 {
134  return READ_PERI_REG(FRC1_COUNT_ADDRESS);
135 }
136 
137 /*************************************
138  *
139  * FRC2 timer
140  *
141  * This is a 32-bit count-up timer
142  *
143  *************************************/
144 
145 #ifdef USE_US_TIMER
146 constexpr uint32_t HW_TIMER2_CLKDIV = TIMER_CLKDIV_16;
147 #else
148 constexpr uint32_t HW_TIMER2_CLKDIV = TIMER_CLKDIV_256;
149 #endif
150 
152 
158 {
159  return READ_PERI_REG(FRC2_COUNT_ADDRESS);
160 }
161 
167 __forceinline void hw_timer2_set_alarm(uint32_t ticks)
168 {
169  WRITE_PERI_REG(FRC2_ALARM_ADDRESS, ticks);
170 }
171 
176 void hw_timer_init(void);
177 
180 #ifdef __cplusplus
181 }
182 #endif
void hw_timer1_write(uint32_t ticks)
Set the timer interval.
Definition: hw_timer.h:104
uint32_t hw_timer2_read(void)
Read current timer2 value.
Definition: hw_timer.h:157
#define __forceinline
Definition: sming_attr.h:13
constexpr uint32_t HW_TIMER2_CLK
Definition: hw_timer.h:151
Definition: hw_timer.h:57
Definition: hw_timer.h:63
hw_timer_source_type_t
Definition: hw_timer.h:66
void hw_timer1_disable(void)
Disable the timer.
Definition: hw_timer.h:112
Definition: hw_timer.h:58
#define HW_TIMER_BASE_CLK
Definition: hw_timer.h:15
void hw_timer1_enable(hw_timer_clkdiv_t div, hw_timer_intr_type_t intr_type, bool auto_load)
Enable the timer.
Definition: hw_timer.h:85
Definition: hw_timer.h:62
hw_timer_clkdiv_t
Definition: hw_timer.h:55
void hw_timer_init(void)
Initialise hardware timers.
void(* hw_timer_callback_t)(void *arg)
Definition: hw_timer.h:53
hw_timer_intr_type_t
Definition: hw_timer.h:61
Definition: hw_timer.h:56
constexpr uint32_t HW_TIMER2_CLKDIV
Definition: hw_timer.h:148
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: hw_timer.h:121
uint32_t hw_timer1_read(void)
Get timer1 count.
Definition: hw_timer.h:132
Definition: hw_timer.h:67
void hw_timer2_set_alarm(uint32_t ticks)
Set timer2 alarm count value.
Definition: hw_timer.h:167
Definition: hw_timer.h:68