Skip to content

Commit 4a39c8c

Browse files
Get STM32 clock rates from framework (#19978)
Co-authored-by: Scott Lahteine <[email protected]>
1 parent c05beb7 commit 4a39c8c

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

Marlin/src/HAL/STM32/timers.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,23 @@
6868
#endif
6969

7070
#ifdef STM32F0xx
71-
#define MCU_TIMER_RATE (F_CPU) // Frequency of timer peripherals
7271
#define MCU_STEP_TIMER 16
7372
#define MCU_TEMP_TIMER 17
7473
#elif defined(STM32F1xx)
75-
#define MCU_TIMER_RATE (F_CPU)
7674
#define MCU_STEP_TIMER 4
7775
#define MCU_TEMP_TIMER 2
7876
#elif defined(STM32F401xC) || defined(STM32F401xE)
79-
#define MCU_TIMER_RATE (F_CPU / 2)
8077
#define MCU_STEP_TIMER 9
8178
#define MCU_TEMP_TIMER 10
8279
#elif defined(STM32F4xx) || defined(STM32F7xx)
83-
#define MCU_TIMER_RATE (F_CPU / 2)
8480
#define MCU_STEP_TIMER 6 // STM32F401 has no TIM6, TIM7, or TIM8
8581
#define MCU_TEMP_TIMER 14 // TIM7 is consumed by Software Serial if used.
8682
#endif
8783

8884
#ifndef HAL_TIMER_RATE
89-
#define HAL_TIMER_RATE MCU_TIMER_RATE
85+
#define HAL_TIMER_RATE GetStepperTimerClkFreq()
9086
#endif
87+
9188
#ifndef STEP_TIMER
9289
#define STEP_TIMER MCU_STEP_TIMER
9390
#endif
@@ -115,6 +112,13 @@ HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
115112
// Public functions
116113
// ------------------------
117114

115+
uint32_t GetStepperTimerClkFreq() {
116+
// Timer input clocks vary between devices, and in some cases between timers on the same device.
117+
// Retrieve at runtime to ensure device compatibility. Cache result to avoid repeated overhead.
118+
static uint32_t clkfreq = timer_instance[STEP_TIMER_NUM]->getTimerClkFreq();
119+
return clkfreq;
120+
}
121+
118122
// frequency is in Hertz
119123
void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
120124
if (!HAL_timer_initialized(timer_num)) {

Marlin/src/HAL/STM32/timers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757

5858
// TODO: get rid of manual rate/prescale/ticks/cycles taken for procedures in stepper.cpp
5959
#define STEPPER_TIMER_RATE 2000000 // 2 Mhz
60-
#define STEPPER_TIMER_PRESCALE ((HAL_TIMER_RATE)/(STEPPER_TIMER_RATE))
60+
extern uint32_t GetStepperTimerClkFreq();
61+
#define STEPPER_TIMER_PRESCALE (GetStepperTimerClkFreq() / (STEPPER_TIMER_RATE))
6162
#define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
6263

6364
#define PULSE_TIMER_RATE STEPPER_TIMER_RATE

Marlin/src/pins/stm32f4/pins_RUMBA32_common.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848
#define STEP_TIMER 10
4949
#define TEMP_TIMER 14
50-
#define HAL_TIMER_RATE F_CPU
5150

5251
//
5352
// Limit Switches

0 commit comments

Comments
 (0)