Followup to HAL_timer_restrain

Followup to #9985
This commit is contained in:
Scott Lahteine 2018-03-07 22:18:13 -06:00
parent 03e3011332
commit 98d48fc731
8 changed files with 21 additions and 21 deletions

View File

@ -150,7 +150,7 @@ extern "C" {
#define _CAT(a, ...) a ## __VA_ARGS__ #define _CAT(a, ...) a ## __VA_ARGS__
#define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare) #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
#define HAL_timer_restrain(timer, interval_us) NOLESS(_CAT(TIMER_OCR_, timer), _CAT(TIMER_COUNTER_, timer) + interval_us * HAL_TICKS_PER_US) #define HAL_timer_restrain(timer, interval_ticks) NOLESS(_CAT(TIMER_OCR_, timer), _CAT(TIMER_COUNTER_, timer) + interval_ticks)
#define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer) #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
#define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer) #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)

View File

@ -109,8 +109,8 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV; return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
} }
FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_us) { FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_us * HAL_TICKS_PER_US; const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp); if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
} }

View File

@ -120,8 +120,8 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
return 0; return 0;
} }
FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_us) { FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_us * HAL_TICKS_PER_US; const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp); if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
} }

View File

@ -154,8 +154,8 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
} }
} }
FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_us) { FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_us * HAL_TICKS_PER_US; const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp); if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
} }

View File

@ -137,8 +137,8 @@ uint32_t HAL_timer_get_count(const uint8_t timer_num) {
return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef); return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
} }
void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_us) { void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_us * HAL_TICKS_PER_US; const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp); if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
} }

View File

@ -96,7 +96,7 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare); void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare);
hal_timer_t HAL_timer_get_compare(const uint8_t timer_num); hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
uint32_t HAL_timer_get_count(const uint8_t timer_num); uint32_t HAL_timer_get_count(const uint8_t timer_num);
void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_us); void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks);
void HAL_timer_isr_prologue(const uint8_t timer_num); void HAL_timer_isr_prologue(const uint8_t timer_num);

View File

@ -105,8 +105,8 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
return 0; return 0;
} }
FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_us) { FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_ticks) {
const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_us * HAL_TICKS_PER_US; const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_ticks;
if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp); if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
} }

View File

@ -365,8 +365,8 @@ void Stepper::isr() {
_NEXT_ISR(ocr_val); _NEXT_ISR(ocr_val);
#if DISABLED(LIN_ADVANCE) #if DISABLED(LIN_ADVANCE)
HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL); HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
HAL_ENABLE_ISRs(); // re-enable ISRs HAL_ENABLE_ISRs();
#endif #endif
return; return;
@ -419,14 +419,14 @@ void Stepper::isr() {
if (current_block->steps[Z_AXIS] > 0) { if (current_block->steps[Z_AXIS] > 0) {
enable_Z(); enable_Z();
_NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
HAL_ENABLE_ISRs(); // re-enable ISRs HAL_ENABLE_ISRs();
return; return;
} }
#endif #endif
} }
else { else {
_NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz _NEXT_ISR(HAL_STEPPER_TIMER_RATE / 1000); // Run at slow speed - 1 KHz
HAL_ENABLE_ISRs(); // re-enable ISRs HAL_ENABLE_ISRs();
return; return;
} }
} }
@ -727,7 +727,8 @@ void Stepper::isr() {
} }
#if DISABLED(LIN_ADVANCE) #if DISABLED(LIN_ADVANCE)
HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL); // Make sure stepper ISR doesn't monopolize the CPU
HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
#endif #endif
// If current block is finished, reset pointer // If current block is finished, reset pointer
@ -736,7 +737,7 @@ void Stepper::isr() {
planner.discard_current_block(); planner.discard_current_block();
} }
#if DISABLED(LIN_ADVANCE) #if DISABLED(LIN_ADVANCE)
HAL_ENABLE_ISRs(); // re-enable ISRs HAL_ENABLE_ISRs();
#endif #endif
} }
@ -889,9 +890,8 @@ void Stepper::isr() {
nextMainISR = 0; nextMainISR = 0;
} }
// Don't run the ISR faster than possible // Make sure stepper ISR doesn't monopolize the CPU
// Make sure stepper interrupt does not monopolise CPU by adjusting compare to give about 8µs room HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL * HAL_TICKS_PER_US);
HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL);
// Restore original ISR settings // Restore original ISR settings
HAL_ENABLE_ISRs(); HAL_ENABLE_ISRs();