This repository was 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
/
Copy pathSTM32TimerInterrupt_Generic.h
191 lines (148 loc) · 7.25 KB
/
STM32TimerInterrupt_Generic.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/****************************************************************************************************************************
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.13.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
1.3.0 K Hoang 01/12/2020 Add Mbed Mano-33-BLE Library. Add support to AVR UNO, Nano, Arduino Mini, Ethernet, BT. etc.
1.3.1 K.Hoang 09/12/2020 Add complex examples and board Version String. Fix SAMD bug.
1.3.2 K.Hoang 06/01/2021 Fix warnings. Optimize examples to reduce memory usage
1.4.0 K.Hoang 02/04/2021 Add support to Arduino, Adafruit, Sparkfun AVR 32u4, 328P, 128128RFA1 and Sparkfun SAMD
1.5.0 K.Hoang 17/04/2021 Add support to Arduino megaAVR ATmega4809-based boards (Nano Every, UNO WiFi Rev2, etc.)
1.6.0 K.Hoang 15/06/2021 Add T3/T4 support to 32u4. Add support to RP2040, ESP32-S2
1.7.0 K.Hoang 13/08/2021 Add support to Adafruit nRF52 core v0.22.0+
1.8.0 K.Hoang 24/11/2021 Update to use latest TimerInterrupt Libraries' versions
1.9.0 K.Hoang 09/05/2022 Update to use latest TimerInterrupt Libraries' versions
1.10.0 K.Hoang 10/08/2022 Update to use latest ESP32_New_TimerInterrupt Library version
1.11.0 K.Hoang 12/08/2022 Add support to new ESP32_C3, ESP32_S2 and ESP32_S3 boards
1.12.0 K.Hoang 29/09/2022 Update for SAMD, RP2040, MBED_RP2040
1.13.0 K.Hoang 16/11/2022 Fix doubled time for ESP32_C3,S2 and S3. Fix poor timer accuracy bug for MBED RP2040
Fix bug disabling TCB0 for megaAVR
*****************************************************************************************************************************/
#pragma once
#ifndef STM32TIMERINTERRUPT_H
#define STM32TIMERINTERRUPT_H
#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) || defined(STM32L5) )
#error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting.
#endif
#ifndef STM32_TIMER_INTERRUPT_VERSION
#define STM32_TIMER_INTERRUPT_VERSION "STM32TimerInterrupt v1.3.0"
#define STM32_TIMER_INTERRUPT_VERSION_MAJOR 1
#define STM32_TIMER_INTERRUPT_VERSION_MINOR 3
#define STM32_TIMER_INTERRUPT_VERSION_PATCH 0
#define STM32_TIMER_INTERRUPT_VERSION_INT 1003000
#endif
#ifndef TIMER_INTERRUPT_DEBUG
#define TIMER_INTERRUPT_DEBUG 0
#endif
#include "TimerInterrupt_Generic_Debug.h"
class STM32TimerInterrupt;
typedef STM32TimerInterrupt STM32Timer;
typedef void (*timerCallback) ();
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 and use MICROSEC_FORMAT. We don't use 16-bit prescaler for now.
// Will use later if very low frequency is needed.
#define TIM_CLOCK_FREQ (1000000.0f)
_frequency = frequency;
_timerCount = (uint32_t) ( TIM_CLOCK_FREQ / frequency );
TISR_LOGWARN3(F("Timer Input Freq (Hz) ="), _hwTimer->getTimerClkFreq(), F(", Timer Clock Frequency ="),
TIM_CLOCK_FREQ);
TISR_LOGWARN3(F("Timer Frequency ="), _frequency, F(", _count ="), (uint32_t) (_timerCount));
_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()
{
//_hwTimer->detachInterrupt();
_hwTimer->pause();
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
void reattachInterrupt()
{
if ( (_frequency > 0) && (_timerCount > 0) && (_callback != NULL) )
setFrequency(_frequency, _callback);
}
// Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
void enableTimer()
{
//setFrequency(_frequency, _callback);
_hwTimer->setCount(0, MICROSEC_FORMAT);
_hwTimer->resume();
}
// Just stop clock source, clear the count
void stopTimer()
{
_hwTimer->pause();
_hwTimer->setCount(0, MICROSEC_FORMAT);
}
// Just reconnect clock source, start current count from 0
void restartTimer()
{
_hwTimer->setCount(0, MICROSEC_FORMAT);
_hwTimer->resume();
}
}; // class STM32TimerInterrupt
#endif // STM32TIMERINTERRUPT_H