This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Releases v1.2.0 1. Add STM32_TimerInterrupt Library
- Loading branch information
1 parent
73e09a0
commit 5f56fa4
Showing
33 changed files
with
549 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: [email protected] | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.