File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
119123void HAL_timer_start (const uint8_t timer_num, const uint32_t frequency) {
120124 if (!HAL_timer_initialized (timer_num)) {
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 4747
4848#define STEP_TIMER 10
4949#define TEMP_TIMER 14
50- #define HAL_TIMER_RATE F_CPU
5150
5251//
5352// Limit Switches
You can’t perform that action at this time.
0 commit comments