From 6149b82119d2bedc8197e18f034f58210f633025 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 9 Dec 2017 19:57:25 -0600 Subject: [PATCH] Allow setting current timer counter --- Marlin/src/HAL/HAL_AVR/HAL_AVR.h | 33 ++++++++++--------- 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, 44 insertions(+), 16 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_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double + +#define TEMP_TIMER_FREQUENCY ((F_CPU) / 64.0 / 256.0) + #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 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_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) +#define HAL_timer_start(timer_num, frequency) -#define HAL_timer_get_current_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_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;