From 1d1ea844c88ad55553b3ce0d7ce9521ba5ea10ae Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Thu, 29 Sep 2022 19:45:46 -0400 Subject: [PATCH] v1.101 using `float` for interval ### Releases v1.10.1 1. Using `float` instead of `ulong` for interval 2. Prevent overflow of SAMD51 TCx by flagging error --- README.md | 15 ++++++++------- changelog.md | 8 +++++++- library.json | 2 +- library.properties | 2 +- src/SAMDTimerInterrupt.h | 3 ++- src/SAMDTimerInterrupt.hpp | 27 ++++++++++++++------------- src/SAMDTimerInterrupt_Impl.h | 22 +++++++++++++++++----- src/SAMD_ISR_Timer-Impl.h | 3 ++- src/SAMD_ISR_Timer.h | 3 ++- src/SAMD_ISR_Timer.hpp | 3 ++- 10 files changed, 56 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index abb1931..3f66619 100644 --- a/README.md +++ b/README.md @@ -617,7 +617,7 @@ While software timer, **programmed for 2s, is activated after 7.937s !!!**. Then ``` Starting ISR_Timer_Complex_WiFiNINA on SAMD_NANO_33_IOT -SAMDTimerInterrupt v1.10.0 +SAMDTimerInterrupt v1.10.1 CPU Frequency = 48 MHz [TISR] SAMDTimerInterrupt: F_CPU (MHz) = 48 , TIMER_HZ = 48 [TISR] TC_Timer::startTimer _Timer = 0x 42002c00 , TC3 = 0x 42002c00 @@ -682,7 +682,7 @@ The following is the sample terminal output when running example [**TimerInterru ``` Starting TimerInterruptTest on ITSYBITSY_M4 -SAMDTimerInterrupt v1.10.0 +SAMDTimerInterrupt v1.10.1 CPU Frequency = 48 MHz [TISR] SAMDTimerInterrupt: F_CPU (MHz) = 120 , TIMER_HZ = 48 [TISR] TC_Timer::startTimer _Timer = 0x 0x4101c000 , TC3 = 0x 0x4101c000 @@ -760,7 +760,7 @@ The following is the sample terminal output when running example [**Argument_Non ``` Starting Argument_None on SAMD_NANO_33_IOT -SAMDTimerInterrupt v1.10.0 +SAMDTimerInterrupt v1.10.1 CPU Frequency = 48 MHz [TISR] SAMDTimerInterrupt: F_CPU (MHz) = 48 , TIMER_HZ = 48 [TISR] TC_Timer::startTimer _Timer = 0x 42002c00 , TC3 = 0x 42002c00 @@ -810,7 +810,7 @@ In this example, 16 independent ISR Timers are used, yet utilized just one Hardw ``` Starting ISR_16_Timers_Array on SAMD_NANO_33_IOT -SAMDTimerInterrupt v1.10.0 +SAMDTimerInterrupt v1.10.1 CPU Frequency = 48 MHz [TISR] SAMDTimerInterrupt: F_CPU (MHz) = 48 , TIMER_HZ = 48 [TISR] TC_Timer::startTimer _Timer = 0x 42002c00 , TC3 = 0x 42002c00 @@ -934,7 +934,7 @@ The following is the sample terminal output when running example [Change_Interva ``` Starting Change_Interval on SAMD_NANO_33_IOT -SAMDTimerInterrupt v1.10.0 +SAMDTimerInterrupt v1.10.1 CPU Frequency = 48 MHz [TISR] SAMDTimerInterrupt: F_CPU (MHz) = 48 , TIMER_HZ = 48 [TISR] TC_Timer::startTimer _Timer = 0x 42002c00 , TC3 = 0x 42002c00 @@ -998,7 +998,7 @@ The following is the sample terminal output when running example [RepeatedAttach ``` Starting RepeatedAttachInterrupt_uS on SEEED_XIAO_M0 -SAMDTimerInterrupt v1.10.0 +SAMDTimerInterrupt v1.10.1 CPU Frequency = 48 MHz [TISR] _period = 19995 , frequency = 50.01 [TISR] SAMDTimerInterrupt: F_CPU (MHz) = 48 , TIMER_HZ = 48 @@ -1065,7 +1065,7 @@ The following is the sample terminal output when running example [SAMD21_MultiTi ``` Starting SAMD21_MultiTimers on SAMD_NANO_33_IOT -SAMDTimerInterrupt v1.10.0 +SAMDTimerInterrupt v1.10.1 CPU Frequency = 48 MHz [TISR] _period = 20000.00 , frequency = 50.00 [TISR] _timerNumber = 0 @@ -1181,6 +1181,7 @@ Submit issues to: [SAMD_TimerInterrupt issues](https://github.com/khoih-prog/SAM 13. Add functions `attachInterruptInterval_MS()` and `setInterval_MS()` 14. 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) 15. Prevent overflow of TCx by flagging error +16. Using float instead of ulong for interval --- --- diff --git a/changelog.md b/changelog.md index 43a5c2e..a5fb4e8 100644 --- a/changelog.md +++ b/changelog.md @@ -12,6 +12,7 @@ ## Table of Contents * [Changelog](#changelog) + * [Releases v1.10.1](#releases-v1101) * [Releases v1.10.0](#releases-v1100) * [Releases v1.9.0](#releases-v190) * [Releases v1.8.0](#releases-v180) @@ -32,10 +33,15 @@ ## Changelog +### Releases v1.10.1 + +1. Using float instead of ulong for interval +2. Prevent overflow of SAMD51 TCx by flagging error + ### 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 +2. Prevent overflow of SAMD21 TCx by flagging error 3. Modify all examples 4. Update `Packages_Patches` diff --git a/library.json b/library.json index aead604..0444ed4 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "SAMD_TimerInterrupt", - "version": "1.10.0", + "version": "1.10.1", "keywords": "timing, device, control, timer, interrupt, hardware, isr, isr-based, hardware-timer, isr-timer, isr-based-timer, mission-critical, accuracy, precise, non-blocking, samd, samd21, samd51, nano-33-iot", "description": "This library enables you to use Interrupt from Hardware Timers on SAMD-based boards. It now supports 16 ISR-based timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware timers, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy. SAMD21 now can use 6 Timers", "authors": diff --git a/library.properties b/library.properties index bd88edc..72119a6 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=SAMD_TimerInterrupt -version=1.10.0 +version=1.10.1 author=Khoi Hoang maintainer=Khoi Hoang sentence=This library enables you to use Interrupt from Hardware Timers on SAMD-based boards such as SAMD21 Nano-33-IoT, Adafruit SAMD51 Itsy-Bitsy M4, SeeedStudio XIAO, Sparkfun SAMD51_MICROMOD, etc. diff --git a/src/SAMDTimerInterrupt.h b/src/SAMDTimerInterrupt.h index c258911..e1983bb 100644 --- a/src/SAMDTimerInterrupt.h +++ b/src/SAMDTimerInterrupt.h @@ -16,7 +16,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.10.0 + Version: 1.10.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,7 @@ 1.8.0 K.Hoang 07/05/2022 Scrap the buggy code in v1.7.0 for TC3 1.9.0 K.Hoang 08/05/2022 Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21 1.10.0 K.Hoang 29/09/2022 Avoid conflict with Servo library. Modify all examples. Prevent overflow of TCx + 1.10.1 K.Hoang 30/09/2022 Using float instead of ulong for interval. Prevent overflow of SAMD51 TCx *****************************************************************************************************************************/ /* SAMD21 diff --git a/src/SAMDTimerInterrupt.hpp b/src/SAMDTimerInterrupt.hpp index 21d7698..e610bc3 100644 --- a/src/SAMDTimerInterrupt.hpp +++ b/src/SAMDTimerInterrupt.hpp @@ -15,8 +15,8 @@ Copyright (c) 2010 OTTOTECNICA Italy Based on BlynkTimer.h Author: Volodymyr Shymanskyy - - Version: 1.10.0 + + Version: 1.10.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,7 @@ 1.8.0 K.Hoang 07/05/2022 Scrap the buggy code in v1.7.0 for TC3 1.9.0 K.Hoang 08/05/2022 Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21 1.10.0 K.Hoang 29/09/2022 Avoid conflict with Servo library. Modify all examples. Prevent overflow of TCx + 1.10.1 K.Hoang 30/09/2022 Using float instead of ulong for interval. Prevent overflow of SAMD51 TCx *****************************************************************************************************************************/ /* SAMD21 @@ -155,13 +156,13 @@ #include "Arduino.h" #ifndef SAMD_TIMER_INTERRUPT_VERSION - #define SAMD_TIMER_INTERRUPT_VERSION "SAMDTimerInterrupt v1.10.0" + #define SAMD_TIMER_INTERRUPT_VERSION "SAMDTimerInterrupt v1.10.1" #define SAMD_TIMER_INTERRUPT_VERSION_MAJOR 1 #define SAMD_TIMER_INTERRUPT_VERSION_MINOR 10 - #define SAMD_TIMER_INTERRUPT_VERSION_PATCH 0 + #define SAMD_TIMER_INTERRUPT_VERSION_PATCH 1 - #define SAMD_TIMER_INTERRUPT_VERSION_INT 1010000 + #define SAMD_TIMER_INTERRUPT_VERSION_INT 1010001 #endif #include "TimerInterrupt_Generic_Debug.h" @@ -245,7 +246,7 @@ class SAMDTimerInterrupt // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool setInterval(const unsigned long& interval, timerCallback callback) + bool setInterval(const float& interval, timerCallback callback) { return setFrequency((float) (1000000.0f / interval), callback); } @@ -254,7 +255,7 @@ class SAMDTimerInterrupt // interval (in milliseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool setInterval_MS(const unsigned long& interval, timerCallback callback) + bool setInterval_MS(const float& interval, timerCallback callback) { return setFrequency((float) (1000.0f / interval), callback); } @@ -270,7 +271,7 @@ class SAMDTimerInterrupt // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool attachInterruptInterval(const unsigned long& interval, timerCallback callback) + bool attachInterruptInterval(const float& interval, timerCallback callback) { return setFrequency( (float) ( 1000000.0f / interval), callback); } @@ -279,7 +280,7 @@ class SAMDTimerInterrupt // interval (in milliseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool attachInterruptInterval_MS(const unsigned long& interval, timerCallback callback) + bool attachInterruptInterval_MS(const float& interval, timerCallback callback) { return setFrequency( (float) ( 1000.0f / interval), callback); } @@ -545,7 +546,7 @@ class SAMDTimerInterrupt // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool setInterval(const unsigned long& interval, timerCallback callback) + bool setInterval(const float& interval, timerCallback callback) { return setFrequency((float) (1000000.0f / interval), callback); } @@ -554,7 +555,7 @@ class SAMDTimerInterrupt // interval (in milliseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool setInterval_MS(const unsigned long& interval, timerCallback callback) + bool setInterval_MS(const float& interval, timerCallback callback) { return setFrequency((float) (1000.0f / interval), callback); } @@ -570,7 +571,7 @@ class SAMDTimerInterrupt // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool attachInterruptInterval(const unsigned long& interval, timerCallback callback) + bool attachInterruptInterval(const float& interval, timerCallback callback) { return setFrequency( (float) ( 1000000.0f / interval), callback); } @@ -579,7 +580,7 @@ class SAMDTimerInterrupt // interval (in milliseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely // No params and duration now. To be added in the future by adding similar functions here or to SAMD-hal-timer.c - bool attachInterruptInterval_MS(const unsigned long& interval, timerCallback callback) + bool attachInterruptInterval_MS(const float& interval, timerCallback callback) { return setFrequency( (float) ( 1000.0f / interval), callback); } diff --git a/src/SAMDTimerInterrupt_Impl.h b/src/SAMDTimerInterrupt_Impl.h index d958f01..0e029c1 100644 --- a/src/SAMDTimerInterrupt_Impl.h +++ b/src/SAMDTimerInterrupt_Impl.h @@ -15,8 +15,8 @@ Copyright (c) 2010 OTTOTECNICA Italy Based on BlynkTimer.h Author: Volodymyr Shymanskyy - - Version: 1.10.0 + + Version: 1.10.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -33,6 +33,7 @@ 1.8.0 K.Hoang 07/05/2022 Scrap the buggy code in v1.7.0 for TC3 1.9.0 K.Hoang 08/05/2022 Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21 1.10.0 K.Hoang 29/09/2022 Avoid conflict with Servo library. Modify all examples. Prevent overflow of TCx + 1.10.1 K.Hoang 30/09/2022 Using float instead of ulong for interval. Prevent overflow of SAMD51 TCx *****************************************************************************************************************************/ /* SAMD21 @@ -84,6 +85,16 @@ { TISR_LOGWARN3(F("SAMDTimerInterrupt: F_CPU (MHz) ="), F_CPU/1000000, F(", TIMER_HZ ="), TIMER_HZ/1000000); TISR_LOGWARN3(F("TC_Timer::startTimer _Timer = 0x"), String((uint32_t) _SAMDTimer, HEX), F(", TC3 = 0x"), String((uint32_t) TC3, HEX)); + + // maxPermittedPeriod = 1,398,101.33us for 48MHz timer clock + float maxPermittedPeriod = (65536.0f * 1024) / (TIMER_HZ / 1000000.0f ); + + if (_period > maxPermittedPeriod ) + { + TISR_LOGERROR3(F("Max permissible _period (us) ="), maxPermittedPeriod, F(", current _period (us) ="), _period); + + return false; + } // Enable the TC bus clock, use clock generator 0 GCLK->PCHCTRL[TC3_GCLK_ID].reg = GCLK_PCHCTRL_GEN_GCLK1_Val | (1 << GCLK_PCHCTRL_CHEN_Pos); @@ -108,7 +119,7 @@ TC3_callback = callback; setPeriod_TIMER_TC3(_period); - + return true; } else @@ -273,12 +284,13 @@ { TISR_LOGDEBUG1(F("_timerNumber ="), _timerNumber); - // maxPermittedPeriod = 1,398,101.33us for 48MHz clock - float maxPermittedPeriod = (65536.0f * 1024) / (F_CPU / 1000000.0f ); + // maxPermittedPeriod = 1,398,101.33us for 48MHz timer clock + float maxPermittedPeriod = (65536.0f * 1024) / (TIMER_HZ / 1000000.0f ); if (_period > maxPermittedPeriod ) { TISR_LOGERROR3(F("Max permissible _period (us) ="), maxPermittedPeriod, F(", current _period (us) ="), _period); + return false; } diff --git a/src/SAMD_ISR_Timer-Impl.h b/src/SAMD_ISR_Timer-Impl.h index 2896724..21f2c74 100644 --- a/src/SAMD_ISR_Timer-Impl.h +++ b/src/SAMD_ISR_Timer-Impl.h @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.10.0 + Version: 1.10.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -36,6 +36,7 @@ 1.8.0 K.Hoang 07/05/2022 Scrap the buggy code in v1.7.0 for TC3 1.9.0 K.Hoang 08/05/2022 Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21 1.10.0 K.Hoang 29/09/2022 Avoid conflict with Servo library. Modify all examples. Prevent overflow of TCx + 1.10.1 K.Hoang 30/09/2022 Using float instead of ulong for interval. Prevent overflow of SAMD51 TCx *****************************************************************************************************************************/ #pragma once diff --git a/src/SAMD_ISR_Timer.h b/src/SAMD_ISR_Timer.h index 46903b4..7723740 100644 --- a/src/SAMD_ISR_Timer.h +++ b/src/SAMD_ISR_Timer.h @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.10.0 + Version: 1.10.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -36,6 +36,7 @@ 1.8.0 K.Hoang 07/05/2022 Scrap the buggy code in v1.7.0 for TC3 1.9.0 K.Hoang 08/05/2022 Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21 1.10.0 K.Hoang 29/09/2022 Avoid conflict with Servo library. Modify all examples. Prevent overflow of TCx + 1.10.1 K.Hoang 30/09/2022 Using float instead of ulong for interval. Prevent overflow of SAMD51 TCx *****************************************************************************************************************************/ #pragma once diff --git a/src/SAMD_ISR_Timer.hpp b/src/SAMD_ISR_Timer.hpp index 3163999..24cb6aa 100644 --- a/src/SAMD_ISR_Timer.hpp +++ b/src/SAMD_ISR_Timer.hpp @@ -19,7 +19,7 @@ Based on BlynkTimer.h Author: Volodymyr Shymanskyy - Version: 1.10.0 + Version: 1.10.1 Version Modified By Date Comments ------- ----------- ---------- ----------- @@ -36,6 +36,7 @@ 1.8.0 K.Hoang 07/05/2022 Scrap the buggy code in v1.7.0 for TC3 1.9.0 K.Hoang 08/05/2022 Add TC4, TC5, TCC1 and TCC2 Timers to SAMD21 1.10.0 K.Hoang 29/09/2022 Avoid conflict with Servo library. Modify all examples. Prevent overflow of TCx + 1.10.1 K.Hoang 30/09/2022 Using float instead of ulong for interval. Prevent overflow of SAMD51 TCx *****************************************************************************************************************************/ #pragma once