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

Commit 93d5805

Browse files
authored
v1.10.0 to avoid conflict with Servo library
### Releases v1.10.0 1. Avoid conflict with `Servo library`. Check [Cannot use TimerInterrupt_Generic Library in the same time than Servo Library #11](khoih-prog/TimerInterrupt_Generic#11) 2. Prevent overflow of TCx by flagging error 3. Modify all examples 4. Update `Packages_Patches`
1 parent 3365572 commit 93d5805

28 files changed

+1078
-690
lines changed

CONTRIBUTING.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p
1515
Please ensure to specify the following:
1616

1717
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
18-
* `SAMD` Core Version (e.g. Arduino SAMD core v1.8.13, Adafruit SAMD core v1.7.10, Seeed Studio SAMD v1.8.2, Sparkfun SAMD v1.8.1)
18+
* `SAMD` Core Version (e.g. Arduino SAMD core v1.8.13, Adafruit SAMD core v1.7.10, Seeed Studio SAMD v1.8.3, Sparkfun SAMD v1.8.1)
1919
* Contextual information (e.g. what you were trying to achieve)
2020
* Simplest possible steps to reproduce
2121
* Anything that might be relevant in your opinion, such as:
@@ -29,10 +29,10 @@ Please ensure to specify the following:
2929
Arduino IDE version: 1.8.19
3030
Arduino SAMD Core Version 1.8.13
3131
OS: Ubuntu 20.04 LTS
32-
Linux xy-Inspiron-3593 5.13.0-40-generic #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
32+
Linux xy-Inspiron-3593 5.15.0-48-generic #54~20.04.1-Ubuntu SMP Thu Sep 1 16:17:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
3333
3434
Context:
35-
I encountered a crash while trying to use the Timer Interrupt.
35+
I encountered a crash while trying to use this library
3636
3737
Steps to reproduce:
3838
1. ...

README.md

+125-70
Large diffs are not rendered by default.

changelog.md

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
## Table of Contents
1313

1414
* [Changelog](#changelog)
15+
* [Releases v1.10.0](#releases-v1100)
1516
* [Releases v1.9.0](#releases-v190)
1617
* [Releases v1.8.0](#releases-v180)
1718
* [Releases v1.7.0](#releases-v170)
@@ -31,6 +32,13 @@
3132

3233
## Changelog
3334

35+
### Releases v1.10.0
36+
37+
1. Avoid conflict with Servo library. Check [Cannot use TimerInterrupt_Generic Library in the same time than Servo Library #11](https://github.com/khoih-prog/TimerInterrupt_Generic/discussions/11)
38+
2. Prevent overflow of TCx by flagging error
39+
3. Modify all examples
40+
4. Update `Packages_Patches`
41+
3442
### Releases v1.9.0
3543

3644
1. Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21

examples/Argument_None/Argument_None.ino

+49-101
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
For SAMD boards
44
Written by Khoi Hoang
55
6-
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_TimerInterrupt
6+
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
77
Licensed under MIT license
88
99
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
@@ -36,12 +36,27 @@
3636
#error This code is designed to run on SAMD21/SAMD51 platform! Please check your Tools->Board setting.
3737
#endif
3838

39+
/////////////////////////////////////////////////////////////////
40+
3941
// These define's must be placed at the beginning before #include "SAMDTimerInterrupt.h"
4042
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
4143
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
4244
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
4345
#define TIMER_INTERRUPT_DEBUG 0
44-
#define _TIMERINTERRUPT_LOGLEVEL_ 0
46+
#define _TIMERINTERRUPT_LOGLEVEL_ 1
47+
48+
// Select only one to be true for SAMD21. Must must be placed at the beginning before #include "SAMDTimerInterrupt.h"
49+
#define USING_TIMER_TC3 true // Only TC3 can be used for SAMD51
50+
#define USING_TIMER_TC4 false // Not to use with Servo library
51+
#define USING_TIMER_TC5 false
52+
#define USING_TIMER_TCC false
53+
#define USING_TIMER_TCC1 false
54+
#define USING_TIMER_TCC2 false // Don't use this, can crash on some boards
55+
56+
// Uncomment To test if conflict with Servo library
57+
//#include "Servo.h"
58+
59+
/////////////////////////////////////////////////////////////////
4560

4661
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
4762
#include "SAMDTimerInterrupt.h"
@@ -50,103 +65,47 @@
5065
#define LED_BUILTIN 13
5166
#endif
5267

53-
#ifndef LED_BLUE
54-
#define LED_BLUE 2
55-
#endif
56-
57-
#ifndef LED_RED
58-
#define LED_RED 8
59-
#endif
60-
61-
#define TIMER0_INTERVAL_MS 500 //1000
62-
63-
volatile uint32_t preMillisTimer0 = 0;
64-
65-
// Depending on the board, you can select SAMD21 Hardware Timer from TC3, TC4, TC5, TCC, TCC1 or TCC2
66-
// SAMD51 Hardware Timer only TC3
67-
68-
// Init SAMD timer TIMER_TC3
69-
SAMDTimer ITimer0(TIMER_TC3);
68+
volatile uint32_t preMillisTimer = 0;
7069

7170
//////////////////////////////////////////////
7271

73-
void TimerHandler0()
74-
{
75-
static bool toggle0 = false;
76-
static bool started = false;
77-
78-
if (!started)
79-
{
80-
started = true;
81-
pinMode(LED_BUILTIN, OUTPUT);
82-
}
83-
84-
#if (TIMER_INTERRUPT_DEBUG > 0)
85-
static uint32_t curMillis = 0;
86-
87-
curMillis = millis();
88-
89-
if (curMillis > TIMER0_INTERVAL_MS)
90-
{
91-
Serial.print(F("ITimer0: millis() = ")); Serial.print(curMillis);
92-
Serial.print(F(", delta = ")); Serial.println(curMillis - preMillisTimer0);
93-
}
94-
95-
preMillisTimer0 = curMillis;
96-
#endif
97-
98-
//timer interrupt toggles pin LED_BUILTIN
99-
digitalWrite(LED_BUILTIN, toggle0);
100-
toggle0 = !toggle0;
101-
}
102-
103-
//////////////////////////////////////////////
104-
105-
#if (TIMER_INTERRUPT_USING_SAMD21)
106-
107-
#define TIMER1_INTERVAL_MS 2000
72+
// TC3, TC4, TC5 max permissible TIMER_INTERVAL_MS is 1398.101 ms, larger will overflow, therefore not permitted
73+
// Use TCC, TCC1, TCC2 for longer TIMER_INTERVAL_MS
74+
#define TIMER_INTERVAL_MS 1000
10875

10976
volatile uint32_t preMillisTimer1 = 0;
11077

111-
// Init SAMD timer TIMER_TCC
112-
//SAMDTimer ITimer1(TIMER_TC4);
113-
//SAMDTimer ITimer1(TIMER_TC5);
114-
SAMDTimer ITimer1(TIMER_TCC);
115-
//SAMDTimer ITimer1(TIMER_TCC1);
116-
//SAMDTimer ITimer1(TIMER_TCC2);
78+
#if USING_TIMER_TC3
79+
#define SELECTED_TIMER TIMER_TC3
80+
#elif USING_TIMER_TC4
81+
#define SELECTED_TIMER TIMER_TC4
82+
#elif USING_TIMER_TC5
83+
#define SELECTED_TIMER TIMER_TC5
84+
#elif USING_TIMER_TCC
85+
#define SELECTED_TIMER TIMER_TCC
86+
#elif USING_TIMER_TCC1
87+
#define SELECTED_TIMER TIMER_TCC1
88+
#elif USING_TIMER_TCC2
89+
#define SELECTED_TIMER TIMER_TCC
90+
#else
91+
#error You have to select 1 Timer
92+
#endif
93+
94+
// Init selected SAMD timer
95+
SAMDTimer ITimer(SELECTED_TIMER);
11796

11897
//////////////////////////////////////////////
11998

120-
void TimerHandler1()
99+
void TimerHandler()
121100
{
122-
static bool toggle1 = false;
123-
static bool started = false;
124-
125-
if (!started)
126-
{
127-
started = true;
128-
pinMode(LED_BLUE, OUTPUT);
129-
}
101+
static bool toggle = false;
130102

131-
#if (TIMER_INTERRUPT_DEBUG > 0)
132-
static uint32_t curMillis = 0;
133-
134-
curMillis = millis();
135-
136-
if (curMillis > TIMER1_INTERVAL_MS)
137-
{
138-
Serial.print(F("ITimer1: millis() = ")); Serial.print(curMillis);
139-
Serial.print(F(", delta = ")); Serial.println(curMillis - preMillisTimer1);
140-
}
141-
142-
preMillisTimer0 = curMillis;
143-
#endif
103+
//Serial.println(toggle? "ON" : "OFF");
144104

145-
//timer interrupt toggles outputPin
146-
digitalWrite(LED_BLUE, toggle1);
147-
toggle1 = !toggle1;
105+
//timer interrupt toggles pin LED_BUILTIN
106+
digitalWrite(LED_BUILTIN, toggle);
107+
toggle = !toggle;
148108
}
149-
#endif
150109

151110
//////////////////////////////////////////////
152111

@@ -164,24 +123,13 @@ void setup()
164123
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
165124

166125
// Interval in millisecs
167-
if (ITimer0.attachInterruptInterval_MS(TIMER0_INTERVAL_MS, TimerHandler0))
126+
if (ITimer.attachInterruptInterval_MS(TIMER_INTERVAL_MS, TimerHandler))
168127
{
169-
preMillisTimer0 = millis();
170-
Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(preMillisTimer0);
128+
preMillisTimer = millis();
129+
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(preMillisTimer);
171130
}
172131
else
173-
Serial.println(F("Can't set ITimer0. Select another freq. or timer"));
174-
175-
#if (TIMER_INTERRUPT_USING_SAMD21)
176-
// Interval in millisecs
177-
if (ITimer1.attachInterruptInterval_MS(TIMER1_INTERVAL_MS, TimerHandler1))
178-
{
179-
preMillisTimer1 = millis();
180-
Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(preMillisTimer1);
181-
}
182-
else
183-
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
184-
#endif
132+
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
185133
}
186134

187135
//////////////////////////////////////////////

0 commit comments

Comments
 (0)