From b8bc965414b57c4abf5e1ab80cd3d21ab2208c60 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 Dec 2017 20:00:10 -0600 Subject: [PATCH 1/4] General cleanup HAL timers --- Marlin/src/HAL/HAL_AVR/HAL_AVR.h | 10 +++--- Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h | 4 +-- .../src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h | 33 +++++++++---------- .../HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h | 10 +++--- Marlin/src/core/macros.h | 3 +- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h index 6c6b9be0b5..8c8c09b82f 100644 --- a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h +++ b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h @@ -100,13 +100,13 @@ extern "C" { // timers -#define STEP_TIMER_NUM OCR1A -#define TEMP_TIMER_NUM 0 -#define TEMP_TIMER_FREQUENCY (F_CPU / 64.0 / 256.0) +#define STEP_TIMER_NUM OCR1A +#define TEMP_TIMER_NUM 0 +#define TEMP_TIMER_FREQUENCY (F_CPU / 64.0 / 256.0) #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 STEPPER_TIMER_PRESCALE 8 #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A) @@ -118,6 +118,8 @@ extern "C" { //void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); #define HAL_timer_start(timer_num,frequency) +#define HAL_timer_get_count(timer) timer + //void HAL_timer_set_count(const uint8_t timer_num, const uint16_t count); #define HAL_timer_set_count(timer, count) timer = (count) diff --git a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h index 2d70792061..03ee584be9 100644 --- a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h +++ b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h @@ -87,7 +87,7 @@ extern const tTimerConfig TimerConfig[]; void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); -FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const uint32_t count) { +FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const hal_timer_t count) { const tTimerConfig *pConfig = &TimerConfig[timer_num]; pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_RC = count; } @@ -97,7 +97,7 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_RC; } -FORCE_INLINE static uint32_t HAL_timer_get_current_count(const uint8_t timer_num) { +FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) { const tTimerConfig *pConfig = &TimerConfig[timer_num]; return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV; } diff --git a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h index 163eb498da..f23de80c3f 100644 --- a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h +++ b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h @@ -51,7 +51,6 @@ typedef uint16_t hal_timer_t; #define TEMP_TIMER_NUM 2 // index of timer to use for temperature #define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts - #define HAL_TIMER_RATE (F_CPU) // frequency of timers peripherals #define STEPPER_TIMER_PRESCALE 36 // prescaler for setting stepper timer, 2Mhz #define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) @@ -60,11 +59,11 @@ typedef uint16_t hal_timer_t; #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency -#define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt (STEP_TIMER_NUM) -#define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt (STEP_TIMER_NUM) +#define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM) +#define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM) -#define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt (TEMP_TIMER_NUM) -#define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt (TEMP_TIMER_NUM) +#define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM) +#define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM) #define HAL_ENABLE_ISRs() do { if (thermalManager.in_temp_isr)DISABLE_TEMPERATURE_INTERRUPT(); else ENABLE_TEMPERATURE_INTERRUPT(); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0) // TODO change this @@ -92,7 +91,7 @@ static HardwareTimer TempTimer(TEMP_TIMER_NUM); // Public functions // -------------------------------------------------------------------------- -void HAL_timer_start (uint8_t timer_num, uint32_t frequency); +void HAL_timer_start(uint8_t timer_num, uint32_t frequency); void HAL_timer_enable_interrupt(uint8_t timer_num); void HAL_timer_disable_interrupt(uint8_t timer_num); @@ -107,26 +106,26 @@ void HAL_timer_disable_interrupt(uint8_t timer_num); * Todo: Look at that possibility later. */ -FORCE_INLINE static void HAL_timer_set_count (uint8_t timer_num, uint32_t count) { +FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const hal_timer_t count) { switch (timer_num) { case STEP_TIMER_NUM: StepperTimer.pause(); - StepperTimer.setCompare (STEP_TIMER_CHAN, count); - StepperTimer.refresh (); - StepperTimer.resume (); + StepperTimer.setCompare(STEP_TIMER_CHAN, count); + StepperTimer.refresh(); + StepperTimer.resume(); break; case TEMP_TIMER_NUM: TempTimer.pause(); - TempTimer.setCompare (TEMP_TIMER_CHAN, count); - TempTimer.refresh (); - TempTimer.resume (); + TempTimer.setCompare(TEMP_TIMER_CHAN, count); + TempTimer.refresh(); + TempTimer.resume(); break; default: break; } } -FORCE_INLINE static hal_timer_t HAL_timer_get_count (uint8_t timer_num) { +FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { hal_timer_t temp; switch (timer_num) { case STEP_TIMER_NUM: @@ -142,7 +141,7 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count (uint8_t timer_num) { return temp; } -FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(uint8_t timer_num) { +FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) { hal_timer_t temp; switch (timer_num) { case STEP_TIMER_NUM: @@ -159,9 +158,9 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(uint8_t timer_num) { } -//void HAL_timer_isr_prologue (uint8_t timer_num); +//void HAL_timer_isr_prologue (const uint8_t timer_num); -FORCE_INLINE static void HAL_timer_isr_prologue(uint8_t timer_num) { +FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) { switch (timer_num) { case STEP_TIMER_NUM: StepperTimer.pause(); 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 65ea165615..0327ce88fd 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h @@ -75,23 +75,23 @@ typedef uint32_t hal_timer_t; void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); -FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const uint32_t count) { - switch(timer_num) { +FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const hal_timer_t count) { + switch (timer_num) { case 0: FTM0_C0V = count; break; case 1: FTM1_C0V = count; break; } } FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { - switch(timer_num) { + switch (timer_num) { case 0: return FTM0_C0V; case 1: return FTM1_C0V; } return 0; } -FORCE_INLINE static uint32_t HAL_timer_get_current_count(const uint8_t timer_num) { - switch(timer_num) { +FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) { + switch (timer_num) { case 0: return FTM0_CNT; case 1: return FTM1_CNT; } diff --git a/Marlin/src/core/macros.h b/Marlin/src/core/macros.h index aed0850215..ea6ef387b5 100644 --- a/Marlin/src/core/macros.h +++ b/Marlin/src/core/macros.h @@ -44,8 +44,7 @@ #define _O3 __attribute__((optimize("O3"))) // Clock speed factors -#define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 -#define INT0_PRESCALER 8 +#define CYCLES_PER_MICROSECOND (F_CPU / 1000000L) // 16 or 20 on AVR // Highly granular delays for step pulses, etc. #define DELAY_0_NOP NOOP From 82ef6b52420b47004fea9c34c6bf445157a7f9bb Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 Dec 2017 19:59:12 -0600 Subject: [PATCH 2/4] Add an option to specify "pulse" timer --- Marlin/src/HAL/HAL_AVR/HAL_AVR.h | 4 ++++ Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h | 3 +++ Marlin/src/HAL/HAL_LPC1768/HAL_timers.h | 3 +++ .../src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h | 3 +++ .../HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h | 3 +++ Marlin/src/module/stepper.cpp | 18 +++++++++--------- 6 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h index 8c8c09b82f..b4b908155e 100644 --- a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h +++ b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h @@ -109,6 +109,10 @@ extern "C" { #define STEPPER_TIMER_PRESCALE 8 #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double +#define PULSE_TIMER_NUM TEMP_TIMER_NUM +#define TIMER_COUNTER_0 TCNT0 +#define PULSE_TIMER_PRESCALE 8 + #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 03ee584be9..c85a8a7ee3 100644 --- a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h +++ b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h @@ -64,6 +64,9 @@ typedef uint32_t hal_timer_t; #define HAL_STEP_TIMER_ISR void TC3_Handler() #define HAL_TEMP_TIMER_ISR void TC4_Handler() +#define PULSE_TIMER_NUM STEP_TIMER_NUM +#define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE + // -------------------------------------------------------------------------- // Types // -------------------------------------------------------------------------- diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h b/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h index 52ee1f7d36..fe5fb472ac 100644 --- a/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h +++ b/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h @@ -53,6 +53,9 @@ typedef uint32_t hal_timer_t; #define HAL_TEMP_TIMER_RATE 1000000 #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency +#define PULSE_TIMER_NUM STEP_TIMER_NUM +#define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE + #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM) #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM) #define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM) diff --git a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h index f23de80c3f..85c0f18661 100644 --- a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h +++ b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h @@ -56,6 +56,9 @@ typedef uint16_t hal_timer_t; #define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // 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 +#define PULSE_TIMER_NUM STEP_TIMER_NUM +#define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE + #define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz #define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency 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 0327ce88fd..f804ab2209 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h @@ -57,6 +57,9 @@ typedef uint32_t hal_timer_t; #define STEPPER_TIMER STEP_TIMER_NUM // Alias? #define STEPPER_TIMER_PRESCALE 0 // Not defined anywhere else! +#define PULSE_TIMER_NUM STEP_TIMER_NUM +#define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE + #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) diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 77da0b26fd..c3f03cdbe3 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -564,7 +564,7 @@ void Stepper::isr() { * 10µs = 160 or 200 cycles. */ #if EXTRA_CYCLES_XYZE > 20 - hal_timer_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM); + hal_timer_t pulse_start = HAL_timer_get_current_count(PULSE_TIMER_NUM); #endif #if HAS_X_STEP @@ -596,8 +596,8 @@ void Stepper::isr() { // For minimum pulse time wait before stopping pulses #if EXTRA_CYCLES_XYZE > 20 - while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ } - pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM); + while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_current_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ } + pulse_start = HAL_timer_get_current_count(PULSE_TIMER_NUM); #elif EXTRA_CYCLES_XYZE > 0 DELAY_NOPS(EXTRA_CYCLES_XYZE); #endif @@ -637,7 +637,7 @@ void Stepper::isr() { // For minimum pulse time wait after stopping pulses also #if EXTRA_CYCLES_XYZE > 20 - if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ } + if (i) while (EXTRA_CYCLES_XYZE > (uint32_t)(HAL_timer_get_current_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ } #elif EXTRA_CYCLES_XYZE > 0 if (i) DELAY_NOPS(EXTRA_CYCLES_XYZE); #endif @@ -818,7 +818,7 @@ void Stepper::isr() { for (uint8_t i = step_loops; i--;) { #if EXTRA_CYCLES_E > 20 - hal_timer_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM); + hal_timer_t pulse_start = HAL_timer_get_current_count(PULSE_TIMER_NUM); #endif START_E_PULSE(0); @@ -837,8 +837,8 @@ void Stepper::isr() { // For minimum pulse time wait before stopping pulses #if EXTRA_CYCLES_E > 20 - while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ } - pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM); + while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_current_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ } + pulse_start = HAL_timer_get_current_count(PULSE_TIMER_NUM); #elif EXTRA_CYCLES_E > 0 DELAY_NOPS(EXTRA_CYCLES_E); #endif @@ -859,7 +859,7 @@ void Stepper::isr() { // For minimum pulse time wait before looping #if EXTRA_CYCLES_E > 20 - if (i) while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ } + if (i) while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_current_count(PULSE_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ } #elif EXTRA_CYCLES_E > 0 if (i) DELAY_NOPS(EXTRA_CYCLES_E); #endif @@ -1299,7 +1299,7 @@ void Stepper::report_positions() { #if EXTRA_CYCLES_BABYSTEP > 20 #define _SAVE_START const hal_timer_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM) - #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ } + #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (PULSE_TIMER_PRESCALE)) { /* nada */ } #else #define _SAVE_START NOOP #if EXTRA_CYCLES_BABYSTEP > 0 From 6149b82119d2bedc8197e18f034f58210f633025 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 Dec 2017 19:57:25 -0600 Subject: [PATCH 3/4] Allow setting current timer counter --- Marlin/src/HAL/HAL_AVR/HAL_AVR.h | 35 ++++++++++--------- Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h | 5 +++ Marlin/src/HAL/HAL_LPC1768/HAL_timers.h | 7 ++++ .../src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h | 8 ++++- .../HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h | 7 ++++ 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h index b4b908155e..2d59323051 100644 --- a/Marlin/src/HAL/HAL_AVR/HAL_AVR.h +++ b/Marlin/src/HAL/HAL_AVR/HAL_AVR.h @@ -100,17 +100,23 @@ extern "C" { // timers -#define STEP_TIMER_NUM OCR1A -#define TEMP_TIMER_NUM 0 -#define TEMP_TIMER_FREQUENCY (F_CPU / 64.0 / 256.0) - #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 8 #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double -#define PULSE_TIMER_NUM TEMP_TIMER_NUM +#define TEMP_TIMER_FREQUENCY ((F_CPU) / 64.0 / 256.0) + +#define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE +#define STEPPER_TIMER_PRESCALE 8 + +#define STEP_TIMER_NUM 1 +#define TIMER_OCR_1 OCR1A +#define TIMER_COUNTER_1 TCNT1 + +#define TEMP_TIMER_NUM 0 +#define TIMER_OCR_0 OCR0A #define TIMER_COUNTER_0 TCNT0 + +#define PULSE_TIMER_NUM TEMP_TIMER_NUM #define PULSE_TIMER_PRESCALE 8 #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A) @@ -119,17 +125,14 @@ extern "C" { #define ENABLE_TEMPERATURE_INTERRUPT() SBI(TIMSK0, OCIE0B) #define DISABLE_TEMPERATURE_INTERRUPT() CBI(TIMSK0, OCIE0B) -//void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency); -#define HAL_timer_start(timer_num,frequency) +#define HAL_timer_start(timer_num, frequency) -#define HAL_timer_get_count(timer) timer +#define _CAT(a, ...) a ## __VA_ARGS__ +#define HAL_timer_set_count(timer, count) (_CAT(TIMER_OCR_, timer) = count) +#define HAL_timer_get_count(timer) _CAT(TIMER_OCR_, timer) +#define HAL_timer_set_current_count(timer, count) (_CAT(TIMER_COUNTER_, timer) = count) +#define HAL_timer_get_current_count(timer) _CAT(TIMER_COUNTER_, timer) -//void HAL_timer_set_count(const uint8_t timer_num, const uint16_t count); -#define HAL_timer_set_count(timer, count) timer = (count) - -#define HAL_timer_get_current_count(timer) timer - -//void HAL_timer_isr_prologue(const uint8_t timer_num); #define HAL_timer_isr_prologue(timer_num) #define HAL_STEP_TIMER_ISR ISR(TIMER1_COMPA_vect) diff --git a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h index c85a8a7ee3..b93e80b7d6 100644 --- a/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h +++ b/Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h @@ -100,6 +100,11 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_RC; } +FORCE_INLINE static void HAL_timer_set_current_count(const uint8_t timer_num, const hal_timer_t count) { + const tTimerConfig *pConfig = &TimerConfig[timer_num]; + pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV = count; +} + FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) { const tTimerConfig *pConfig = &TimerConfig[timer_num]; return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV; diff --git a/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h b/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h index fe5fb472ac..15085efa60 100644 --- a/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h +++ b/Marlin/src/HAL/HAL_LPC1768/HAL_timers.h @@ -103,6 +103,13 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { return 0; } +FORCE_INLINE static void HAL_timer_set_current_count(const uint8_t timer_num, const hal_timer_t count) { + switch (timer_num) { + case 0: LPC_TIM0->TC = count; break; + case 1: LPC_TIM1->TC = count; break; + } +} + FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) { switch (timer_num) { case 0: return LPC_TIM0->TC; diff --git a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h index 85c0f18661..541a214a7a 100644 --- a/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h +++ b/Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h @@ -144,6 +144,13 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { return temp; } +FORCE_INLINE static void HAL_timer_set_current_count(const uint8_t timer_num, const hal_timer_t count) { + switch (timer_num) { + case STEP_TIMER_NUM: StepperTimer.setCount(count); break; + case TEMP_TIMER_NUM: TempTimer.setCount(count); break; + } +} + FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) { hal_timer_t temp; switch (timer_num) { @@ -160,7 +167,6 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_ return temp; } - //void HAL_timer_isr_prologue (const uint8_t timer_num); FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) { 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 f804ab2209..3675ea60bd 100644 --- a/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h +++ b/Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h @@ -93,6 +93,13 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) { return 0; } +FORCE_INLINE static void HAL_timer_set_current_count(const uint8_t timer_num, const hal_timer_t count) { + switch (timer_num) { + case 0: FTM0_CNT = count; + case 1: FTM1_CNT = count; + } +} + FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) { switch (timer_num) { case 0: return FTM0_CNT; From 5cf6a062e315e7c0936f60ba6bf11d7f8fb1f20c Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 Dec 2017 18:41:15 -0600 Subject: [PATCH 4/4] Reset timer count before first block step --- Marlin/src/module/stepper.cpp | 21 +++++++++++++++------ Marlin/src/module/stepper.h | 3 --- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index c3f03cdbe3..cbb227d936 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -408,10 +408,13 @@ void Stepper::isr() { } // If there is no current block, attempt to pop one from the buffer + bool first_step = false; if (!current_block) { // Anything in the buffer? if ((current_block = planner.get_current_block())) { trapezoid_generator_reset(); + HAL_timer_set_current_count(STEP_TIMER_NUM, 0); + first_step = true; // Initialize Bresenham counters to 1/2 the ceiling counter_X = counter_Y = counter_Z = counter_E = -(current_block->step_event_count >> 1); @@ -666,12 +669,18 @@ void Stepper::isr() { // Calculate new timer value if (step_events_completed <= (uint32_t)current_block->accelerate_until) { - #ifdef CPU_32_BIT - MultiU32X24toH32(acc_step_rate, acceleration_time, current_block->acceleration_rate); - #else - MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate); - #endif - acc_step_rate += current_block->initial_rate; + if (first_step) { + acc_step_rate = current_block->initial_rate; + acceleration_time = 0; + } + else { + #ifdef CPU_32_BIT + MultiU32X24toH32(acc_step_rate, acceleration_time, current_block->acceleration_rate); + #else + MultiU24X32toH16(acc_step_rate, acceleration_time, current_block->acceleration_rate); + #endif + acc_step_rate += current_block->initial_rate; + } // upper limit NOMORE(acc_step_rate, current_block->nominal_rate); diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index b15d699872..b1e73bf053 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -362,9 +362,6 @@ class Stepper { OCR1A_nominal = calc_timer_interval(current_block->nominal_rate); // make a note of the number of step loops required at nominal speed step_loops_nominal = step_loops; - acc_step_rate = current_block->initial_rate; - acceleration_time = calc_timer_interval(acc_step_rate); - _NEXT_ISR(acceleration_time); #if ENABLED(LIN_ADVANCE) if (current_block->use_advance_lead) {