Browse Source

Merge pull request #8725 from thinkyhead/bf2_hal_timer_for_advance

[2.0.x] TCNT0 => HAL_timer_get_current_count
Scott Lahteine 7 years ago
parent
commit
a2ac452ef9
No account linked to committer's email address
3 changed files with 26 additions and 25 deletions
  1. 22
    21
      Marlin/src/module/stepper.cpp
  2. 3
    3
      Marlin/src/module/stepper.h
  3. 1
    1
      Marlin/src/module/temperature.cpp

+ 22
- 21
Marlin/src/module/stepper.cpp View File

118
   constexpr hal_timer_t ADV_NEVER = HAL_TIMER_TYPE_MAX;
118
   constexpr hal_timer_t ADV_NEVER = HAL_TIMER_TYPE_MAX;
119
 
119
 
120
   hal_timer_t Stepper::nextMainISR = 0,
120
   hal_timer_t Stepper::nextMainISR = 0,
121
-         Stepper::nextAdvanceISR = ADV_NEVER,
122
-         Stepper::eISR_Rate = ADV_NEVER;
121
+              Stepper::nextAdvanceISR = ADV_NEVER,
122
+              Stepper::eISR_Rate = ADV_NEVER;
123
 
123
 
124
   volatile int Stepper::e_steps[E_STEPPERS];
124
   volatile int Stepper::e_steps[E_STEPPERS];
125
   int Stepper::final_estep_rate,
125
   int Stepper::final_estep_rate,
152
   long Stepper::counter_m[MIXING_STEPPERS];
152
   long Stepper::counter_m[MIXING_STEPPERS];
153
 #endif
153
 #endif
154
 
154
 
155
-hal_timer_t Stepper::acc_step_rate; // needed for deceleration start point
156
 uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
155
 uint8_t Stepper::step_loops, Stepper::step_loops_nominal;
157
-hal_timer_t Stepper::OCR1A_nominal;
156
+
157
+hal_timer_t Stepper::OCR1A_nominal,
158
+            Stepper::acc_step_rate; // needed for deceleration start point
158
 
159
 
159
 volatile long Stepper::endstops_trigsteps[XYZ];
160
 volatile long Stepper::endstops_trigsteps[XYZ];
160
 
161
 
557
     /**
558
     /**
558
      * If a minimum pulse time was specified get the timer 0 value.
559
      * If a minimum pulse time was specified get the timer 0 value.
559
      *
560
      *
560
-     * TCNT0 has an 8x prescaler, so it increments every 8 cycles.
561
+     * On AVR the TCNT0 timer has an 8x prescaler, so it increments every 8 cycles.
561
      * That's every 0.5µs on 16MHz and every 0.4µs on 20MHz.
562
      * That's every 0.5µs on 16MHz and every 0.4µs on 20MHz.
562
      * 20 counts of TCNT0 -by itself- is a good pulse delay.
563
      * 20 counts of TCNT0 -by itself- is a good pulse delay.
563
      * 10µs = 160 or 200 cycles.
564
      * 10µs = 160 or 200 cycles.
564
      */
565
      */
565
     #if EXTRA_CYCLES_XYZE > 20
566
     #if EXTRA_CYCLES_XYZE > 20
566
-      uint32_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM);
567
+      hal_timer_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM);
567
     #endif
568
     #endif
568
 
569
 
569
     #if HAS_X_STEP
570
     #if HAS_X_STEP
676
     NOMORE(acc_step_rate, current_block->nominal_rate);
677
     NOMORE(acc_step_rate, current_block->nominal_rate);
677
 
678
 
678
     // step_rate to timer interval
679
     // step_rate to timer interval
679
-    const hal_timer_t timer = calc_timer(acc_step_rate);
680
+    const hal_timer_t interval = calc_timer_interval(acc_step_rate);
680
 
681
 
681
-    SPLIT(timer);  // split step into multiple ISRs if larger than  ENDSTOP_NOMINAL_OCR_VAL
682
+    SPLIT(interval);  // split step into multiple ISRs if larger than  ENDSTOP_NOMINAL_OCR_VAL
682
     _NEXT_ISR(ocr_val);
683
     _NEXT_ISR(ocr_val);
683
 
684
 
684
-    acceleration_time += timer;
685
+    acceleration_time += interval;
685
 
686
 
686
     #if ENABLED(LIN_ADVANCE)
687
     #if ENABLED(LIN_ADVANCE)
687
 
688
 
693
           current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
694
           current_estep_rate[TOOL_E_INDEX] = ((uint32_t)acc_step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
694
         #endif
695
         #endif
695
       }
696
       }
696
-      eISR_Rate = adv_rate(e_steps[TOOL_E_INDEX], timer, step_loops);
697
+      eISR_Rate = adv_rate(e_steps[TOOL_E_INDEX], interval, step_loops);
697
 
698
 
698
     #endif // LIN_ADVANCE
699
     #endif // LIN_ADVANCE
699
   }
700
   }
713
       step_rate = current_block->final_rate;
714
       step_rate = current_block->final_rate;
714
 
715
 
715
     // step_rate to timer interval
716
     // step_rate to timer interval
716
-    const hal_timer_t timer = calc_timer(step_rate);
717
+    const hal_timer_t interval = calc_timer_interval(step_rate);
717
 
718
 
718
-    SPLIT(timer);  // split step into multiple ISRs if larger than  ENDSTOP_NOMINAL_OCR_VAL
719
+    SPLIT(interval);  // split step into multiple ISRs if larger than  ENDSTOP_NOMINAL_OCR_VAL
719
     _NEXT_ISR(ocr_val);
720
     _NEXT_ISR(ocr_val);
720
-    deceleration_time += timer;
721
+    deceleration_time += interval;
721
 
722
 
722
     #if ENABLED(LIN_ADVANCE)
723
     #if ENABLED(LIN_ADVANCE)
723
 
724
 
729
           current_estep_rate[TOOL_E_INDEX] = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
730
           current_estep_rate[TOOL_E_INDEX] = ((uint32_t)step_rate * current_block->abs_adv_steps_multiplier8) >> 17;
730
         #endif
731
         #endif
731
       }
732
       }
732
-      eISR_Rate = adv_rate(e_steps[TOOL_E_INDEX], timer, step_loops);
733
+      eISR_Rate = adv_rate(e_steps[TOOL_E_INDEX], interval, step_loops);
733
 
734
 
734
     #endif // LIN_ADVANCE
735
     #endif // LIN_ADVANCE
735
   }
736
   }
754
     #ifdef CPU_32_BIT
755
     #ifdef CPU_32_BIT
755
       // Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
756
       // Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
756
       hal_timer_t stepper_timer_count = HAL_timer_get_count(STEP_TIMER_NUM),
757
       hal_timer_t stepper_timer_count = HAL_timer_get_count(STEP_TIMER_NUM),
757
-                     stepper_timer_current_count = HAL_timer_get_current_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
758
+                  stepper_timer_current_count = HAL_timer_get_current_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
758
       HAL_timer_set_count(STEP_TIMER_NUM, max(stepper_timer_count, stepper_timer_current_count));
759
       HAL_timer_set_count(STEP_TIMER_NUM, max(stepper_timer_count, stepper_timer_current_count));
759
     #else
760
     #else
760
       NOLESS(OCR1A, TCNT1 + 16);
761
       NOLESS(OCR1A, TCNT1 + 16);
817
     for (uint8_t i = step_loops; i--;) {
818
     for (uint8_t i = step_loops; i--;) {
818
 
819
 
819
       #if EXTRA_CYCLES_E > 20
820
       #if EXTRA_CYCLES_E > 20
820
-        uint32_t pulse_start = TCNT0;
821
+        hal_timer_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM);
821
       #endif
822
       #endif
822
 
823
 
823
       START_E_PULSE(0);
824
       START_E_PULSE(0);
836
 
837
 
837
       // For minimum pulse time wait before stopping pulses
838
       // For minimum pulse time wait before stopping pulses
838
       #if EXTRA_CYCLES_E > 20
839
       #if EXTRA_CYCLES_E > 20
839
-        while (EXTRA_CYCLES_E > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
840
-        pulse_start = TCNT0;
840
+        while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ }
841
+        pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM);
841
       #elif EXTRA_CYCLES_E > 0
842
       #elif EXTRA_CYCLES_E > 0
842
         DELAY_NOPS(EXTRA_CYCLES_E);
843
         DELAY_NOPS(EXTRA_CYCLES_E);
843
       #endif
844
       #endif
858
 
859
 
859
       // For minimum pulse time wait before looping
860
       // For minimum pulse time wait before looping
860
       #if EXTRA_CYCLES_E > 20
861
       #if EXTRA_CYCLES_E > 20
861
-        if (i) while (EXTRA_CYCLES_E > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
862
+        if (i) while (EXTRA_CYCLES_E > (hal_timer_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ }
862
       #elif EXTRA_CYCLES_E > 0
863
       #elif EXTRA_CYCLES_E > 0
863
         if (i) DELAY_NOPS(EXTRA_CYCLES_E);
864
         if (i) DELAY_NOPS(EXTRA_CYCLES_E);
864
       #endif
865
       #endif
1297
   #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
1298
   #define _APPLY_DIR(AXIS, INVERT) AXIS ##_APPLY_DIR(INVERT, true)
1298
 
1299
 
1299
   #if EXTRA_CYCLES_BABYSTEP > 20
1300
   #if EXTRA_CYCLES_BABYSTEP > 20
1300
-    #define _SAVE_START const uint32_t pulse_start = TCNT0
1301
-    #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(TCNT0 - pulse_start) * (INT0_PRESCALER)) { /* nada */ }
1301
+    #define _SAVE_START const hal_timer_t pulse_start = HAL_timer_get_current_count(STEP_TIMER_NUM)
1302
+    #define _PULSE_WAIT while (EXTRA_CYCLES_BABYSTEP > (uint32_t)(HAL_timer_get_current_count(STEP_TIMER_NUM) - pulse_start) * (STEPPER_TIMER_PRESCALE)) { /* nada */ }
1302
   #else
1303
   #else
1303
     #define _SAVE_START NOOP
1304
     #define _SAVE_START NOOP
1304
     #if EXTRA_CYCLES_BABYSTEP > 0
1305
     #if EXTRA_CYCLES_BABYSTEP > 0

+ 3
- 3
Marlin/src/module/stepper.h View File

278
 
278
 
279
   private:
279
   private:
280
 
280
 
281
-    FORCE_INLINE static hal_timer_t calc_timer(hal_timer_t step_rate) {
281
+    FORCE_INLINE static hal_timer_t calc_timer_interval(hal_timer_t step_rate) {
282
       hal_timer_t timer;
282
       hal_timer_t timer;
283
 
283
 
284
       NOMORE(step_rate, MAX_STEP_FREQUENCY);
284
       NOMORE(step_rate, MAX_STEP_FREQUENCY);
359
 
359
 
360
       deceleration_time = 0;
360
       deceleration_time = 0;
361
       // step_rate to timer interval
361
       // step_rate to timer interval
362
-      OCR1A_nominal = calc_timer(current_block->nominal_rate);
362
+      OCR1A_nominal = calc_timer_interval(current_block->nominal_rate);
363
       // make a note of the number of step loops required at nominal speed
363
       // make a note of the number of step loops required at nominal speed
364
       step_loops_nominal = step_loops;
364
       step_loops_nominal = step_loops;
365
       acc_step_rate = current_block->initial_rate;
365
       acc_step_rate = current_block->initial_rate;
366
-      acceleration_time = calc_timer(acc_step_rate);
366
+      acceleration_time = calc_timer_interval(acc_step_rate);
367
       _NEXT_ISR(acceleration_time);
367
       _NEXT_ISR(acceleration_time);
368
 
368
 
369
       #if ENABLED(LIN_ADVANCE)
369
       #if ENABLED(LIN_ADVANCE)

+ 1
- 1
Marlin/src/module/temperature.cpp View File

1588
 /**
1588
 /**
1589
  * Timer 0 is shared with millies so don't change the prescaler.
1589
  * Timer 0 is shared with millies so don't change the prescaler.
1590
  *
1590
  *
1591
- * This ISR uses the compare method so it runs at the base
1591
+ * On AVR this ISR uses the compare method so it runs at the base
1592
  * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
1592
  * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
1593
  * in OCR0B above (128 or halfway between OVFs).
1593
  * in OCR0B above (128 or halfway between OVFs).
1594
  *
1594
  *

Loading…
Cancel
Save