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

Commit

Permalink
v1.10.0 to avoid conflict with Servo library
Browse files Browse the repository at this point in the history
### 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`
  • Loading branch information
khoih-prog authored Sep 29, 2022
1 parent 3365572 commit 93d5805
Show file tree
Hide file tree
Showing 28 changed files with 1,078 additions and 690 deletions.
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p
Please ensure to specify the following:

* Arduino IDE version (e.g. 1.8.19) or Platform.io version
* `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)
* `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)
* Contextual information (e.g. what you were trying to achieve)
* Simplest possible steps to reproduce
* Anything that might be relevant in your opinion, such as:
Expand All @@ -29,10 +29,10 @@ Please ensure to specify the following:
Arduino IDE version: 1.8.19
Arduino SAMD Core Version 1.8.13
OS: Ubuntu 20.04 LTS
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
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
Context:
I encountered a crash while trying to use the Timer Interrupt.
I encountered a crash while trying to use this library
Steps to reproduce:
1. ...
Expand Down
195 changes: 125 additions & 70 deletions README.md

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
## Table of Contents

* [Changelog](#changelog)
* [Releases v1.10.0](#releases-v1100)
* [Releases v1.9.0](#releases-v190)
* [Releases v1.8.0](#releases-v180)
* [Releases v1.7.0](#releases-v170)
Expand All @@ -31,6 +32,13 @@

## Changelog

### Releases v1.10.0

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)
2. Prevent overflow of TCx by flagging error
3. Modify all examples
4. Update `Packages_Patches`

### Releases v1.9.0

1. Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21
Expand Down
150 changes: 49 additions & 101 deletions examples/Argument_None/Argument_None.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
For SAMD boards
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/SAMD_TimerInterrupt
Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic
Licensed under MIT license
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
Expand Down Expand Up @@ -36,12 +36,27 @@
#error This code is designed to run on SAMD21/SAMD51 platform! Please check your Tools->Board setting.
#endif

/////////////////////////////////////////////////////////////////

// These define's must be placed at the beginning before #include "SAMDTimerInterrupt.h"
// _TIMERINTERRUPT_LOGLEVEL_ from 0 to 4
// Don't define _TIMERINTERRUPT_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
// Don't define TIMER_INTERRUPT_DEBUG > 2. Only for special ISR debugging only. Can hang the system.
#define TIMER_INTERRUPT_DEBUG 0
#define _TIMERINTERRUPT_LOGLEVEL_ 0
#define _TIMERINTERRUPT_LOGLEVEL_ 1

// Select only one to be true for SAMD21. Must must be placed at the beginning before #include "SAMDTimerInterrupt.h"
#define USING_TIMER_TC3 true // Only TC3 can be used for SAMD51
#define USING_TIMER_TC4 false // Not to use with Servo library
#define USING_TIMER_TC5 false
#define USING_TIMER_TCC false
#define USING_TIMER_TCC1 false
#define USING_TIMER_TCC2 false // Don't use this, can crash on some boards

// Uncomment To test if conflict with Servo library
//#include "Servo.h"

/////////////////////////////////////////////////////////////////

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

#ifndef LED_BLUE
#define LED_BLUE 2
#endif

#ifndef LED_RED
#define LED_RED 8
#endif

#define TIMER0_INTERVAL_MS 500 //1000

volatile uint32_t preMillisTimer0 = 0;

// Depending on the board, you can select SAMD21 Hardware Timer from TC3, TC4, TC5, TCC, TCC1 or TCC2
// SAMD51 Hardware Timer only TC3

// Init SAMD timer TIMER_TC3
SAMDTimer ITimer0(TIMER_TC3);
volatile uint32_t preMillisTimer = 0;

//////////////////////////////////////////////

void TimerHandler0()
{
static bool toggle0 = false;
static bool started = false;

if (!started)
{
started = true;
pinMode(LED_BUILTIN, OUTPUT);
}

#if (TIMER_INTERRUPT_DEBUG > 0)
static uint32_t curMillis = 0;

curMillis = millis();

if (curMillis > TIMER0_INTERVAL_MS)
{
Serial.print(F("ITimer0: millis() = ")); Serial.print(curMillis);
Serial.print(F(", delta = ")); Serial.println(curMillis - preMillisTimer0);
}

preMillisTimer0 = curMillis;
#endif

//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle0);
toggle0 = !toggle0;
}

//////////////////////////////////////////////

#if (TIMER_INTERRUPT_USING_SAMD21)

#define TIMER1_INTERVAL_MS 2000
// TC3, TC4, TC5 max permissible TIMER_INTERVAL_MS is 1398.101 ms, larger will overflow, therefore not permitted
// Use TCC, TCC1, TCC2 for longer TIMER_INTERVAL_MS
#define TIMER_INTERVAL_MS 1000

volatile uint32_t preMillisTimer1 = 0;

// Init SAMD timer TIMER_TCC
//SAMDTimer ITimer1(TIMER_TC4);
//SAMDTimer ITimer1(TIMER_TC5);
SAMDTimer ITimer1(TIMER_TCC);
//SAMDTimer ITimer1(TIMER_TCC1);
//SAMDTimer ITimer1(TIMER_TCC2);
#if USING_TIMER_TC3
#define SELECTED_TIMER TIMER_TC3
#elif USING_TIMER_TC4
#define SELECTED_TIMER TIMER_TC4
#elif USING_TIMER_TC5
#define SELECTED_TIMER TIMER_TC5
#elif USING_TIMER_TCC
#define SELECTED_TIMER TIMER_TCC
#elif USING_TIMER_TCC1
#define SELECTED_TIMER TIMER_TCC1
#elif USING_TIMER_TCC2
#define SELECTED_TIMER TIMER_TCC
#else
#error You have to select 1 Timer
#endif

// Init selected SAMD timer
SAMDTimer ITimer(SELECTED_TIMER);

//////////////////////////////////////////////

void TimerHandler1()
void TimerHandler()
{
static bool toggle1 = false;
static bool started = false;

if (!started)
{
started = true;
pinMode(LED_BLUE, OUTPUT);
}
static bool toggle = false;

#if (TIMER_INTERRUPT_DEBUG > 0)
static uint32_t curMillis = 0;

curMillis = millis();

if (curMillis > TIMER1_INTERVAL_MS)
{
Serial.print(F("ITimer1: millis() = ")); Serial.print(curMillis);
Serial.print(F(", delta = ")); Serial.println(curMillis - preMillisTimer1);
}

preMillisTimer0 = curMillis;
#endif
//Serial.println(toggle? "ON" : "OFF");

//timer interrupt toggles outputPin
digitalWrite(LED_BLUE, toggle1);
toggle1 = !toggle1;
//timer interrupt toggles pin LED_BUILTIN
digitalWrite(LED_BUILTIN, toggle);
toggle = !toggle;
}
#endif

//////////////////////////////////////////////

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

// Interval in millisecs
if (ITimer0.attachInterruptInterval_MS(TIMER0_INTERVAL_MS, TimerHandler0))
if (ITimer.attachInterruptInterval_MS(TIMER_INTERVAL_MS, TimerHandler))
{
preMillisTimer0 = millis();
Serial.print(F("Starting ITimer0 OK, millis() = ")); Serial.println(preMillisTimer0);
preMillisTimer = millis();
Serial.print(F("Starting ITimer OK, millis() = ")); Serial.println(preMillisTimer);
}
else
Serial.println(F("Can't set ITimer0. Select another freq. or timer"));

#if (TIMER_INTERRUPT_USING_SAMD21)
// Interval in millisecs
if (ITimer1.attachInterruptInterval_MS(TIMER1_INTERVAL_MS, TimerHandler1))
{
preMillisTimer1 = millis();
Serial.print(F("Starting ITimer1 OK, millis() = ")); Serial.println(preMillisTimer1);
}
else
Serial.println(F("Can't set ITimer1. Select another freq. or timer"));
#endif
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
}

//////////////////////////////////////////////
Expand Down
Loading

0 comments on commit 93d5805

Please sign in to comment.