11/* ************************************************************************************
2- * ESP32FastTimerInterrupt.h
3- * For ESP32 boards
4- * Written by Khoi Hoang
5- *
6- * Built by Khoi Hoang https://github.com/khoih-prog/ESP32_ISR_Servo
7- * Licensed under MIT license
8- * Version: 1.0.1
9- *
10- * The ESP32 has two timer groups, each one with two general purpose hardware timers. All the timers
11- * are based on 64 bits counters and 16 bit prescalers
12- * The timer counters can be configured to count up or down and support automatic reload and software reload
13- * They can also generate alarms when they reach a specific value, defined by the software.
14- * The value of the counter can be read by the software program.
15- *
16- * Version Modified By Date Comments
17- * ------- ----------- ---------- -----------
18- * 1.0.0 K Hoang 12/12/2019 Initial coding
19- * 1.0.1 K Hoang 13/12/2019 Add more features getPosition and getPulseWidth. Optimize.
2+ ESP32FastTimerInterrupt.h
3+ For ESP32 boards
4+ Written by Khoi Hoang
5+
6+ Built by Khoi Hoang https://github.com/khoih-prog/ESP32_ISR_Servo
7+ Licensed under MIT license
8+ Version: 1.0.1
9+
10+ The ESP32 has two timer groups, each one with two general purpose hardware timers. All the timers
11+ are based on 64 bits counters and 16 bit prescalers
12+ The timer counters can be configured to count up or down and support automatic reload and software reload
13+ They can also generate alarms when they reach a specific value, defined by the software.
14+ The value of the counter can be read by the software program.
15+
16+ Version Modified By Date Comments
17+ ------- ----------- ---------- -----------
18+ 1.0.0 K Hoang 12/12/2019 Initial coding
19+ 1.0.1 K Hoang 13/12/2019 Add more features getPosition and getPulseWidth. Optimize.
2020****************************************************************************************/
2121
2222#ifndef ESP32FastTimerInterrupt_h
3333#include < esp32-hal-timer.h>
3434
3535/* hw_timer_t defined in harware/espressif/esp32/cores/esp32/esp32-hal-timer.c:
36- typedef struct hw_timer_s {
36+ typedef struct hw_timer_s {
3737 hw_timer_reg_t * dev;
3838 uint8_t num;
3939 uint8_t group;
4040 uint8_t timer;
4141 portMUX_TYPE lock;
42- } hw_timer_t;
42+ } hw_timer_t;
4343
44- static hw_timer_t hw_timer[4] = {
44+ static hw_timer_t hw_timer[4] = {
4545 {(hw_timer_reg_t *)(DR_REG_TIMERGROUP0_BASE),0,0,0,portMUX_INITIALIZER_UNLOCKED},
4646 {(hw_timer_reg_t *)(DR_REG_TIMERGROUP0_BASE + 0x0024),1,0,1,portMUX_INITIALIZER_UNLOCKED},
4747 {(hw_timer_reg_t *)(DR_REG_TIMERGROUP0_BASE + 0x1000),2,1,0,portMUX_INITIALIZER_UNLOCKED},
4848 {(hw_timer_reg_t *)(DR_REG_TIMERGROUP0_BASE + 0x1024),3,1,1,portMUX_INITIALIZER_UNLOCKED}
49- };
49+ };
5050
51- typedef void (*voidFuncPtr)(void);
52- static voidFuncPtr __timerInterruptHandlers[4] = {0,0,0,0};
51+ typedef void (*voidFuncPtr)(void);
52+ static voidFuncPtr __timerInterruptHandlers[4] = {0,0,0,0};
5353
54- void IRAM_ATTR __timerISR(void * arg){
54+ void IRAM_ATTR __timerISR(void * arg){
5555 uint32_t s0 = TIMERG0.int_st_timers.val;
5656 uint32_t s1 = TIMERG1.int_st_timers.val;
5757 TIMERG0.int_clr_timers.val = s0;
@@ -72,12 +72,12 @@ void IRAM_ATTR __timerISR(void * arg){
7272 __timerInterruptHandlers[i]();
7373 }
7474 }
75- }
75+ }
7676
7777*/
7878
79- #ifndef USE_ESP32_TIMER_NO
80- #define USE_ESP32_TIMER_NO 3
79+ #ifndef USE_ESP32_TIMER_NO
80+ #define USE_ESP32_TIMER_NO 3
8181#endif
8282
8383
@@ -95,86 +95,86 @@ class ESP32FastTimerInterrupt
9595 private:
9696 hw_timer_t * _timer;
9797 uint8_t _timerNo;
98-
98+
9999 timer_callback _callback; // pointer to the callback function
100100 float _frequency; // Timer frequency
101101 uint64_t _timerCount; // count to activate timer
102-
102+
103103 public:
104104
105- ESP32FastTimerInterrupt ()
106- {
107- _timer = NULL ;
108- // Default timer 3
109- _timerNo = USE_ESP32_TIMER_NO; // MAX_ESP32_NUM_TIMERS;
110-
111- _frequency = 0 ;
112- _timerCount = 0 ;
113- _callback = NULL ;
114- };
105+ ESP32FastTimerInterrupt ()
106+ {
107+ _timer = NULL ;
108+ // Default timer 3
109+ _timerNo = USE_ESP32_TIMER_NO; // MAX_ESP32_NUM_TIMERS;
110+
111+ _frequency = 0 ;
112+ _timerCount = 0 ;
113+ _callback = NULL ;
114+ };
115+
116+ ESP32FastTimerInterrupt (uint8_t timerNo)
117+ {
118+ _timer = NULL ;
119+
120+ if (timerNo < MAX_ESP32_NUM_TIMERS)
121+ _timerNo = timerNo;
122+ else
123+ _timerNo = USE_ESP32_TIMER_NO; // MAX_ESP32_NUM_TIMERS
124+
125+ _frequency = 0 ;
126+ _timerCount = 0 ;
127+ _callback = NULL ;
128+ };
129+
130+ // frequency (in hertz)
131+ // No params and duration now. To be added in the future by adding similar functions here or to esp32-hal-timer.c
132+ bool setFrequency (float frequency, timer_callback callback)
133+ {
134+ // select timer frequency is 1MHz => 1us, _timerCount = 10 for 10us pulse
135+ // Will use later if very low frequency is needed.
136+ _frequency = 1000000 ;
137+ _timerCount = (uint64_t ) _frequency / frequency;
138+ // count up
139+
140+ #if (TIMER_INTERRUPT_DEBUG > 0)
141+ Serial.println (" ESP32TimerInterrupt: _timerNo = " + String (_timerNo) + " , _fre = " + String (_frequency)
142+ + " , _count = " + String ((uint32_t ) (_timerCount >> 32 ) ) + " - " + String ((uint32_t ) (_timerCount)));
143+ #endif
115144
116- ESP32FastTimerInterrupt (uint8_t timerNo)
117- {
118- _timer = NULL ;
145+ // Clock to timer (prescaler) is F_CPU / 3 = 240MHz / 3 = 80MHz
146+ _timer = timerBegin (_timerNo, F_CPU / (_frequency * 3 ), true );
147+ // Interrupt on EGDE
148+ timerAttachInterrupt (_timer, callback, true );
119149
120- if (timerNo < MAX_ESP32_NUM_TIMERS)
121- _timerNo = timerNo;
122- else
123- _timerNo = USE_ESP32_TIMER_NO; // MAX_ESP32_NUM_TIMERS
150+ // autoreload = true to run forever
151+ timerAlarmWrite (_timer, _timerCount, true );
124152
125- _frequency = 0 ;
126- _timerCount = 0 ;
127- _callback = NULL ;
128- };
129-
130- // frequency (in hertz)
131- // No params and duration now. To be added in the future by adding similar functions here or to esp32-hal-timer.c
132- bool setFrequency (float frequency, timer_callback callback)
133- {
134- // select timer frequency is 1MHz => 1us, _timerCount = 10 for 10us pulse
135- // Will use later if very low frequency is needed.
136- _frequency = 1000000 ;
137- _timerCount = (uint64_t ) _frequency / frequency;
138- // count up
139-
140- #if (TIMER_INTERRUPT_DEBUG > 0)
141- Serial.println (" ESP32TimerInterrupt: _timerNo = " + String (_timerNo) + " , _fre = " + String (_frequency)
142- + " , _count = " + String ((uint32_t ) (_timerCount>>32 ) ) + " - " + String ((uint32_t ) (_timerCount)));
143- #endif
144-
145- // Clock to timer (prescaler) is F_CPU / 3 = 240MHz / 3 = 80MHz
146- _timer = timerBegin (_timerNo, F_CPU / (_frequency * 3 ), true );
147- // Interrupt on EGDE
148- timerAttachInterrupt (_timer, callback, true );
149-
150- // autoreload = true to run forever
151- timerAlarmWrite (_timer, _timerCount, true );
152-
153- timerAlarmEnable (_timer);
154-
155- _callback = callback;
156-
157- return true ;
158- }
159-
160- // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
161- // No params and duration now. To be addes in the future by adding similar functions here or to esp32-hal-timer.c
162- bool attachInterruptInterval (unsigned long interval, timer_callback callback)
163- {
164- return setFrequency ( (float ) ( 1000000 .0f / interval), callback);
165- }
153+ timerAlarmEnable (_timer);
166154
167- void detachInterrupt ()
168- {
169- timerDetachInterrupt (_timer);
170- }
155+ _callback = callback;
171156
172- // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
173- void reattachInterrupt ()
174- {
175- if ( (_frequency != 0 ) && (_timerCount != 0 ) && (_callback != NULL ) )
176- setFrequency (_frequency, _callback);
177- }
157+ return true ;
158+ }
159+
160+ // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
161+ // No params and duration now. To be addes in the future by adding similar functions here or to esp32-hal-timer.c
162+ bool attachInterruptInterval (unsigned long interval, timer_callback callback)
163+ {
164+ return setFrequency ( (float ) ( 1000000 .0f / interval), callback);
165+ }
166+
167+ void detachInterrupt ()
168+ {
169+ timerDetachInterrupt (_timer);
170+ }
171+
172+ // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
173+ void reattachInterrupt ()
174+ {
175+ if ( (_frequency != 0 ) && (_timerCount != 0 ) && (_callback != NULL ) )
176+ setFrequency (_frequency, _callback);
177+ }
178178}; // class ESP32FastTimerInterrupt
179179
180180#endif // #ifndef ESP32FastTimerInterrupt_h
0 commit comments