From 841a6ee68d5883cc47f4a32d5c5740aa24ee7de2 Mon Sep 17 00:00:00 2001 From: midilab Date: Tue, 9 May 2023 05:42:58 -0400 Subject: [PATCH] implmemtation review --- src/platforms/avr.h | 2 +- src/platforms/stm32.h | 38 ++++++++++++++++---------------------- src/uClock.cpp | 4 ++-- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/platforms/avr.h b/src/platforms/avr.h index c96db5f..0a8dac9 100644 --- a/src/platforms/avr.h +++ b/src/platforms/avr.h @@ -3,7 +3,7 @@ #define ATOMIC(X) noInterrupts(); X; interrupts(); // want a different avr clock support? -// TODO: we should do this using macro guards for avrs different clocks +// TODO: we should do this using macro guards for avrs different clocks freqeuncy setup at compile time #define AVR_CLOCK_FREQ 16000000 void initTimer(uint32_t init_clock) diff --git a/src/platforms/stm32.h b/src/platforms/stm32.h index 45436fe..5464935 100644 --- a/src/platforms/stm32.h +++ b/src/platforms/stm32.h @@ -1,16 +1,13 @@ #include -static TIM_HandleTypeDef s_TimerInstance = {0}; - -typedef void (*timer_callback_t)(void); -timer_callback_t timer_callback = NULL; +static TIM_HandleTypeDef _uclockTimer = {0}; #define ATOMIC(X) noInterrupts(); X; interrupts(); // forward declaration of ISR void uclockISR(); -void timer_attachInterrupt(TIM_TypeDef *tim, uint32_t microseconds, timer_callback_t callback) +void timer_attachInterrupt(TIM_TypeDef *tim, uint32_t us_interval) { // Enable timer clock if (tim == TIM2) __HAL_RCC_TIM2_CLK_ENABLE(); @@ -21,36 +18,33 @@ void timer_attachInterrupt(TIM_TypeDef *tim, uint32_t microseconds, timer_callba uint32_t prescaler = (SystemCoreClock / 1000000UL) - 1; // Calculate the period value - uint32_t period = (microseconds * 2UL) - 1UL; + uint32_t period = (us_interval * 2UL) - 1UL; // Set up the timer instance - s_TimerInstance.Instance = tim; - s_TimerInstance.Init.Prescaler = prescaler; - s_TimerInstance.Init.CounterMode = TIM_COUNTERMODE_UP; - s_TimerInstance.Init.Period = period; - s_TimerInstance.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + _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(&s_TimerInstance); - HAL_TIM_Base_Start_IT(&s_TimerInstance); - - // Save the callback function - timer_callback = callback; + HAL_TIM_Base_Init(&_uclockTimer); + HAL_TIM_Base_Start_IT(&_uclockTimer); } void TIM2_IRQHandler() { - // Call the callback function - timer_callback(); + // Call the uClock ISR handler + uclockISR(); // Clear the interrupt flag - __HAL_TIM_CLEAR_FLAG(&s_TimerInstance, TIM_FLAG_UPDATE); + __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, uclockISR); + timer_attachInterrupt(TIM2, us_interval); } void setTimer(uint32_t us_interval) @@ -59,7 +53,7 @@ void setTimer(uint32_t us_interval) uint32_t period = (us_interval * 2UL) - 1UL; // Update the timer instance with the new period value - s_TimerInstance.Init.Period = period; - HAL_TIM_Base_Init(&s_TimerInstance); + _uclockTimer.Init.Period = period; + HAL_TIM_Base_Init(&_uclockTimer); } diff --git a/src/uClock.cpp b/src/uClock.cpp index a6f8c8a..eb6ac75 100755 --- a/src/uClock.cpp +++ b/src/uClock.cpp @@ -34,7 +34,7 @@ #include "platforms/avr.h" #endif // -// Teensyduino port +// Teensyduino ARMs port // #if defined(TEENSYDUINO) #include "platforms/teensy.h" @@ -52,7 +52,7 @@ #include "platforms/esp32.h" #endif // -// STM32XX family? +// STM32XX family // #if defined(ARDUINO_ARCH_STM32) #include "platforms/stm32.h"