mirror of https://github.com/midilab/uClock
parent
841a6ee68d
commit
8904afff61
@ -1,59 +1,33 @@ |
||||
#include <Arduino.h> |
||||
|
||||
static TIM_HandleTypeDef _uclockTimer = {0}; |
||||
#if !defined(STM32_CORE_VERSION) || (STM32_CORE_VERSION < 0x01090000) |
||||
#error "Due to API change, this library is compatible with STM32_CORE_VERSION >= 0x01090000. Please update your stm32duino core." |
||||
#endif |
||||
|
||||
#if defined(TIM1) |
||||
TIM_TypeDef * TimerInstance = TIM1; |
||||
#else |
||||
TIM_TypeDef * TimerInstance = TIM2; |
||||
#endif |
||||
|
||||
// Instantiate HardwareTimer object. Thanks to 'new' instanciation, HardwareTimer is not destructed when setup() function is finished.
|
||||
HardwareTimer * _uclockTimer = new HardwareTimer(TimerInstance); |
||||
|
||||
#define ATOMIC(X) noInterrupts(); X; interrupts(); |
||||
|
||||
// forward declaration of ISR
|
||||
void uclockISR(); |
||||
|
||||
void timer_attachInterrupt(TIM_TypeDef *tim, uint32_t us_interval) |
||||
{ |
||||
// Enable timer clock
|
||||
if (tim == TIM2) __HAL_RCC_TIM2_CLK_ENABLE(); |
||||
else if (tim == TIM3) __HAL_RCC_TIM3_CLK_ENABLE(); |
||||
else if (tim == TIM4) __HAL_RCC_TIM4_CLK_ENABLE(); |
||||
|
||||
// Calculate the prescaler value
|
||||
uint32_t prescaler = (SystemCoreClock / 1000000UL) - 1; |
||||
|
||||
// Calculate the period value
|
||||
uint32_t period = (us_interval * 2UL) - 1UL; |
||||
|
||||
// Set up the timer instance
|
||||
_uclockTimer.Instance = tim; |
||||
_uclockTimer.Init.Prescaler = prescaler; |
||||
_uclockTimer.Init.CounterMode = TIM_COUNTERMODE_UP; |
||||
_uclockTimer.Init.Period = period; |
||||
_uclockTimer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
||||
|
||||
// Configure the timer instance
|
||||
HAL_TIM_Base_Init(&_uclockTimer); |
||||
HAL_TIM_Base_Start_IT(&_uclockTimer); |
||||
} |
||||
|
||||
void TIM2_IRQHandler() |
||||
{ |
||||
// Call the uClock ISR handler
|
||||
uclockISR(); |
||||
|
||||
// Clear the interrupt flag
|
||||
__HAL_TIM_CLEAR_FLAG(&_uclockTimer, TIM_FLAG_UPDATE); |
||||
} |
||||
|
||||
void initTimer(uint32_t us_interval) |
||||
{ |
||||
// Set up the timer to call the callback function every us_interval microseconds
|
||||
timer_attachInterrupt(TIM2, us_interval); |
||||
_uclockTimer->setOverflow(us_interval, MICROSEC_FORMAT); |
||||
_uclockTimer->attachInterrupt(uclockISR); |
||||
_uclockTimer->resume(); |
||||
} |
||||
|
||||
void setTimer(uint32_t us_interval) |
||||
{ |
||||
// Calculate the period value
|
||||
uint32_t period = (us_interval * 2UL) - 1UL; |
||||
|
||||
// Update the timer instance with the new period value
|
||||
_uclockTimer.Init.Period = period; |
||||
HAL_TIM_Base_Init(&_uclockTimer); |
||||
_uclockTimer->setOverflow(us_interval, MICROSEC_FORMAT); |
||||
_uclockTimer->refresh(); |
||||
} |
||||
|
||||
|
Loading…
Reference in new issue