Apply fixes for DUE

Alternative to #7882. If F_CPU is greater than 1000 it can be evenly divided by 8. Over 10000, 16; over 100000, 32; over 1 million, 64; etc.
This commit is contained in:
Scott Lahteine 2017-10-07 13:34:25 -05:00
parent 4b36a542e0
commit 8315a8a716
8 changed files with 20 additions and 21 deletions

View file

@ -102,10 +102,10 @@ extern "C" {
#define TEMP_TIMER_NUM 0 #define TEMP_TIMER_NUM 0
#define TEMP_TIMER_FREQUENCY (F_CPU / 64.0 / 256.0) #define TEMP_TIMER_FREQUENCY (F_CPU / 64.0 / 256.0)
#define HAL_TIMER_RATE ((F_CPU) / 8.0) #define HAL_TIMER_RATE ((F_CPU) / 8) // i.e., 2MHz or 2.5MHz
#define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE
#define STEPPER_TIMER_PRESCALE INT0_PRESCALER #define STEPPER_TIMER_PRESCALE INT0_PRESCALER
#define HAL_TICKS_PER_US (((F_CPU) / 8) / 1000000) // Can not be of type double #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // Cannot be of type double
#define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A) #define ENABLE_STEPPER_DRIVER_INTERRUPT() SBI(TIMSK1, OCIE1A)
#define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A) #define DISABLE_STEPPER_DRIVER_INTERRUPT() CBI(TIMSK1, OCIE1A)

View file

@ -46,7 +46,7 @@
#define STEP_TIMER_NUM 3 // index of timer to use for stepper #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 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 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_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 #define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us

View file

@ -33,22 +33,22 @@
void HAL_timer_init(void) { void HAL_timer_init(void) {
SBI(LPC_SC->PCONP, 1); // power on timer0 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 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) { void HAL_timer_start(const uint8_t timer_num, const uint32_t frequency) {
switch (timer_num) { switch (timer_num) {
case 0: case 0:
LPC_TIM0->MCR = 3; // Match on MR0, reset on MR0 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 LPC_TIM0->TCR = _BV(0); // enable
break; break;
case 1: case 1:
LPC_TIM1->MCR = 3; 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); LPC_TIM1->TCR = _BV(0);
break; break;
default: break; default: break;

View file

@ -93,14 +93,14 @@ Timer_clock4: Prescaler 128 -> 656.25kHz
* TODO: Calculate Timer prescale value, so we get the 32bit to adjust * 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) { switch (timer_num) {
case STEP_TIMER_NUM: case STEP_TIMER_NUM:
StepperTimer.pause(); StepperTimer.pause();
StepperTimer.setCount(0); StepperTimer.setCount(0);
StepperTimer.setPrescaleFactor(STEPPER_TIMER_PRESCALE); StepperTimer.setPrescaleFactor(STEPPER_TIMER_PRESCALE);
StepperTimer.setOverflow(0xFFFF); 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.refresh();
StepperTimer.resume(); StepperTimer.resume();
break; break;
@ -109,14 +109,14 @@ void HAL_timer_start (uint8_t timer_num, uint32_t frequency) {
TempTimer.setCount(0); TempTimer.setCount(0);
TempTimer.setPrescaleFactor(TEMP_TIMER_PRESCALE); TempTimer.setPrescaleFactor(TEMP_TIMER_PRESCALE);
TempTimer.setOverflow(0xFFFF); 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.refresh();
TempTimer.resume(); TempTimer.resume();
break; break;
} }
} }
void HAL_timer_enable_interrupt (uint8_t timer_num) { void HAL_timer_enable_interrupt(uint8_t timer_num) {
switch (timer_num) { switch (timer_num) {
case STEP_TIMER_NUM: case STEP_TIMER_NUM:
StepperTimer.attachInterrupt(STEP_TIMER_CHAN, stepTC_Handler); 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) { switch (timer_num) {
case STEP_TIMER_NUM: case STEP_TIMER_NUM:
StepperTimer.detachInterrupt(STEP_TIMER_CHAN); StepperTimer.detachInterrupt(STEP_TIMER_CHAN);

View file

@ -59,7 +59,7 @@
#define HAL_TIMER_RATE (FTM0_TIMER_RATE) #define HAL_TIMER_RATE (FTM0_TIMER_RATE)
#define HAL_STEPPER_TIMER_RATE HAL_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 #define TEMP_TIMER_FREQUENCY 1000

View file

@ -224,7 +224,7 @@ inline void servo_probe_test() {
* - Machine continues to operate * - Machine continues to operate
* - Reports changes to endstops * - Reports changes to endstops
* - Toggles LED_PIN when an endstop changes * - 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 * M43 T - Toggle pin(s) and report which pin is being toggled
* S<pin> - Start Pin number. If not given, will default to 0 * S<pin> - Start Pin number. If not given, will default to 0

View file

@ -207,7 +207,7 @@
/** /**
* Hidden options for developer * 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 #ifndef STEP_DOUBLER_FREQUENCY
#if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE) #if ENABLED(ADVANCE) || ENABLED(LIN_ADVANCE)
#define STEP_DOUBLER_FREQUENCY 60000 // Hz #define STEP_DOUBLER_FREQUENCY 60000 // Hz

View file

@ -303,10 +303,9 @@ class Stepper {
#ifdef CPU_32_BIT #ifdef CPU_32_BIT
// In case of high-performance processor, it is able to calculate in real-time // In case of high-performance processor, it is able to calculate in real-time
timer = (uint32_t)(HAL_STEPPER_TIMER_RATE) / step_rate; constexpr uint32_t MIN_TIME_PER_STEP = (HAL_STEPPER_TIMER_RATE) / ((STEP_DOUBLER_FREQUENCY) * 2);
if (timer < (HAL_STEPPER_TIMER_RATE / (STEP_DOUBLER_FREQUENCY * 2))) { // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen) timer = uint32_t(HAL_STEPPER_TIMER_RATE) / step_rate;
timer = (HAL_STEPPER_TIMER_RATE / (STEP_DOUBLER_FREQUENCY * 2)); NOLESS(timer, MIN_TIME_PER_STEP); // (STEP_DOUBLER_FREQUENCY * 2 kHz - this should never happen)
}
#else #else
NOLESS(step_rate, F_CPU / 500000); NOLESS(step_rate, F_CPU / 500000);
step_rate -= F_CPU / 500000; // Correct for minimal speed step_rate -= F_CPU / 500000; // Correct for minimal speed