From 5f56fa4deab3d831b69556c1afc2aed29e67f658 Mon Sep 17 00:00:00 2001 From: Khoi Hoang <57012152+khoih-prog@users.noreply.github.com> Date: Thu, 12 Nov 2020 23:32:16 -0500 Subject: [PATCH] v1.2.0 ### Releases v1.2.0 1. Add STM32_TimerInterrupt Library --- src/ESP32TimerInterrupt.h | 3 +- src/ESP8266TimerInterrupt.h | 3 +- src/ISR_Timer-Impl_Generic.h | 3 +- src/ISR_Timer_Generic.h | 3 +- src/NRF52TimerInterrupt.h | 3 +- src/SAMDTimerInterrupt.h | 3 +- src/SAMDUETimerInterrupt.h | 3 +- src/STM32TimerInterrupt.h | 156 +++++++++++++++++++++++++++++++ src/TeensyTimerInterrupt.h | 3 +- src/TimerInterrupt.h | 3 +- src/TimerInterrupt_Generic.h | 11 ++- src_cpp/ESP32TimerInterrupt.h | 3 +- src_cpp/ESP8266TimerInterrupt.h | 3 +- src_cpp/ISR_Timer_Generic.cpp | 3 +- src_cpp/ISR_Timer_Generic.h | 3 +- src_cpp/NRF52TimerInterrupt.h | 3 +- src_cpp/SAMDTimerInterrupt.h | 3 +- src_cpp/SAMDUETimerInterrupt.h | 3 +- src_cpp/STM32TimerInterrupt.h | 156 +++++++++++++++++++++++++++++++ src_cpp/TeensyTimerInterrupt.h | 3 +- src_cpp/TimerInterrupt.h | 3 +- src_cpp/TimerInterrupt_Generic.h | 11 ++- src_h/ESP32TimerInterrupt.h | 3 +- src_h/ESP8266TimerInterrupt.h | 3 +- src_h/ISR_Timer-Impl_Generic.h | 3 +- src_h/ISR_Timer_Generic.h | 3 +- src_h/NRF52TimerInterrupt.h | 3 +- src_h/SAMDTimerInterrupt.h | 3 +- src_h/SAMDUETimerInterrupt.h | 3 +- src_h/STM32TimerInterrupt.h | 156 +++++++++++++++++++++++++++++++ src_h/TeensyTimerInterrupt.h | 3 +- src_h/TimerInterrupt.h | 3 +- src_h/TimerInterrupt_Generic.h | 11 ++- 33 files changed, 549 insertions(+), 33 deletions(-) create mode 100644 src/STM32TimerInterrupt.h create mode 100644 src_cpp/STM32TimerInterrupt.h create mode 100644 src_h/STM32TimerInterrupt.h diff --git a/src/ESP32TimerInterrupt.h b/src/ESP32TimerInterrupt.h index e131a2c0..2704d559 100644 --- a/src/ESP32TimerInterrupt.h +++ b/src/ESP32TimerInterrupt.h @@ -25,11 +25,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src/ESP8266TimerInterrupt.h b/src/ESP8266TimerInterrupt.h index 5459e5b9..f23f675c 100644 --- a/src/ESP8266TimerInterrupt.h +++ b/src/ESP8266TimerInterrupt.h @@ -24,11 +24,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src/ISR_Timer-Impl_Generic.h b/src/ISR_Timer-Impl_Generic.h index 82209b42..e0172cc3 100644 --- a/src/ISR_Timer-Impl_Generic.h +++ b/src/ISR_Timer-Impl_Generic.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src/ISR_Timer_Generic.h b/src/ISR_Timer_Generic.h index 65c9fa01..beb1d767 100644 --- a/src/ISR_Timer_Generic.h +++ b/src/ISR_Timer_Generic.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src/NRF52TimerInterrupt.h b/src/NRF52TimerInterrupt.h index ed7c87ff..d66e07eb 100644 --- a/src/NRF52TimerInterrupt.h +++ b/src/NRF52TimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ /* nRF52 has 5 Hardware TIMERs: NRF_TIMER0-NRF_TIMER4 diff --git a/src/SAMDTimerInterrupt.h b/src/SAMDTimerInterrupt.h index 1ca1e0c9..3b7bb4b5 100644 --- a/src/SAMDTimerInterrupt.h +++ b/src/SAMDTimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ /* SAMD21 diff --git a/src/SAMDUETimerInterrupt.h b/src/SAMDUETimerInterrupt.h index 4f385202..7e00891f 100644 --- a/src/SAMDUETimerInterrupt.h +++ b/src/SAMDUETimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src/STM32TimerInterrupt.h b/src/STM32TimerInterrupt.h new file mode 100644 index 00000000..f6e217cd --- /dev/null +++ b/src/STM32TimerInterrupt.h @@ -0,0 +1,156 @@ +/**************************************************************************************************************************** + STM32TimerInterrupt.h + For STM32 boards + Written by Khoi Hoang + + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by + unsigned long miliseconds), you just consume only one Hardware timer and avoid conflicting with other cores' tasks. + The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers + Therefore, their executions are not blocked by bad-behaving functions / tasks. + This important feature is absolutely necessary for mission-critical tasks. + + Based on SimpleTimer - A timer library for Arduino. + Author: mromani@ottotecnica.com + Copyright (c) 2010 OTTOTECNICA Italy + + Based on BlynkTimer.h + Author: Volodymyr Shymanskyy + + Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic + Licensed under MIT license + + Version: 1.2.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library +*****************************************************************************************************************************/ + +#pragma once + +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#endif + +#ifndef TIMER_INTERRUPT_DEBUG + #define TIMER_INTERRUPT_DEBUG 0 +#endif + +class STM32TimerInterrupt; + +typedef STM32TimerInterrupt STM32Timer; + +typedef void (*timerCallback) (void); + + +class STM32TimerInterrupt +{ + private: + TIM_TypeDef* _timer; + HardwareTimer* _hwTimer = NULL; + + timerCallback _callback; // pointer to the callback function + float _frequency; // Timer frequency + uint32_t _timerCount; // count to activate timer + + public: + + STM32TimerInterrupt(TIM_TypeDef* timer) + { + _timer = timer; + + _hwTimer = new HardwareTimer(_timer); + + _callback = NULL; + }; + + ~STM32TimerInterrupt() + { + if (_hwTimer) + delete _hwTimer; + } + + // frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool setFrequency(float frequency, timerCallback callback) + { + // select timer frequency is 1MHz for better accuracy. We don't use 16-bit prescaler for now. + // Will use later if very low frequency is needed. + _frequency = 1000000; + _timerCount = (uint32_t) _frequency / frequency; + +#if (TIMER_INTERRUPT_DEBUG > 0) + Serial.println("STM32TimerInterrupt: Timer Input Freq (Hz) = " + String(_hwTimer->getTimerClkFreq()) + ", _fre = " + String(_frequency) + + ", _count = " + String((uint32_t) (_timerCount))); +#endif + + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->setOverflow(_timerCount, MICROSEC_FORMAT); + + _hwTimer->attachInterrupt(callback); + _hwTimer->resume(); + + return true; + } + + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool setInterval(unsigned long interval, timerCallback callback) + { + return setFrequency((float) (1000000.0f / interval), callback); + } + + bool attachInterrupt(float frequency, timerCallback callback) + { + return setFrequency(frequency, callback); + } + + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool attachInterruptInterval(unsigned long interval, timerCallback callback) + { + return setFrequency( (float) ( 1000000.0f / interval), callback); + } + + void detachInterrupt() + { + _hwTimer->detachInterrupt(); + } + + void disableTimer(void) + { + //_hwTimer->detachInterrupt(); + _hwTimer->pause(); + } + + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely + void reattachInterrupt() + { + setFrequency(_frequency, _callback); + } + + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely + void enableTimer(void) + { + //setFrequency(_frequency, _callback); + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->resume(); + } + + // Just stop clock source, clear the count + void stopTimer(void) + { + _hwTimer->pause(); + _hwTimer->setCount(0, MICROSEC_FORMAT); + } + + // Just reconnect clock source, start current count from 0 + void restartTimer(void) + { + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->resume(); + } +}; // class STM32TimerInterrupt diff --git a/src/TeensyTimerInterrupt.h b/src/TeensyTimerInterrupt.h index d0b21b26..d0c54ec4 100644 --- a/src/TeensyTimerInterrupt.h +++ b/src/TeensyTimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src/TimerInterrupt.h b/src/TimerInterrupt.h index a1ddd446..445d6395 100644 --- a/src/TimerInterrupt.h +++ b/src/TimerInterrupt.h @@ -26,11 +26,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ diff --git a/src/TimerInterrupt_Generic.h b/src/TimerInterrupt_Generic.h index 0dcd368a..695e8847 100644 --- a/src/TimerInterrupt_Generic.h +++ b/src/TimerInterrupt_Generic.h @@ -19,16 +19,17 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once -#define TIMER_INTERRUPT_GENERIC_VERSION "1.1.0" +#define TIMER_INTERRUPT_GENERIC_VERSION "1.2.0" #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) @@ -74,6 +75,12 @@ #define TIMER_INTERRUPT_USING_TEENSY true #include "TeensyTimerInterrupt.h" +#elif ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + + #define TIMER_INTERRUPT_USING_STM32 true + #include "STM32TimerInterrupt.h" #else #error Unsupported Board! Please check your Tools->Board setting. diff --git a/src_cpp/ESP32TimerInterrupt.h b/src_cpp/ESP32TimerInterrupt.h index e131a2c0..2704d559 100644 --- a/src_cpp/ESP32TimerInterrupt.h +++ b/src_cpp/ESP32TimerInterrupt.h @@ -25,11 +25,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_cpp/ESP8266TimerInterrupt.h b/src_cpp/ESP8266TimerInterrupt.h index 5459e5b9..f23f675c 100644 --- a/src_cpp/ESP8266TimerInterrupt.h +++ b/src_cpp/ESP8266TimerInterrupt.h @@ -24,11 +24,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_cpp/ISR_Timer_Generic.cpp b/src_cpp/ISR_Timer_Generic.cpp index 4cd5b539..c3587532 100644 --- a/src_cpp/ISR_Timer_Generic.cpp +++ b/src_cpp/ISR_Timer_Generic.cpp @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #include "ISR_Timer_Generic.h" diff --git a/src_cpp/ISR_Timer_Generic.h b/src_cpp/ISR_Timer_Generic.h index 9294febf..0e900709 100644 --- a/src_cpp/ISR_Timer_Generic.h +++ b/src_cpp/ISR_Timer_Generic.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_cpp/NRF52TimerInterrupt.h b/src_cpp/NRF52TimerInterrupt.h index ed7c87ff..d66e07eb 100644 --- a/src_cpp/NRF52TimerInterrupt.h +++ b/src_cpp/NRF52TimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ /* nRF52 has 5 Hardware TIMERs: NRF_TIMER0-NRF_TIMER4 diff --git a/src_cpp/SAMDTimerInterrupt.h b/src_cpp/SAMDTimerInterrupt.h index 1ca1e0c9..3b7bb4b5 100644 --- a/src_cpp/SAMDTimerInterrupt.h +++ b/src_cpp/SAMDTimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ /* SAMD21 diff --git a/src_cpp/SAMDUETimerInterrupt.h b/src_cpp/SAMDUETimerInterrupt.h index 4f385202..7e00891f 100644 --- a/src_cpp/SAMDUETimerInterrupt.h +++ b/src_cpp/SAMDUETimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_cpp/STM32TimerInterrupt.h b/src_cpp/STM32TimerInterrupt.h new file mode 100644 index 00000000..f6e217cd --- /dev/null +++ b/src_cpp/STM32TimerInterrupt.h @@ -0,0 +1,156 @@ +/**************************************************************************************************************************** + STM32TimerInterrupt.h + For STM32 boards + Written by Khoi Hoang + + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by + unsigned long miliseconds), you just consume only one Hardware timer and avoid conflicting with other cores' tasks. + The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers + Therefore, their executions are not blocked by bad-behaving functions / tasks. + This important feature is absolutely necessary for mission-critical tasks. + + Based on SimpleTimer - A timer library for Arduino. + Author: mromani@ottotecnica.com + Copyright (c) 2010 OTTOTECNICA Italy + + Based on BlynkTimer.h + Author: Volodymyr Shymanskyy + + Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic + Licensed under MIT license + + Version: 1.2.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library +*****************************************************************************************************************************/ + +#pragma once + +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#endif + +#ifndef TIMER_INTERRUPT_DEBUG + #define TIMER_INTERRUPT_DEBUG 0 +#endif + +class STM32TimerInterrupt; + +typedef STM32TimerInterrupt STM32Timer; + +typedef void (*timerCallback) (void); + + +class STM32TimerInterrupt +{ + private: + TIM_TypeDef* _timer; + HardwareTimer* _hwTimer = NULL; + + timerCallback _callback; // pointer to the callback function + float _frequency; // Timer frequency + uint32_t _timerCount; // count to activate timer + + public: + + STM32TimerInterrupt(TIM_TypeDef* timer) + { + _timer = timer; + + _hwTimer = new HardwareTimer(_timer); + + _callback = NULL; + }; + + ~STM32TimerInterrupt() + { + if (_hwTimer) + delete _hwTimer; + } + + // frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool setFrequency(float frequency, timerCallback callback) + { + // select timer frequency is 1MHz for better accuracy. We don't use 16-bit prescaler for now. + // Will use later if very low frequency is needed. + _frequency = 1000000; + _timerCount = (uint32_t) _frequency / frequency; + +#if (TIMER_INTERRUPT_DEBUG > 0) + Serial.println("STM32TimerInterrupt: Timer Input Freq (Hz) = " + String(_hwTimer->getTimerClkFreq()) + ", _fre = " + String(_frequency) + + ", _count = " + String((uint32_t) (_timerCount))); +#endif + + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->setOverflow(_timerCount, MICROSEC_FORMAT); + + _hwTimer->attachInterrupt(callback); + _hwTimer->resume(); + + return true; + } + + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool setInterval(unsigned long interval, timerCallback callback) + { + return setFrequency((float) (1000000.0f / interval), callback); + } + + bool attachInterrupt(float frequency, timerCallback callback) + { + return setFrequency(frequency, callback); + } + + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool attachInterruptInterval(unsigned long interval, timerCallback callback) + { + return setFrequency( (float) ( 1000000.0f / interval), callback); + } + + void detachInterrupt() + { + _hwTimer->detachInterrupt(); + } + + void disableTimer(void) + { + //_hwTimer->detachInterrupt(); + _hwTimer->pause(); + } + + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely + void reattachInterrupt() + { + setFrequency(_frequency, _callback); + } + + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely + void enableTimer(void) + { + //setFrequency(_frequency, _callback); + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->resume(); + } + + // Just stop clock source, clear the count + void stopTimer(void) + { + _hwTimer->pause(); + _hwTimer->setCount(0, MICROSEC_FORMAT); + } + + // Just reconnect clock source, start current count from 0 + void restartTimer(void) + { + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->resume(); + } +}; // class STM32TimerInterrupt diff --git a/src_cpp/TeensyTimerInterrupt.h b/src_cpp/TeensyTimerInterrupt.h index d0b21b26..d0c54ec4 100644 --- a/src_cpp/TeensyTimerInterrupt.h +++ b/src_cpp/TeensyTimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_cpp/TimerInterrupt.h b/src_cpp/TimerInterrupt.h index a1ddd446..445d6395 100644 --- a/src_cpp/TimerInterrupt.h +++ b/src_cpp/TimerInterrupt.h @@ -26,11 +26,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ diff --git a/src_cpp/TimerInterrupt_Generic.h b/src_cpp/TimerInterrupt_Generic.h index 0dcd368a..695e8847 100644 --- a/src_cpp/TimerInterrupt_Generic.h +++ b/src_cpp/TimerInterrupt_Generic.h @@ -19,16 +19,17 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once -#define TIMER_INTERRUPT_GENERIC_VERSION "1.1.0" +#define TIMER_INTERRUPT_GENERIC_VERSION "1.2.0" #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) @@ -74,6 +75,12 @@ #define TIMER_INTERRUPT_USING_TEENSY true #include "TeensyTimerInterrupt.h" +#elif ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + + #define TIMER_INTERRUPT_USING_STM32 true + #include "STM32TimerInterrupt.h" #else #error Unsupported Board! Please check your Tools->Board setting. diff --git a/src_h/ESP32TimerInterrupt.h b/src_h/ESP32TimerInterrupt.h index e131a2c0..2704d559 100644 --- a/src_h/ESP32TimerInterrupt.h +++ b/src_h/ESP32TimerInterrupt.h @@ -25,11 +25,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_h/ESP8266TimerInterrupt.h b/src_h/ESP8266TimerInterrupt.h index 5459e5b9..f23f675c 100644 --- a/src_h/ESP8266TimerInterrupt.h +++ b/src_h/ESP8266TimerInterrupt.h @@ -24,11 +24,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_h/ISR_Timer-Impl_Generic.h b/src_h/ISR_Timer-Impl_Generic.h index 82209b42..e0172cc3 100644 --- a/src_h/ISR_Timer-Impl_Generic.h +++ b/src_h/ISR_Timer-Impl_Generic.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_h/ISR_Timer_Generic.h b/src_h/ISR_Timer_Generic.h index 65c9fa01..beb1d767 100644 --- a/src_h/ISR_Timer_Generic.h +++ b/src_h/ISR_Timer_Generic.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_h/NRF52TimerInterrupt.h b/src_h/NRF52TimerInterrupt.h index ed7c87ff..d66e07eb 100644 --- a/src_h/NRF52TimerInterrupt.h +++ b/src_h/NRF52TimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ /* nRF52 has 5 Hardware TIMERs: NRF_TIMER0-NRF_TIMER4 diff --git a/src_h/SAMDTimerInterrupt.h b/src_h/SAMDTimerInterrupt.h index 1ca1e0c9..3b7bb4b5 100644 --- a/src_h/SAMDTimerInterrupt.h +++ b/src_h/SAMDTimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ /* SAMD21 diff --git a/src_h/SAMDUETimerInterrupt.h b/src_h/SAMDUETimerInterrupt.h index 4f385202..7e00891f 100644 --- a/src_h/SAMDUETimerInterrupt.h +++ b/src_h/SAMDUETimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_h/STM32TimerInterrupt.h b/src_h/STM32TimerInterrupt.h new file mode 100644 index 00000000..f6e217cd --- /dev/null +++ b/src_h/STM32TimerInterrupt.h @@ -0,0 +1,156 @@ +/**************************************************************************************************************************** + STM32TimerInterrupt.h + For STM32 boards + Written by Khoi Hoang + + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by + unsigned long miliseconds), you just consume only one Hardware timer and avoid conflicting with other cores' tasks. + The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers + Therefore, their executions are not blocked by bad-behaving functions / tasks. + This important feature is absolutely necessary for mission-critical tasks. + + Based on SimpleTimer - A timer library for Arduino. + Author: mromani@ottotecnica.com + Copyright (c) 2010 OTTOTECNICA Italy + + Based on BlynkTimer.h + Author: Volodymyr Shymanskyy + + Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic + Licensed under MIT license + + Version: 1.2.0 + + Version Modified By Date Comments + ------- ----------- ---------- ----------- + 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library +*****************************************************************************************************************************/ + +#pragma once + +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. +#endif + +#ifndef TIMER_INTERRUPT_DEBUG + #define TIMER_INTERRUPT_DEBUG 0 +#endif + +class STM32TimerInterrupt; + +typedef STM32TimerInterrupt STM32Timer; + +typedef void (*timerCallback) (void); + + +class STM32TimerInterrupt +{ + private: + TIM_TypeDef* _timer; + HardwareTimer* _hwTimer = NULL; + + timerCallback _callback; // pointer to the callback function + float _frequency; // Timer frequency + uint32_t _timerCount; // count to activate timer + + public: + + STM32TimerInterrupt(TIM_TypeDef* timer) + { + _timer = timer; + + _hwTimer = new HardwareTimer(_timer); + + _callback = NULL; + }; + + ~STM32TimerInterrupt() + { + if (_hwTimer) + delete _hwTimer; + } + + // frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool setFrequency(float frequency, timerCallback callback) + { + // select timer frequency is 1MHz for better accuracy. We don't use 16-bit prescaler for now. + // Will use later if very low frequency is needed. + _frequency = 1000000; + _timerCount = (uint32_t) _frequency / frequency; + +#if (TIMER_INTERRUPT_DEBUG > 0) + Serial.println("STM32TimerInterrupt: Timer Input Freq (Hz) = " + String(_hwTimer->getTimerClkFreq()) + ", _fre = " + String(_frequency) + + ", _count = " + String((uint32_t) (_timerCount))); +#endif + + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->setOverflow(_timerCount, MICROSEC_FORMAT); + + _hwTimer->attachInterrupt(callback); + _hwTimer->resume(); + + return true; + } + + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool setInterval(unsigned long interval, timerCallback callback) + { + return setFrequency((float) (1000000.0f / interval), callback); + } + + bool attachInterrupt(float frequency, timerCallback callback) + { + return setFrequency(frequency, callback); + } + + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c + bool attachInterruptInterval(unsigned long interval, timerCallback callback) + { + return setFrequency( (float) ( 1000000.0f / interval), callback); + } + + void detachInterrupt() + { + _hwTimer->detachInterrupt(); + } + + void disableTimer(void) + { + //_hwTimer->detachInterrupt(); + _hwTimer->pause(); + } + + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely + void reattachInterrupt() + { + setFrequency(_frequency, _callback); + } + + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely + void enableTimer(void) + { + //setFrequency(_frequency, _callback); + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->resume(); + } + + // Just stop clock source, clear the count + void stopTimer(void) + { + _hwTimer->pause(); + _hwTimer->setCount(0, MICROSEC_FORMAT); + } + + // Just reconnect clock source, start current count from 0 + void restartTimer(void) + { + _hwTimer->setCount(0, MICROSEC_FORMAT); + _hwTimer->resume(); + } +}; // class STM32TimerInterrupt diff --git a/src_h/TeensyTimerInterrupt.h b/src_h/TeensyTimerInterrupt.h index d0b21b26..d0c54ec4 100644 --- a/src_h/TeensyTimerInterrupt.h +++ b/src_h/TeensyTimerInterrupt.h @@ -19,11 +19,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once diff --git a/src_h/TimerInterrupt.h b/src_h/TimerInterrupt.h index a1ddd446..445d6395 100644 --- a/src_h/TimerInterrupt.h +++ b/src_h/TimerInterrupt.h @@ -26,11 +26,12 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ diff --git a/src_h/TimerInterrupt_Generic.h b/src_h/TimerInterrupt_Generic.h index 0dcd368a..695e8847 100644 --- a/src_h/TimerInterrupt_Generic.h +++ b/src_h/TimerInterrupt_Generic.h @@ -19,16 +19,17 @@ Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic Licensed under MIT license - Version: 1.1.0 + Version: 1.2.0 Version Modified By Date Comments ------- ----------- ---------- ----------- 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library *****************************************************************************************************************************/ #pragma once -#define TIMER_INTERRUPT_GENERIC_VERSION "1.1.0" +#define TIMER_INTERRUPT_GENERIC_VERSION "1.2.0" #if defined(__AVR_ATmega8__) || defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) @@ -74,6 +75,12 @@ #define TIMER_INTERRUPT_USING_TEENSY true #include "TeensyTimerInterrupt.h" +#elif ( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ + defined(STM32WB) || defined(STM32MP1) ) + + #define TIMER_INTERRUPT_USING_STM32 true + #include "STM32TimerInterrupt.h" #else #error Unsupported Board! Please check your Tools->Board setting.