1
1
/* ***************************************************************************************************************************
2
- Change_Interval.ino
3
- For NRF52 boards using mbed-RTOS such as Nano-33-BLE
4
- Written by Khoi Hoang
5
-
6
- Built by Khoi Hoang https://github.com/khoih-prog/NRF52_MBED_TimerInterrupt
7
- Licensed under MIT license
8
-
9
- Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
10
- unsigned long miliseconds), you just consume only one NRF52 timer and avoid conflicting with other cores' tasks.
11
- The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
12
- Therefore, their executions are not blocked by bad-behaving functions / tasks.
13
- This important feature is absolutely necessary for mission-critical tasks.
14
-
15
- Based on SimpleTimer - A timer library for Arduino.
16
- Author: mromani@ottotecnica.com
17
- Copyright (c) 2010 OTTOTECNICA Italy
18
-
19
- Based on BlynkTimer.h
20
- Author: Volodymyr Shymanskyy
21
-
22
- Version: 1.0.2
23
-
24
- Version Modified By Date Comments
25
- ------- ----------- ---------- -----------
26
- 1.0.1 K Hoang 22/11/2020 Initial coding and sync with NRF52_TimerInterrupt
27
- 1.0.2 K Hoang 23/11/2020 Add and optimize examples
2
+ Change_Interval.ino
3
+ For ESP8266 boards
4
+ Written by Khoi Hoang
5
+
6
+ Built by Khoi Hoang https://github.com/khoih-prog/ESP8266TimerInterrupt
7
+ Licensed under MIT license
8
+
9
+ The ESP8266 timers are badly designed, using only 23-bit counter along with maximum 256 prescaler. They're only better than UNO / Mega.
10
+ The ESP8266 has two hardware timers, but timer0 has been used for WiFi and it's not advisable to use. Only timer1 is available.
11
+ The timer1's 23-bit counter terribly can count only up to 8,388,607. So the timer1 maximum interval is very short.
12
+ Using 256 prescaler, maximum timer1 interval is only 26.843542 seconds !!!
13
+
14
+ Now with these new 16 ISR-based timers, the maximum interval is practically unlimited (limited only by unsigned long miliseconds)
15
+ The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
16
+ Therefore, their executions are not blocked by bad-behaving functions / tasks.
17
+ This important feature is absolutely necessary for mission-critical tasks.
18
+
19
+ Based on SimpleTimer - A timer library for Arduino.
20
+ Author: mromani@ottotecnica.com
21
+ Copyright (c) 2010 OTTOTECNICA Italy
22
+
23
+ Based on BlynkTimer.h
24
+ Author: Volodymyr Shymanskyy
25
+
26
+ Version: 1.2.0
27
+
28
+ Version Modified By Date Comments
29
+ ------- ----------- ---------- -----------
30
+ 1.0.0 K Hoang 23/11/2019 Initial coding
31
+ 1.0.1 K Hoang 25/11/2019 New release fixing compiler error
32
+ 1.0.2 K.Hoang 26/11/2019 Permit up to 16 super-long-time, super-accurate ISR-based timers to avoid being blocked
33
+ 1.0.3 K.Hoang 17/05/2020 Restructure code. Fix example. Enhance README.
34
+ 1.1.0 K.Hoang 27/10/2020 Restore cpp code besides Impl.h code to use if Multiple-Definition linker error.
35
+ 1.1.1 K.Hoang 06/12/2020 Add Version String and Change_Interval example to show how to change TimerInterval
36
+ 1.2.0 K.Hoang 08/01/2021 Add better debug feature. Optimize code and examples to reduce RAM usage
28
37
*****************************************************************************************************************************/
29
38
30
39
/*
39
48
or the entire sequence of your code which accesses the data.
40
49
*/
41
50
42
- #if !( ARDUINO_ARCH_NRF52840 && TARGET_NAME == ARDUINO_NANO33BLE )
43
- #error This code is designed to run on nRF52-based Nano-33-BLE boards using mbed-RTOS platform ! Please check your Tools->Board setting.
51
+ #if !defined(ESP8266 )
52
+ #error This code is designed to run on ESP8266 and ESP8266-based boards ! Please check your Tools->Board setting.
44
53
#endif
45
54
46
- // These define's must be placed at the beginning before #include "NRF52TimerInterrupt.h"
47
- // For Nano33-BLE, don't use Serial.print() in ISR as system will definitely hang.
48
- #define NRF52_MBED_TIMER_INTERRUPT_DEBUG 0
55
+ // These define's must be placed at the beginning before #include "ESP8266TimerInterrupt.h"
56
+ // _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
57
+ // Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
58
+ #define TIMER_INTERRUPT_DEBUG 0
59
+ #define _TIMERINTERRUPT_LOGLEVEL_ 0
49
60
50
- #include " NRF52_MBED_TimerInterrupt .h"
61
+ #include " ESP8266TimerInterrupt .h"
51
62
52
- // #ifndef LED_BUILTIN
53
- // #define LED_BUILTIN D13
54
- // #endif
55
-
56
- #ifndef LED_BLUE_PIN
57
- #define LED_BLUE_PIN D7
58
- #endif
59
-
60
- #ifndef LED_RED_PIN
61
- #define LED_RED_PIN D8
63
+ #ifndef LED_BUILTIN
64
+ #define LED_BUILTIN D4 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED
62
65
#endif
63
66
64
- #define TIMER0_INTERVAL_MS 500 // 1000
65
- #define TIMER1_INTERVAL_MS 2000
66
67
67
- volatile uint32_t Timer0Count = 0 ;
68
- volatile uint32_t Timer1Count = 0 ;
68
+ #define TIMER_INTERVAL_MS 500 // 1000
69
69
70
- // Depending on the board, you can select NRF52 Hardware Timer from NRF_TIMER_1,NRF_TIMER_3,NRF_TIMER_4 (1,3 and 4)
71
- // If you select the already-used NRF_TIMER_0 or NRF_TIMER_2, it'll be auto modified to use NRF_TIMER_1
70
+ volatile uint32_t TimerCount = 0 ;
72
71
73
- // Init NRF52 timer NRF_TIMER1
74
- NRF52_MBED_Timer ITimer0 (NRF_TIMER_4);
75
-
76
- // Init NRF52 timer NRF_TIMER3
77
- NRF52_MBED_Timer ITimer1 (NRF_TIMER_3);
72
+ // Init ESP8266Timer
73
+ ESP8266Timer ITimer;
78
74
79
75
void printResult (uint32_t currTime)
80
76
{
81
- Serial.printf (" Time = %ld, Timer0Count = %lu, , Timer1Count = %lu\n " , currTime, Timer0Count, Timer1Count);
77
+ Serial.print (F (" Time = " )); Serial.print (currTime);
78
+ Serial.print (F (" , TimerCount = " )); Serial.println (TimerCount);
82
79
}
83
80
84
- void TimerHandler0 ( void )
81
+ void TimerHandler ( )
85
82
{
86
- static bool toggle0 = false ;
83
+ static bool toggle = false ;
87
84
88
- // Flag for checking to be sure ISR is working as SErial .print is not OK here in ISR
89
- Timer0Count ++;
85
+ // Flag for checking to be sure ISR is working as Serial .print is not OK here in ISR
86
+ TimerCount ++;
90
87
91
88
// timer interrupt toggles pin LED_BUILTIN
92
- digitalWrite (LED_BUILTIN, toggle0);
93
- toggle0 = !toggle0;
94
- }
95
-
96
- void TimerHandler1 (void )
97
- {
98
- static bool toggle1 = false ;
99
-
100
- // Flag for checking to be sure ISR is working as Serial.print is not OK here in ISR
101
- Timer1Count++;
102
-
103
- // timer interrupt toggles outputPin
104
- digitalWrite (LED_BLUE_PIN, toggle1);
105
- toggle1 = !toggle1;
89
+ digitalWrite (LED_BUILTIN, toggle);
90
+ toggle = !toggle;
106
91
}
107
92
108
93
void setup ()
109
94
{
110
95
pinMode (LED_BUILTIN, OUTPUT);
111
- pinMode (LED_BLUE_PIN, OUTPUT);
112
96
113
97
Serial.begin (115200 );
114
98
while (!Serial);
115
99
116
100
delay (100 );
117
101
118
- Serial.printf (" \n Starting Change_Interval on %s\n " , BOARD_NAME);
119
- Serial.printf (" Version : v%s\n " , NRF52_MBED_TIMER_INTERRUPT_VERSION);
102
+ Serial.print (F (" \n Starting Change_Interval on " )); Serial.println (ARDUINO_BOARD);
103
+ Serial.println (ESP8266_TIMER_INTERRUPT_VERSION);
104
+ Serial.print (F (" CPU Frequency = " )); Serial.print (F_CPU / 1000000 ); Serial.println (F (" MHz" ));
120
105
121
106
// Interval in microsecs
122
- if (ITimer0.attachInterruptInterval (TIMER0_INTERVAL_MS * 1000 , TimerHandler0))
123
- {
124
- Serial.printf (" Starting ITimer0 OK, millis() = %ld\n " , millis ());
125
- }
126
- else
127
- Serial.println (" Can't set ITimer0. Select another freq. or timer" );
128
-
129
- // Interval in microsecs
130
- if (ITimer1.attachInterruptInterval (TIMER1_INTERVAL_MS * 1000 , TimerHandler1))
107
+ if (ITimer.attachInterruptInterval (TIMER_INTERVAL_MS * 1000 , TimerHandler))
131
108
{
132
- Serial.printf ( " Starting ITimer1 OK, millis() = %ld \n " , millis ());
109
+ Serial.print ( F ( " Starting ITimer OK, millis() = " )); Serial. println ( millis ());
133
110
}
134
111
else
135
- Serial.println (" Can't set ITimer1 . Select another freq. or timer" );
112
+ Serial.println (F ( " Can't set ITimer . Select another freq. or timer" ) );
136
113
}
137
114
138
115
#define CHECK_INTERVAL_MS 10000L
@@ -157,10 +134,9 @@ void loop()
157
134
// setInterval(unsigned long interval, timerCallback callback)
158
135
multFactor = (multFactor + 1 ) % 2 ;
159
136
160
- ITimer0.setInterval (TIMER0_INTERVAL_MS * 1000 * (multFactor + 1 ), TimerHandler0);
161
- ITimer1.setInterval (TIMER1_INTERVAL_MS * 1000 * (multFactor + 1 ), TimerHandler1);
137
+ ITimer.setInterval (TIMER_INTERVAL_MS * 1000 * (multFactor + 1 ), TimerHandler);
162
138
163
- Serial.printf ( " Changing Interval, Timer0 = %lu, Timer1 = %lu \n " , TIMER0_INTERVAL_MS * (multFactor + 1 ), TIMER1_INTERVAL_MS * (multFactor + 1 ));
139
+ Serial.print ( F ( " Changing Interval, Timer = " )); Serial. println (TIMER_INTERVAL_MS * (multFactor + 1 ));
164
140
165
141
lastChangeTime = currTime;
166
142
}
0 commit comments