|
| 1 | +/**************************************************************************************************************************** |
| 2 | + STM32TimerInterrupt.h |
| 3 | + For STM32 boards |
| 4 | + Written by Khoi Hoang |
| 5 | +
|
| 6 | + Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by |
| 7 | + unsigned long miliseconds), you just consume only one Hardware timer and avoid conflicting with other cores' tasks. |
| 8 | + The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers |
| 9 | + Therefore, their executions are not blocked by bad-behaving functions / tasks. |
| 10 | + This important feature is absolutely necessary for mission-critical tasks. |
| 11 | +
|
| 12 | + Based on SimpleTimer - A timer library for Arduino. |
| 13 | + |
| 14 | + Copyright (c) 2010 OTTOTECNICA Italy |
| 15 | +
|
| 16 | + Based on BlynkTimer.h |
| 17 | + Author: Volodymyr Shymanskyy |
| 18 | + |
| 19 | + Built by Khoi Hoang https://github.com/khoih-prog/TimerInterrupt_Generic |
| 20 | + Licensed under MIT license |
| 21 | +
|
| 22 | + Version: 1.2.0 |
| 23 | +
|
| 24 | + Version Modified By Date Comments |
| 25 | + ------- ----------- ---------- ----------- |
| 26 | + 1.1.0 K Hoang 10/11/2020 Initial Super-Library coding to merge all TimerInterrupt Libraries |
| 27 | + 1.2.0 K Hoang 12/11/2020 Add STM32_TimerInterrupt Library |
| 28 | +*****************************************************************************************************************************/ |
| 29 | + |
| 30 | +#pragma once |
| 31 | + |
| 32 | +#if !( defined(STM32F0) || defined(STM32F1) || defined(STM32F2) || defined(STM32F3) ||defined(STM32F4) || defined(STM32F7) || \ |
| 33 | + defined(STM32L0) || defined(STM32L1) || defined(STM32L4) || defined(STM32H7) ||defined(STM32G0) || defined(STM32G4) || \ |
| 34 | + defined(STM32WB) || defined(STM32MP1) ) |
| 35 | + #error This code is designed to run on STM32F/L/H/G/WB/MP1 platform! Please check your Tools->Board setting. |
| 36 | +#endif |
| 37 | + |
| 38 | +#ifndef TIMER_INTERRUPT_DEBUG |
| 39 | + #define TIMER_INTERRUPT_DEBUG 0 |
| 40 | +#endif |
| 41 | + |
| 42 | +class STM32TimerInterrupt; |
| 43 | + |
| 44 | +typedef STM32TimerInterrupt STM32Timer; |
| 45 | + |
| 46 | +typedef void (*timerCallback) (void); |
| 47 | + |
| 48 | + |
| 49 | +class STM32TimerInterrupt |
| 50 | +{ |
| 51 | + private: |
| 52 | + TIM_TypeDef* _timer; |
| 53 | + HardwareTimer* _hwTimer = NULL; |
| 54 | + |
| 55 | + timerCallback _callback; // pointer to the callback function |
| 56 | + float _frequency; // Timer frequency |
| 57 | + uint32_t _timerCount; // count to activate timer |
| 58 | + |
| 59 | + public: |
| 60 | + |
| 61 | + STM32TimerInterrupt(TIM_TypeDef* timer) |
| 62 | + { |
| 63 | + _timer = timer; |
| 64 | + |
| 65 | + _hwTimer = new HardwareTimer(_timer); |
| 66 | + |
| 67 | + _callback = NULL; |
| 68 | + }; |
| 69 | + |
| 70 | + ~STM32TimerInterrupt() |
| 71 | + { |
| 72 | + if (_hwTimer) |
| 73 | + delete _hwTimer; |
| 74 | + } |
| 75 | + |
| 76 | + // frequency (in hertz) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely |
| 77 | + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c |
| 78 | + bool setFrequency(float frequency, timerCallback callback) |
| 79 | + { |
| 80 | + // select timer frequency is 1MHz for better accuracy. We don't use 16-bit prescaler for now. |
| 81 | + // Will use later if very low frequency is needed. |
| 82 | + _frequency = 1000000; |
| 83 | + _timerCount = (uint32_t) _frequency / frequency; |
| 84 | + |
| 85 | +#if (TIMER_INTERRUPT_DEBUG > 0) |
| 86 | + Serial.println("STM32TimerInterrupt: Timer Input Freq (Hz) = " + String(_hwTimer->getTimerClkFreq()) + ", _fre = " + String(_frequency) |
| 87 | + + ", _count = " + String((uint32_t) (_timerCount))); |
| 88 | +#endif |
| 89 | + |
| 90 | + _hwTimer->setCount(0, MICROSEC_FORMAT); |
| 91 | + _hwTimer->setOverflow(_timerCount, MICROSEC_FORMAT); |
| 92 | + |
| 93 | + _hwTimer->attachInterrupt(callback); |
| 94 | + _hwTimer->resume(); |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely |
| 100 | + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c |
| 101 | + bool setInterval(unsigned long interval, timerCallback callback) |
| 102 | + { |
| 103 | + return setFrequency((float) (1000000.0f / interval), callback); |
| 104 | + } |
| 105 | + |
| 106 | + bool attachInterrupt(float frequency, timerCallback callback) |
| 107 | + { |
| 108 | + return setFrequency(frequency, callback); |
| 109 | + } |
| 110 | + |
| 111 | + // interval (in microseconds) and duration (in milliseconds). Duration = 0 or not specified => run indefinitely |
| 112 | + // No params and duration now. To be addes in the future by adding similar functions here or to STM32-hal-timer.c |
| 113 | + bool attachInterruptInterval(unsigned long interval, timerCallback callback) |
| 114 | + { |
| 115 | + return setFrequency( (float) ( 1000000.0f / interval), callback); |
| 116 | + } |
| 117 | + |
| 118 | + void detachInterrupt() |
| 119 | + { |
| 120 | + _hwTimer->detachInterrupt(); |
| 121 | + } |
| 122 | + |
| 123 | + void disableTimer(void) |
| 124 | + { |
| 125 | + //_hwTimer->detachInterrupt(); |
| 126 | + _hwTimer->pause(); |
| 127 | + } |
| 128 | + |
| 129 | + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely |
| 130 | + void reattachInterrupt() |
| 131 | + { |
| 132 | + setFrequency(_frequency, _callback); |
| 133 | + } |
| 134 | + |
| 135 | + // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely |
| 136 | + void enableTimer(void) |
| 137 | + { |
| 138 | + //setFrequency(_frequency, _callback); |
| 139 | + _hwTimer->setCount(0, MICROSEC_FORMAT); |
| 140 | + _hwTimer->resume(); |
| 141 | + } |
| 142 | + |
| 143 | + // Just stop clock source, clear the count |
| 144 | + void stopTimer(void) |
| 145 | + { |
| 146 | + _hwTimer->pause(); |
| 147 | + _hwTimer->setCount(0, MICROSEC_FORMAT); |
| 148 | + } |
| 149 | + |
| 150 | + // Just reconnect clock source, start current count from 0 |
| 151 | + void restartTimer(void) |
| 152 | + { |
| 153 | + _hwTimer->setCount(0, MICROSEC_FORMAT); |
| 154 | + _hwTimer->resume(); |
| 155 | + } |
| 156 | +}; // class STM32TimerInterrupt |
0 commit comments