Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 08b3ea3

Browse files
authored
v1.0.2 fix indentation
1 parent 475cca3 commit 08b3ea3

File tree

3 files changed

+265
-263
lines changed

3 files changed

+265
-263
lines changed

src/ESP8266FastTimerInterrupt.h

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
/****************************************************************************************************************************
2-
* ESP8266FastTimerInterrupt.h
3-
* For ESP8266 boards
4-
* Written by Khoi Hoang
5-
*
6-
* Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_ISR_Servo
7-
* Licensed under MIT license
8-
* Version: 1.0.2
9-
*
10-
* The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
11-
* The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
12-
* The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
13-
* Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
14-
*
15-
* Version Modified By Date Comments
16-
* ------- ----------- ---------- -----------
17-
* 1.0.0 K Hoang 04/12/2019 Initial coding
18-
* 1.0.1 K Hoang 05/12/2019 Add more features getPosition and getPulseWidth. Optimize.
19-
* 1.0.2 K Hoang 20/12/2019 Add more Blynk examples.Change example names to avoid duplication.
2+
ESP8266FastTimerInterrupt.h
3+
For ESP8266 boards
4+
Written by Khoi Hoang
5+
6+
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_ISR_Servo
7+
Licensed under MIT license
8+
Version: 1.0.2
9+
10+
The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
11+
The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
12+
The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
13+
Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
14+
15+
Version Modified By Date Comments
16+
------- ----------- ---------- -----------
17+
1.0.0 K Hoang 04/12/2019 Initial coding
18+
1.0.1 K Hoang 05/12/2019 Add more features getPosition and getPulseWidth. Optimize.
19+
1.0.2 K Hoang 20/12/2019 Add more Blynk examples.Change example names to avoid duplication.
2020
*****************************************************************************************************************************/
2121

2222
#ifndef ESP8266FastTimerInterrupt_h
@@ -32,29 +32,29 @@
3232

3333
/* From /arduino-1.8.10/hardware/esp8266com/esp8266/cores/esp8266/esp8266_peri.h
3434
35-
#define ESP8266_REG(addr) *((volatile uint32_t *)(0x60000000+(addr)))
36-
#define ESP8266_DREG(addr) *((volatile uint32_t *)(0x3FF00000+(addr)))
37-
#define ESP8266_CLOCK 80000000UL
35+
#define ESP8266_REG(addr) *((volatile uint32_t *)(0x60000000+(addr)))
36+
#define ESP8266_DREG(addr) *((volatile uint32_t *)(0x3FF00000+(addr)))
37+
#define ESP8266_CLOCK 80000000UL
3838
39-
//CPU Register
40-
#define CPU2X ESP8266_DREG(0x14) //when bit 0 is set, F_CPU = 160MHz
39+
//CPU Register
40+
#define CPU2X ESP8266_DREG(0x14) //when bit 0 is set, F_CPU = 160MHz
4141
*/
4242

4343
/* From /arduino-1.8.10/hardware/esp8266com/esp8266/cores/esp8266/Arduino.h
4444
45-
//timer dividers
46-
enum TIM_DIV_ENUM {
45+
//timer dividers
46+
enum TIM_DIV_ENUM {
4747
TIM_DIV1 = 0, // 80 / 160 MHz (80 / 160 ticks/us - 104857.588 us max)
4848
TIM_DIV16 = 1, // 5 / 10 MHz (5 / 10 ticks/us - 1677721.4 us max)
4949
TIM_DIV256 = 3 // 312.5 / 625 Khz (1 tick = 3.2 / 1.6 us - 26843542.4 us max)
50-
};
50+
};
5151
52-
//timer int_types
53-
#define TIM_EDGE 0
54-
#define TIM_LEVEL 1
55-
//timer reload values
56-
#define TIM_SINGLE 0 //on interrupt routine you need to write a new value to start the timer again
57-
#define TIM_LOOP 1 //on interrupt the counter will start with the same value again
52+
//timer int_types
53+
#define TIM_EDGE 0
54+
#define TIM_LEVEL 1
55+
//timer reload values
56+
#define TIM_SINGLE 0 //on interrupt routine you need to write a new value to start the timer again
57+
#define TIM_LOOP 1 //on interrupt the counter will start with the same value again
5858
5959
*/
6060

@@ -71,75 +71,75 @@ typedef void (*timer_callback) (void);
7171

7272
class ESP8266TimerInterrupt
7373
{
74-
private:
74+
private:
7575
timer_callback _callback; // pointer to the callback function
7676
float _frequency; // Timer frequency
7777
uint32_t _timerCount; // count to activate timer
78-
78+
7979
public:
8080

81-
ESP8266TimerInterrupt()
82-
{
83-
_frequency = 0;
84-
_timerCount = 0;
85-
_callback = NULL;
86-
};
87-
88-
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
89-
// No params and duration now. To be addes in the future by adding similar functions here or to esp32-hal-timer.c
90-
bool setFrequency(float frequency, timer_callback callback)
91-
{
92-
bool isOKFlag = true;
93-
94-
// ESP8266 only has one usable timer1, max count is only 8,388,607. So to get longer time, we use 16 divider to get 10us
95-
//_frequency = 80000000 / 256;
96-
_frequency = 80000000 / 16;
97-
_timerCount = (uint32_t) _frequency / frequency;
98-
_callback = callback;
99-
100-
if( _timerCount > MAX_ESP8266_COUNT)
81+
ESP8266TimerInterrupt()
82+
{
83+
_frequency = 0;
84+
_timerCount = 0;
85+
_callback = NULL;
86+
};
87+
88+
// frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
89+
// No params and duration now. To be addes in the future by adding similar functions here or to esp32-hal-timer.c
90+
bool setFrequency(float frequency, timer_callback callback)
91+
{
92+
bool isOKFlag = true;
93+
94+
// ESP8266 only has one usable timer1, max count is only 8,388,607. So to get longer time, we use 16 divider to get 10us
95+
//_frequency = 80000000 / 256;
96+
_frequency = 80000000 / 16;
97+
_timerCount = (uint32_t) _frequency / frequency;
98+
_callback = callback;
99+
100+
if ( _timerCount > MAX_ESP8266_COUNT)
101+
{
102+
_timerCount = MAX_ESP8266_COUNT;
103+
// Flag error
104+
isOKFlag = false;
105+
}
106+
107+
// count up
108+
#if (TIMER_INTERRUPT_DEBUG > 0)
109+
Serial.println("ESP8266TimerInterrupt: _fre = " + String(_frequency) + ", _count = " + String(_timerCount));
110+
#endif
111+
112+
// Clock to timer (prescaler) is always 80MHz, even F_CPU is 160 MHz
113+
114+
timer1_attachInterrupt(callback);
115+
116+
timer1_write(_timerCount);
117+
118+
// Interrupt on EGDE, autoloop
119+
//timer1_enable(TIM_DIV256, TIM_EDGE, TIM_LOOP);
120+
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
121+
122+
return isOKFlag;
123+
}
124+
125+
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
126+
// No params and duration now. To be addes in the future by adding similar functions here or to esp32-hal-timer.c
127+
bool attachInterruptInterval(unsigned long interval, timer_callback callback)
128+
{
129+
return setFrequency( (float) ( 1000000.0f / interval), callback);
130+
}
131+
132+
void detachInterrupt()
133+
{
134+
timer1_disable();
135+
}
136+
137+
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
138+
void reattachInterrupt()
101139
{
102-
_timerCount = MAX_ESP8266_COUNT;
103-
// Flag error
104-
isOKFlag = false;
140+
if ( (_frequency != 0) && (_timerCount != 0) && (_callback != NULL) )
141+
setFrequency(_frequency, _callback);
105142
}
106-
107-
// count up
108-
#if (TIMER_INTERRUPT_DEBUG > 0)
109-
Serial.println("ESP8266TimerInterrupt: _fre = " + String(_frequency) + ", _count = " + String(_timerCount));
110-
#endif
111-
112-
// Clock to timer (prescaler) is always 80MHz, even F_CPU is 160 MHz
113-
114-
timer1_attachInterrupt(callback);
115-
116-
timer1_write(_timerCount);
117-
118-
// Interrupt on EGDE, autoloop
119-
//timer1_enable(TIM_DIV256, TIM_EDGE, TIM_LOOP);
120-
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
121-
122-
return isOKFlag;
123-
}
124-
125-
// interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely
126-
// No params and duration now. To be addes in the future by adding similar functions here or to esp32-hal-timer.c
127-
bool attachInterruptInterval(unsigned long interval, timer_callback callback)
128-
{
129-
return setFrequency( (float) ( 1000000.0f / interval), callback);
130-
}
131-
132-
void detachInterrupt()
133-
{
134-
timer1_disable();
135-
}
136-
137-
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
138-
void reattachInterrupt()
139-
{
140-
if ( (_frequency != 0) && (_timerCount != 0) && (_callback != NULL) )
141-
setFrequency(_frequency, _callback);
142-
}
143143
}; // class ESP8266TimerInterrupt
144144

145145
#endif //#ifndef ESP8266TimerInterrupt_h

0 commit comments

Comments
 (0)