From 8904afff61207f62578d2b1aa5d9dd25727ec4e4 Mon Sep 17 00:00:00 2001 From: midilab Date: Sat, 13 May 2023 05:16:03 -0400 Subject: [PATCH] change implmementation from HAL to SMT32Duino official examples for timer usage --- .../STM32UartMasterMidiClock.ino | 8 +-- src/platforms/stm32.h | 60 ++++++------------- 2 files changed, 21 insertions(+), 47 deletions(-) diff --git a/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino b/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino index 18ad367..6c8a03f 100644 --- a/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino +++ b/examples/STM32UartMasterMidiClock/STM32UartMasterMidiClock.ino @@ -36,21 +36,21 @@ void handle_bpm_led(uint32_t tick) // Internal clock handlers void ClockOut96PPQN(uint32_t tick) { // Send MIDI_CLOCK to external gears - Serial.write(MIDI_CLOCK); + //Serial.write(MIDI_CLOCK); handle_bpm_led(tick); } void onClockStart() { - Serial.write(MIDI_START); + //Serial.write(MIDI_START); } void onClockStop() { - Serial.write(MIDI_STOP); + //Serial.write(MIDI_STOP); } void setup() { // Initialize serial communication at 31250 bits per second, the default MIDI serial speed communication: - Serial.begin(31250); + //Serial.begin(31250); // A led to count bpms pinMode(LED_BUILTIN, OUTPUT); diff --git a/src/platforms/stm32.h b/src/platforms/stm32.h index 5464935..857bfbe 100644 --- a/src/platforms/stm32.h +++ b/src/platforms/stm32.h @@ -1,59 +1,33 @@ #include -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(); }