Get STM32 clock rates from framework (#19978)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
This commit is contained in:
Jason Smith 2020-11-01 01:39:30 -07:00 committed by GitHub
parent c05beb74a9
commit 4a39c8cd53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 7 deletions

View File

@ -68,26 +68,23 @@
#endif #endif
#ifdef STM32F0xx #ifdef STM32F0xx
#define MCU_TIMER_RATE (F_CPU) // Frequency of timer peripherals
#define MCU_STEP_TIMER 16 #define MCU_STEP_TIMER 16
#define MCU_TEMP_TIMER 17 #define MCU_TEMP_TIMER 17
#elif defined(STM32F1xx) #elif defined(STM32F1xx)
#define MCU_TIMER_RATE (F_CPU)
#define MCU_STEP_TIMER 4 #define MCU_STEP_TIMER 4
#define MCU_TEMP_TIMER 2 #define MCU_TEMP_TIMER 2
#elif defined(STM32F401xC) || defined(STM32F401xE) #elif defined(STM32F401xC) || defined(STM32F401xE)
#define MCU_TIMER_RATE (F_CPU / 2)
#define MCU_STEP_TIMER 9 #define MCU_STEP_TIMER 9
#define MCU_TEMP_TIMER 10 #define MCU_TEMP_TIMER 10
#elif defined(STM32F4xx) || defined(STM32F7xx) #elif defined(STM32F4xx) || defined(STM32F7xx)
#define MCU_TIMER_RATE (F_CPU / 2)
#define MCU_STEP_TIMER 6 // STM32F401 has no TIM6, TIM7, or TIM8 #define MCU_STEP_TIMER 6 // STM32F401 has no TIM6, TIM7, or TIM8
#define MCU_TEMP_TIMER 14 // TIM7 is consumed by Software Serial if used. #define MCU_TEMP_TIMER 14 // TIM7 is consumed by Software Serial if used.
#endif #endif
#ifndef HAL_TIMER_RATE #ifndef HAL_TIMER_RATE
#define HAL_TIMER_RATE MCU_TIMER_RATE #define HAL_TIMER_RATE GetStepperTimerClkFreq()
#endif #endif
#ifndef STEP_TIMER #ifndef STEP_TIMER
#define STEP_TIMER MCU_STEP_TIMER #define STEP_TIMER MCU_STEP_TIMER
#endif #endif
@ -115,6 +112,13 @@ HardwareTimer *timer_instance[NUM_HARDWARE_TIMERS] = { nullptr };
// Public functions // Public functions
// ------------------------ // ------------------------
uint32_t GetStepperTimerClkFreq() {
// Timer input clocks vary between devices, and in some cases between timers on the same device.
// Retrieve at runtime to ensure device compatibility. Cache result to avoid repeated overhead.
static uint32_t clkfreq = timer_instance[STEP_TIMER_NUM]->getTimerClkFreq();
return clkfreq;
}
// frequency is in Hertz // frequency is in Hertz
void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
if (!HAL_timer_initialized(timer_num)) { if (!HAL_timer_initialized(timer_num)) {

View File

@ -57,7 +57,8 @@
// TODO: get rid of manual rate/prescale/ticks/cycles taken for procedures in stepper.cpp // TODO: get rid of manual rate/prescale/ticks/cycles taken for procedures in stepper.cpp
#define STEPPER_TIMER_RATE 2000000 // 2 Mhz #define STEPPER_TIMER_RATE 2000000 // 2 Mhz
#define STEPPER_TIMER_PRESCALE ((HAL_TIMER_RATE)/(STEPPER_TIMER_RATE)) extern uint32_t GetStepperTimerClkFreq();
#define STEPPER_TIMER_PRESCALE (GetStepperTimerClkFreq() / (STEPPER_TIMER_RATE))
#define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs #define STEPPER_TIMER_TICKS_PER_US ((STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
#define PULSE_TIMER_RATE STEPPER_TIMER_RATE #define PULSE_TIMER_RATE STEPPER_TIMER_RATE

View File

@ -47,7 +47,6 @@
#define STEP_TIMER 10 #define STEP_TIMER 10
#define TEMP_TIMER 14 #define TEMP_TIMER 14
#define HAL_TIMER_RATE F_CPU
// //
// Limit Switches // Limit Switches