11/* ***************************************************************************************************************************
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- 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+ 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
2837*****************************************************************************************************************************/
2938
3039/*
3948 or the entire sequence of your code which accesses the data.
4049*/
4150
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.
4453#endif
4554
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
4960
50- #include " NRF52_MBED_TimerInterrupt .h"
61+ #include " ESP8266TimerInterrupt .h"
5162
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
6265#endif
6366
64- #define TIMER0_INTERVAL_MS 500 // 1000
65- #define TIMER1_INTERVAL_MS 2000
6667
67- volatile uint32_t Timer0Count = 0 ;
68- volatile uint32_t Timer1Count = 0 ;
68+ #define TIMER_INTERVAL_MS 500 // 1000
6969
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 ;
7271
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;
7874
7975void printResult (uint32_t currTime)
8076{
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);
8279}
8380
84- void TimerHandler0 ( void )
81+ void TimerHandler ( )
8582{
86- static bool toggle0 = false ;
83+ static bool toggle = false ;
8784
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 ++;
9087
9188 // 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;
10691}
10792
10893void setup ()
10994{
11095 pinMode (LED_BUILTIN, OUTPUT);
111- pinMode (LED_BLUE_PIN, OUTPUT);
11296
11397 Serial.begin (115200 );
11498 while (!Serial);
11599
116100 delay (100 );
117101
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" ));
120105
121106 // 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))
131108 {
132- Serial.printf ( " Starting ITimer1 OK, millis() = %ld \n " , millis ());
109+ Serial.print ( F ( " Starting ITimer OK, millis() = " )); Serial. println ( millis ());
133110 }
134111 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" ) );
136113}
137114
138115#define CHECK_INTERVAL_MS 10000L
@@ -157,10 +134,9 @@ void loop()
157134 // setInterval(unsigned long interval, timerCallback callback)
158135 multFactor = (multFactor + 1 ) % 2 ;
159136
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);
162138
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 ));
164140
165141 lastChangeTime = currTime;
166142 }
0 commit comments