Bläddra i källkod

Get STM32 clock rates from framework (#19978)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
Jason Smith 4 år sedan
förälder
incheckning
4a39c8cd53
Inget konto är kopplat till bidragsgivarens mejladress

+ 9
- 5
Marlin/src/HAL/STM32/timers.cpp Visa fil

68
 #endif
68
 #endif
69
 
69
 
70
 #ifdef STM32F0xx
70
 #ifdef STM32F0xx
71
-  #define MCU_TIMER_RATE (F_CPU)      // Frequency of timer peripherals
72
   #define MCU_STEP_TIMER 16
71
   #define MCU_STEP_TIMER 16
73
   #define MCU_TEMP_TIMER 17
72
   #define MCU_TEMP_TIMER 17
74
 #elif defined(STM32F1xx)
73
 #elif defined(STM32F1xx)
75
-  #define MCU_TIMER_RATE (F_CPU)
76
   #define MCU_STEP_TIMER  4
74
   #define MCU_STEP_TIMER  4
77
   #define MCU_TEMP_TIMER  2
75
   #define MCU_TEMP_TIMER  2
78
 #elif defined(STM32F401xC) || defined(STM32F401xE)
76
 #elif defined(STM32F401xC) || defined(STM32F401xE)
79
-  #define MCU_TIMER_RATE (F_CPU / 2)
80
   #define MCU_STEP_TIMER  9
77
   #define MCU_STEP_TIMER  9
81
   #define MCU_TEMP_TIMER 10
78
   #define MCU_TEMP_TIMER 10
82
 #elif defined(STM32F4xx) || defined(STM32F7xx)
79
 #elif defined(STM32F4xx) || defined(STM32F7xx)
83
-  #define MCU_TIMER_RATE (F_CPU / 2)
84
   #define MCU_STEP_TIMER  6           // STM32F401 has no TIM6, TIM7, or TIM8
80
   #define MCU_STEP_TIMER  6           // STM32F401 has no TIM6, TIM7, or TIM8
85
   #define MCU_TEMP_TIMER 14           // TIM7 is consumed by Software Serial if used.
81
   #define MCU_TEMP_TIMER 14           // TIM7 is consumed by Software Serial if used.
86
 #endif
82
 #endif
87
 
83
 
88
 #ifndef HAL_TIMER_RATE
84
 #ifndef HAL_TIMER_RATE
89
-  #define HAL_TIMER_RATE MCU_TIMER_RATE
85
+  #define HAL_TIMER_RATE GetStepperTimerClkFreq()
90
 #endif
86
 #endif
87
+
91
 #ifndef STEP_TIMER
88
 #ifndef STEP_TIMER
92
   #define STEP_TIMER MCU_STEP_TIMER
89
   #define STEP_TIMER MCU_STEP_TIMER
93
 #endif
90
 #endif
115
 // Public functions
112
 // Public functions
116
 // ------------------------
113
 // ------------------------
117
 
114
 
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
+
118
 // frequency is in Hertz
122
 // frequency is in Hertz
119
 void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
123
 void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
120
   if (!HAL_timer_initialized(timer_num)) {
124
   if (!HAL_timer_initialized(timer_num)) {

+ 2
- 1
Marlin/src/HAL/STM32/timers.h Visa fil

57
 
57
 
58
 // TODO: get rid of manual rate/prescale/ticks/cycles taken for procedures in stepper.cpp
58
 // TODO: get rid of manual rate/prescale/ticks/cycles taken for procedures in stepper.cpp
59
 #define STEPPER_TIMER_RATE 2000000 // 2 Mhz
59
 #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))
61
 #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
62
 #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
62
 
63
 
63
 #define PULSE_TIMER_RATE STEPPER_TIMER_RATE
64
 #define PULSE_TIMER_RATE STEPPER_TIMER_RATE

+ 0
- 1
Marlin/src/pins/stm32f4/pins_RUMBA32_common.h Visa fil

47
 
47
 
48
 #define STEP_TIMER 10
48
 #define STEP_TIMER 10
49
 #define TEMP_TIMER 14
49
 #define TEMP_TIMER 14
50
-#define HAL_TIMER_RATE                     F_CPU
51
 
50
 
52
 //
51
 //
53
 // Limit Switches
52
 // Limit Switches

Laddar…
Avbryt
Spara