diff --git a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h index fdd26b0cf1..95c0db5c6e 100644 --- a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h +++ b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h @@ -102,10 +102,10 @@ extern "C" { #define TEMP_TIMER_NUM 0 #define TEMP_TIMER_FREQUENCY (F_CPU / 64.0 / 256.0) -#define HAL_TIMER_RATE ((F_CPU) / 8.0) -#define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE -#define STEPPER_TIMER_PRESCALE INT0_PRESCALER -#define HAL_TICKS_PER_US (((F_CPU) / 8) / 1000000) // Can not be of type double +#define HAL_TIMER_RATE ((F_CPU) / 8) // i.e., 2MHz or 2.5MHz +#define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE +#define STEPPER_TIMER_PRESCALE INT0_PRESCALER +#define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A) #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A) diff --git a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h index 64a0e72a39..5dfd42ce94 100644 --- a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h +++ b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h @@ -46,7 +46,7 @@ #define STEP_TIMER_NUM 3 // index of timer to use for stepper #define TEMP_TIMER_NUM 4 // index of timer to use for temperature -#define HAL_TIMER_RATE ((F_CPU) / 2.0) // frequency of timers peripherals +#define HAL_TIMER_RATE ((F_CPU) / 2) // frequency of timers peripherals #define STEPPER_TIMER_PRESCALE 1.0 // prescaler for setting stepper frequency #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL_timers.cpp b/Marlin/src/HAL/HAL_LPC1768/HAL_timers.cpp index 3613f8a406..c46e80f4b8 100644 --- a/Marlin/src/HAL/HAL_LPC1768/HAL_timers.cpp +++ b/Marlin/src/HAL/HAL_LPC1768/HAL_timers.cpp @@ -33,22 +33,22 @@ void HAL_timer_init(void) { SBI(LPC_SC->PCONP, 1); // power on timer0 - LPC_TIM0->PR = ((HAL_TIMER_RATE / HAL_STEPPER_TIMER_RATE) - 1); // Use prescaler to set frequency if needed + LPC_TIM0->PR = (HAL_TIMER_RATE) / (HAL_STEPPER_TIMER_RATE) - 1; // Use prescaler to set frequency if needed SBI(LPC_SC->PCONP, 2); // power on timer1 - LPC_TIM1->PR = ((HAL_TIMER_RATE / 1000000) - 1); + LPC_TIM1->PR = (HAL_TIMER_RATE) / 1000000 - 1; } void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) { switch (timer_num) { case 0: LPC_TIM0->MCR = 3; // Match on MR0, reset on MR0 - LPC_TIM0->MR0 = (uint32_t)(HAL_STEPPER_TIMER_RATE / frequency); // Match value (period) to set frequency + LPC_TIM0->MR0 = uint32_t(HAL_STEPPER_TIMER_RATE) / frequency; // Match value (period) to set frequency LPC_TIM0->TCR = _BV(0); // enable break; case 1: LPC_TIM1->MCR = 3; - LPC_TIM1->MR0 = (uint32_t)(HAL_TEMP_TIMER_RATE / frequency);; + LPC_TIM1->MR0 = uint32_t(HAL_TEMP_TIMER_RATE) / frequency; LPC_TIM1->TCR = _BV(0); break; default: break; diff --git a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.cpp b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.cpp index bf1ae230b3..eb002ab93f 100644 --- a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.cpp +++ b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.cpp @@ -93,14 +93,14 @@ Timer_clock4: Prescaler 128 -> 656.25kHz * TODO: Calculate Timer prescale value, so we get the 32bit to adjust */ -void HAL_timer_start (uint8_t timer_num, uint32_t frequency) { +void HAL_timer_start(uint8_t timer_num, uint32_t frequency) { switch (timer_num) { case STEP_TIMER_NUM: StepperTimer.pause(); StepperTimer.setCount(0); StepperTimer.setPrescaleFactor(STEPPER_TIMER_PRESCALE); StepperTimer.setOverflow(0xFFFF); - StepperTimer.setCompare (STEP_TIMER_CHAN, (HAL_STEPPER_TIMER_RATE / frequency)); + StepperTimer.setCompare(STEP_TIMER_CHAN, uint32_t(HAL_STEPPER_TIMER_RATE) / frequency); StepperTimer.refresh(); StepperTimer.resume(); break; @@ -109,14 +109,14 @@ void HAL_timer_start (uint8_t timer_num, uint32_t frequency) { TempTimer.setCount(0); TempTimer.setPrescaleFactor(TEMP_TIMER_PRESCALE); TempTimer.setOverflow(0xFFFF); - TempTimer.setCompare (TEMP_TIMER_CHAN, ((F_CPU / TEMP_TIMER_PRESCALE) / frequency)); + TempTimer.setCompare(TEMP_TIMER_CHAN, (F_CPU) / (TEMP_TIMER_PRESCALE) / frequency); TempTimer.refresh(); TempTimer.resume(); break; } } -void HAL_timer_enable_interrupt (uint8_t timer_num) { +void HAL_timer_enable_interrupt(uint8_t timer_num) { switch (timer_num) { case STEP_TIMER_NUM: StepperTimer.attachInterrupt(STEP_TIMER_CHAN, stepTC_Handler); @@ -129,7 +129,7 @@ void HAL_timer_enable_interrupt (uint8_t timer_num) { } } -void HAL_timer_disable_interrupt (uint8_t timer_num) { +void HAL_timer_disable_interrupt(uint8_t timer_num) { switch (timer_num) { case STEP_TIMER_NUM: StepperTimer.detachInterrupt(STEP_TIMER_CHAN); diff --git a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h index ab12de81f8..e48ea54604 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h @@ -59,7 +59,7 @@ #define HAL_TIMER_RATE (FTM0_TIMER_RATE) #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE -#define HAL_TICKS_PER_US (HAL_STEPPER_TIMER_RATE/1000000) +#define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) #define TEMP_TIMER_FREQUENCY 1000 diff --git a/Marlin/src/gcode/config/M43.cpp b/Marlin/src/gcode/config/M43.cpp index 225843017e..e3f2021f55 100644 --- a/Marlin/src/gcode/config/M43.cpp +++ b/Marlin/src/gcode/config/M43.cpp @@ -224,7 +224,7 @@ inline void servo_probe_test() { * - Machine continues to operate * - Reports changes to endstops * - Toggles LED_PIN when an endstop changes - * - Can not reliably catch the 5mS pulse from BLTouch type probes + * - Cannot reliably catch the 5mS pulse from BLTouch type probes * * M43 T - Toggle pin(s) and report which pin is being toggled * S - Start Pin number. If not given, will default to 0 diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index 15a3e1e548..c86a665b09 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -207,7 +207,7 @@ /** * Hidden options for developer */ - // Double stepping start from STEP_DOUBLER_FREQUENCY + 1, quad stepping start from STEP_DOUBLER_FREQUENCY * 2 + 1 + // Double stepping starts at STEP_DOUBLER_FREQUENCY + 1, quad stepping starts at STEP_DOUBLER_FREQUENCY * 2 + 1 #ifndef STEP_DOUBLER_FREQUENCY #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE) #define STEP_DOUBLER_FREQUENCY 60000 // Hz diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index d4f198c18f..bf3c164976 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -303,10 +303,9 @@ class Stepper { #ifdef CPU_32_BIT // In case of high-performance processor, it is able to calculate in real-time - timer = (uint32_t)(HAL_STEPPER_TIMER_RATE) / step_rate; - if (timer < (HAL_STEPPER_TIMER_RATE / (STEP_DOUBLER_FREQUENCY * 2))) { // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen) - timer = (HAL_STEPPER_TIMER_RATE / (STEP_DOUBLER_FREQUENCY * 2)); - } + constexpr uint32_t MIN_TIME_PER_STEP = (HAL_STEPPER_TIMER_RATE) / ((STEP_DOUBLER_FREQUENCY) * 2); + timer = uint32_t(HAL_STEPPER_TIMER_RATE) / step_rate; + NOLESS(timer, MIN_TIME_PER_STEP); // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen) #else NOLESS(step_rate, F_CPU / 500000); step_rate -= F_CPU / 500000; // Correct for minimal speed