Przeglądaj źródła

HAL function to ensure min stepper interrupt interval (#9985)

Chris Pepper 7 lat temu
rodzic
commit
a1a88ebabc

+ 4
- 0
Marlin/src/HAL/HAL_AVR/HAL_AVR.h Wyświetl plik

@@ -126,6 +126,8 @@ extern "C" {
126 126
 #define HAL_STEPPER_TIMER_RATE  HAL_TIMER_RATE
127 127
 #define STEPPER_TIMER_PRESCALE  8
128 128
 
129
+#define STEP_TIMER_MIN_INTERVAL 8 // minimum time in µs between stepper interrupts
130
+
129 131
 #define STEP_TIMER_NUM          1
130 132
 #define TIMER_OCR_1             OCR1A
131 133
 #define TIMER_COUNTER_1         TCNT1
@@ -148,6 +150,8 @@ extern "C" {
148 150
 
149 151
 #define _CAT(a, ...) a ## __VA_ARGS__
150 152
 #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
153
+#define HAL_timer_restrain(timer, interval_µs) NOLESS(_CAT(TIMER_OCR_, timer), _CAT(TIMER_COUNTER_, timer) + interval_µs * HAL_TICKS_PER_US)
154
+
151 155
 #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
152 156
 #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
153 157
 

+ 2
- 2
Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp Wyświetl plik

@@ -221,7 +221,7 @@ void spiBegin (void) {
221 221
   /** Soft SPI receive byte */
222 222
   uint8_t spiRec() {
223 223
     uint8_t data = 0;
224
-    // no interrupts during byte receive - about 8 us
224
+    // no interrupts during byte receive - about 8µs
225 225
     cli();
226 226
     // output pin high - like sending 0xFF
227 227
     WRITE(MOSI_PIN, HIGH);
@@ -252,7 +252,7 @@ void spiBegin (void) {
252 252
   //------------------------------------------------------------------------------
253 253
   /** Soft SPI send byte */
254 254
   void spiSend(uint8_t data) {
255
-    // no interrupts during byte send - about 8 us
255
+    // no interrupts during byte send - about 8µs
256 256
     cli();
257 257
     for (uint8_t i = 0; i < 8; i++) {
258 258
       WRITE(SCK_PIN, LOW);

+ 7
- 1
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h Wyświetl plik

@@ -50,9 +50,10 @@ typedef uint32_t hal_timer_t;
50 50
 #define HAL_TIMER_RATE         ((F_CPU) / 2)    // frequency of timers peripherals
51 51
 #define STEPPER_TIMER_PRESCALE 1.0              // prescaler for setting stepper frequency
52 52
 #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
53
-#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us
53
+#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
54 54
 
55 55
 #define TEMP_TIMER_FREQUENCY   1000 // temperature interrupt frequency
56
+#define STEP_TIMER_MIN_INTERVAL   8 // minimum time in µs between stepper interrupts
56 57
 
57 58
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
58 59
 #define DISABLE_STEPPER_DRIVER_INTERRUPT()  HAL_timer_disable_interrupt(STEP_TIMER_NUM)
@@ -108,6 +109,11 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
108 109
   return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
109 110
 }
110 111
 
112
+FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_µs) {
113
+  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_µs * HAL_TICKS_PER_US;
114
+  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
115
+}
116
+
111 117
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
112 118
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
113 119
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 8
- 1
Marlin/src/HAL/HAL_LPC1768/HAL_timers.h Wyświetl plik

@@ -49,10 +49,12 @@ typedef uint32_t hal_timer_t;
49 49
 #define HAL_TIMER_RATE         ((SystemCoreClock) / 4)  // frequency of timers peripherals
50 50
 #define STEPPER_TIMER_PRESCALE 1.0              // prescaler for setting stepper frequency
51 51
 #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
52
-#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us
52
+#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
53 53
 #define HAL_TEMP_TIMER_RATE    1000000
54 54
 #define TEMP_TIMER_FREQUENCY   1000 // temperature interrupt frequency
55 55
 
56
+#define STEP_TIMER_MIN_INTERVAL   8 // minimum time in µs between stepper interrupts
57
+
56 58
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
57 59
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
58 60
 
@@ -118,6 +120,11 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
118 120
   return 0;
119 121
 }
120 122
 
123
+FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_µs) {
124
+  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_µs * HAL_TICKS_PER_US;
125
+  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
126
+}
127
+
121 128
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
122 129
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
123 130
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 9
- 2
Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h Wyświetl plik

@@ -67,13 +67,15 @@ typedef uint16_t hal_timer_t;
67 67
 #define HAL_TIMER_RATE         (F_CPU)  // frequency of timers peripherals
68 68
 #define STEPPER_TIMER_PRESCALE 18             // prescaler for setting stepper timer, 4Mhz
69 69
 #define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
70
-#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us
70
+#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
71 71
 
72 72
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
73 73
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
74 74
 
75 75
 #define TEMP_TIMER_PRESCALE     1000 // prescaler for setting Temp timer, 72Khz
76
-#define TEMP_TIMER_FREQUENCY    100 // temperature interrupt frequency
76
+#define TEMP_TIMER_FREQUENCY     100 // temperature interrupt frequency
77
+
78
+#define STEP_TIMER_MIN_INTERVAL    8 // minimum time in µs between stepper interrupts
77 79
 
78 80
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
79 81
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
@@ -152,6 +154,11 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_compare(const uint8_t timer_num) {
152 154
   }
153 155
 }
154 156
 
157
+FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_µs) {
158
+  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_µs * HAL_TICKS_PER_US;
159
+  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
160
+}
161
+
155 162
 FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
156 163
   switch (timer_num) {
157 164
   case STEP_TIMER_NUM:

+ 5
- 0
Marlin/src/HAL/HAL_STM32F7/HAL_timers_STM32F7.cpp Wyświetl plik

@@ -137,6 +137,11 @@ uint32_t HAL_timer_get_count(const uint8_t timer_num) {
137 137
   return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
138 138
 }
139 139
 
140
+void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_µs) {
141
+  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_µs * HAL_TICKS_PER_US;
142
+  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
143
+}
144
+
140 145
 void HAL_timer_isr_prologue(const uint8_t timer_num) {
141 146
   if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
142 147
     __HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);

+ 4
- 1
Marlin/src/HAL/HAL_STM32F7/HAL_timers_STM32F7.h Wyświetl plik

@@ -44,7 +44,7 @@
44 44
 #define HAL_TIMER_RATE         (HAL_RCC_GetSysClockFreq() / 2)  // frequency of timer peripherals
45 45
 #define STEPPER_TIMER_PRESCALE 54            // was 40,prescaler for setting stepper timer, 2Mhz
46 46
 #define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
47
-#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us
47
+#define HAL_TICKS_PER_US       ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per µs
48 48
 
49 49
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
50 50
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
@@ -52,6 +52,8 @@
52 52
 #define TEMP_TIMER_PRESCALE     1000 // prescaler for setting Temp timer, 72Khz
53 53
 #define TEMP_TIMER_FREQUENCY    1000 // temperature interrupt frequency
54 54
 
55
+#define STEP_TIMER_MIN_INTERVAL    8 // minimum time in µs between stepper interrupts
56
+
55 57
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
56 58
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
57 59
 
@@ -94,6 +96,7 @@ void HAL_timer_disable_interrupt(const uint8_t timer_num);
94 96
 void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare);
95 97
 hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
96 98
 uint32_t HAL_timer_get_count(const uint8_t timer_num);
99
+void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_µs);
97 100
 
98 101
 void HAL_timer_isr_prologue(const uint8_t timer_num);
99 102
 

+ 1
- 1
Marlin/src/HAL/HAL_STM32F7/TMC2660.h Wyświetl plik

@@ -315,7 +315,7 @@ class TMC26XStepper {
315 315
     /*!
316 316
      * \brief readout the motor maximum current in mA (1000 is an Amp)
317 317
      * This is the maximum current. to get the current current - which may be affected by CoolStep us getCurrentCurrent()
318
-     *\return the maximum motor current in milli amps
318
+     * \return the maximum motor current in milli amps
319 319
      * \sa getCurrentCurrent()
320 320
      */
321 321
     unsigned int getCurrent(void);

+ 7
- 0
Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h Wyświetl plik

@@ -66,6 +66,8 @@ typedef uint32_t hal_timer_t;
66 66
 
67 67
 #define TEMP_TIMER_FREQUENCY   1000
68 68
 
69
+#define STEP_TIMER_MIN_INTERVAL   8 // minimum time in µs between stepper interrupts
70
+
69 71
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
70 72
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
71 73
 #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
@@ -103,6 +105,11 @@ FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
103 105
   return 0;
104 106
 }
105 107
 
108
+FORCE_INLINE static void HAL_timer_restrain(const uint8_t timer_num, const uint16_t interval_µs) {
109
+  const hal_timer_t mincmp = HAL_timer_get_count(timer_num) + interval_µs * HAL_TICKS_PER_US;
110
+  if (HAL_timer_get_compare(timer_num) < mincmp) HAL_timer_set_compare(timer_num, mincmp);
111
+}
112
+
106 113
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
107 114
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
108 115
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 4
- 21
Marlin/src/module/stepper.cpp Wyświetl plik

@@ -365,11 +365,7 @@ void Stepper::isr() {
365 365
     _NEXT_ISR(ocr_val);
366 366
 
367 367
     #if DISABLED(LIN_ADVANCE)
368
-      #ifdef CPU_32_BIT
369
-        HAL_timer_set_compare(STEP_TIMER_NUM, ocr_val);
370
-      #else
371
-        NOLESS(OCR1A, TCNT1 + 16);
372
-      #endif
368
+      HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL);
373 369
       HAL_ENABLE_ISRs(); // re-enable ISRs
374 370
     #endif
375 371
 
@@ -731,14 +727,7 @@ void Stepper::isr() {
731 727
   }
732 728
 
733 729
   #if DISABLED(LIN_ADVANCE)
734
-    #ifdef CPU_32_BIT
735
-      // Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
736
-      hal_timer_t stepper_timer_count = HAL_timer_get_compare(STEP_TIMER_NUM),
737
-                  stepper_timer_current_count = HAL_timer_get_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
738
-      HAL_timer_set_compare(STEP_TIMER_NUM, max(stepper_timer_count, stepper_timer_current_count));
739
-    #else
740
-      NOLESS(OCR1A, TCNT1 + 16);
741
-    #endif
730
+    HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL);
742 731
   #endif
743 732
 
744 733
   // If current block is finished, reset pointer
@@ -901,14 +890,8 @@ void Stepper::isr() {
901 890
     }
902 891
 
903 892
     // Don't run the ISR faster than possible
904
-    #ifdef CPU_32_BIT
905
-      // Make sure stepper interrupt does not monopolise CPU by adjusting count to give about 8 us room
906
-      uint32_t stepper_timer_count = HAL_timer_get_compare(STEP_TIMER_NUM),
907
-               stepper_timer_current_count = HAL_timer_get_count(STEP_TIMER_NUM) + 8 * HAL_TICKS_PER_US;
908
-      HAL_timer_set_compare(STEP_TIMER_NUM, max(stepper_timer_count, stepper_timer_current_count));
909
-    #else
910
-      NOLESS(OCR1A, TCNT1 + 16);
911
-    #endif
893
+    // Make sure stepper interrupt does not monopolise CPU by adjusting compare to give about 8µs room
894
+    HAL_timer_restrain(STEP_TIMER_NUM, STEP_TIMER_MIN_INTERVAL);
912 895
 
913 896
     // Restore original ISR settings
914 897
     HAL_ENABLE_ISRs();

Ładowanie…
Anuluj
Zapisz