Merge pull request #8736 from thinkyhead/bf2_first_step_fix

[2.0.x] Reset timer count before first block step
This commit is contained in:
Scott Lahteine 2017-12-10 19:02:22 -06:00 committed by GitHub
commit 11f9c253e0
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 108 additions and 58 deletions

View file

@ -100,30 +100,39 @@ 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 INT0_PRESCALER
#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 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)
#define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)
#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)
//void HAL_timer_set_count(const uint8_t timer_num, const uint16_t count);
#define HAL_timer_set_count(timer, count) timer = (count)
#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)
#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)

View file

@ -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
// --------------------------------------------------------------------------
@ -87,7 +90,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 +100,12 @@ 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 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;
}

View file

@ -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)
@ -100,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;

View file

@ -51,12 +51,14 @@ 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)
#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
@ -107,7 +109,7 @@ 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();
@ -126,7 +128,7 @@ FORCE_INLINE static void HAL_timer_set_count (uint8_t timer_num, uint32_t count)
}
}
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 +144,14 @@ 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 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) {
case STEP_TIMER_NUM:
@ -158,10 +167,9 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(uint8_t timer_num) {
return temp;
}
//void HAL_timer_isr_prologue (const uint8_t timer_num);
//void HAL_timer_isr_prologue (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();

View file

@ -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)
@ -75,7 +78,7 @@ 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) {
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;
@ -90,7 +93,14 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
return 0;
}
FORCE_INLINE static uint32_t HAL_timer_get_current_count(const uint8_t timer_num) {
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;
case 1: return FTM1_CNT;

View file

@ -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

View file

@ -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);
@ -564,7 +567,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 +599,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 +640,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
@ -666,12 +669,18 @@ void Stepper::isr() {
// Calculate new timer value
if (step_events_completed <= (uint32_t)current_block->accelerate_until) {
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);
@ -818,7 +827,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 +846,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 +868,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 +1308,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

View file

@ -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) {