Pārlūkot izejas kodu

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

Chris Pepper 7 gadus atpakaļ
vecāks
revīzija
a1a88ebabc

+ 4
- 0
Marlin/src/HAL/HAL_AVR/HAL_AVR.h Parādīt failu

126
 #define HAL_STEPPER_TIMER_RATE  HAL_TIMER_RATE
126
 #define HAL_STEPPER_TIMER_RATE  HAL_TIMER_RATE
127
 #define STEPPER_TIMER_PRESCALE  8
127
 #define STEPPER_TIMER_PRESCALE  8
128
 
128
 
129
+#define STEP_TIMER_MIN_INTERVAL 8 // minimum time in µs between stepper interrupts
130
+
129
 #define STEP_TIMER_NUM          1
131
 #define STEP_TIMER_NUM          1
130
 #define TIMER_OCR_1             OCR1A
132
 #define TIMER_OCR_1             OCR1A
131
 #define TIMER_COUNTER_1         TCNT1
133
 #define TIMER_COUNTER_1         TCNT1
148
 
150
 
149
 #define _CAT(a, ...) a ## __VA_ARGS__
151
 #define _CAT(a, ...) a ## __VA_ARGS__
150
 #define HAL_timer_set_compare(timer, compare) (_CAT(TIMER_OCR_, timer) = compare)
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
 #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
155
 #define HAL_timer_get_compare(timer) _CAT(TIMER_OCR_, timer)
152
 #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
156
 #define HAL_timer_get_count(timer) _CAT(TIMER_COUNTER_, timer)
153
 
157
 

+ 2
- 2
Marlin/src/HAL/HAL_AVR/HAL_spi_AVR.cpp Parādīt failu

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

+ 7
- 1
Marlin/src/HAL/HAL_DUE/HAL_timers_Due.h Parādīt failu

50
 #define HAL_TIMER_RATE         ((F_CPU) / 2)    // frequency of timers peripherals
50
 #define HAL_TIMER_RATE         ((F_CPU) / 2)    // frequency of timers peripherals
51
 #define STEPPER_TIMER_PRESCALE 1.0              // prescaler for setting stepper frequency
51
 #define STEPPER_TIMER_PRESCALE 1.0              // prescaler for setting stepper frequency
52
 #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
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
 #define TEMP_TIMER_FREQUENCY   1000 // temperature interrupt frequency
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
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
58
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
58
 #define DISABLE_STEPPER_DRIVER_INTERRUPT()  HAL_timer_disable_interrupt(STEP_TIMER_NUM)
59
 #define DISABLE_STEPPER_DRIVER_INTERRUPT()  HAL_timer_disable_interrupt(STEP_TIMER_NUM)
108
   return pConfig->pTimerRegs->TC_CHANNEL[pConfig->channel].TC_CV;
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
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
117
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
112
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
118
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
113
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
119
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 8
- 1
Marlin/src/HAL/HAL_LPC1768/HAL_timers.h Parādīt failu

49
 #define HAL_TIMER_RATE         ((SystemCoreClock) / 4)  // frequency of timers peripherals
49
 #define HAL_TIMER_RATE         ((SystemCoreClock) / 4)  // frequency of timers peripherals
50
 #define STEPPER_TIMER_PRESCALE 1.0              // prescaler for setting stepper frequency
50
 #define STEPPER_TIMER_PRESCALE 1.0              // prescaler for setting stepper frequency
51
 #define HAL_STEPPER_TIMER_RATE HAL_TIMER_RATE   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
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
 #define HAL_TEMP_TIMER_RATE    1000000
53
 #define HAL_TEMP_TIMER_RATE    1000000
54
 #define TEMP_TIMER_FREQUENCY   1000 // temperature interrupt frequency
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
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
58
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
57
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
59
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
58
 
60
 
118
   return 0;
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
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
128
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
122
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
129
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
123
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
130
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 9
- 2
Marlin/src/HAL/HAL_STM32F1/HAL_timers_Stm32f1.h Parādīt failu

67
 #define HAL_TIMER_RATE         (F_CPU)  // frequency of timers peripherals
67
 #define HAL_TIMER_RATE         (F_CPU)  // frequency of timers peripherals
68
 #define STEPPER_TIMER_PRESCALE 18             // prescaler for setting stepper timer, 4Mhz
68
 #define STEPPER_TIMER_PRESCALE 18             // prescaler for setting stepper timer, 4Mhz
69
 #define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
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
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
72
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
73
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
73
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
74
 
74
 
75
 #define TEMP_TIMER_PRESCALE     1000 // prescaler for setting Temp timer, 72Khz
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
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
80
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
79
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
81
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
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
 FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
162
 FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
156
   switch (timer_num) {
163
   switch (timer_num) {
157
   case STEP_TIMER_NUM:
164
   case STEP_TIMER_NUM:

+ 5
- 0
Marlin/src/HAL/HAL_STM32F7/HAL_timers_STM32F7.cpp Parādīt failu

137
   return __HAL_TIM_GetCounter(&timerConfig[timer_num].timerdef);
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
 void HAL_timer_isr_prologue(const uint8_t timer_num) {
145
 void HAL_timer_isr_prologue(const uint8_t timer_num) {
141
   if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
146
   if (__HAL_TIM_GET_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE) == SET) {
142
     __HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);
147
     __HAL_TIM_CLEAR_FLAG(&timerConfig[timer_num].timerdef, TIM_FLAG_UPDATE);

+ 4
- 1
Marlin/src/HAL/HAL_STM32F7/HAL_timers_STM32F7.h Parādīt failu

44
 #define HAL_TIMER_RATE         (HAL_RCC_GetSysClockFreq() / 2)  // frequency of timer peripherals
44
 #define HAL_TIMER_RATE         (HAL_RCC_GetSysClockFreq() / 2)  // frequency of timer peripherals
45
 #define STEPPER_TIMER_PRESCALE 54            // was 40,prescaler for setting stepper timer, 2Mhz
45
 #define STEPPER_TIMER_PRESCALE 54            // was 40,prescaler for setting stepper timer, 2Mhz
46
 #define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)   // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
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
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
49
 #define PULSE_TIMER_NUM STEP_TIMER_NUM
50
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
50
 #define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
52
 #define TEMP_TIMER_PRESCALE     1000 // prescaler for setting Temp timer, 72Khz
52
 #define TEMP_TIMER_PRESCALE     1000 // prescaler for setting Temp timer, 72Khz
53
 #define TEMP_TIMER_FREQUENCY    1000 // temperature interrupt frequency
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
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
57
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
56
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
58
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
57
 
59
 
94
 void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare);
96
 void HAL_timer_set_compare(const uint8_t timer_num, const uint32_t compare);
95
 hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
97
 hal_timer_t HAL_timer_get_compare(const uint8_t timer_num);
96
 uint32_t HAL_timer_get_count(const uint8_t timer_num);
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
 void HAL_timer_isr_prologue(const uint8_t timer_num);
101
 void HAL_timer_isr_prologue(const uint8_t timer_num);
99
 
102
 

+ 1
- 1
Marlin/src/HAL/HAL_STM32F7/TMC2660.h Parādīt failu

315
     /*!
315
     /*!
316
      * \brief readout the motor maximum current in mA (1000 is an Amp)
316
      * \brief readout the motor maximum current in mA (1000 is an Amp)
317
      * This is the maximum current. to get the current current - which may be affected by CoolStep us getCurrentCurrent()
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
      * \sa getCurrentCurrent()
319
      * \sa getCurrentCurrent()
320
      */
320
      */
321
     unsigned int getCurrent(void);
321
     unsigned int getCurrent(void);

+ 7
- 0
Marlin/src/HAL/HAL_TEENSY35_36/HAL_timers_Teensy.h Parādīt failu

66
 
66
 
67
 #define TEMP_TIMER_FREQUENCY   1000
67
 #define TEMP_TIMER_FREQUENCY   1000
68
 
68
 
69
+#define STEP_TIMER_MIN_INTERVAL   8 // minimum time in µs between stepper interrupts
70
+
69
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
71
 #define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
70
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
72
 #define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
71
 #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
73
 #define STEPPER_ISR_ENABLED() HAL_timer_interrupt_enabled(STEP_TIMER_NUM)
103
   return 0;
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
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
113
 void HAL_timer_enable_interrupt(const uint8_t timer_num);
107
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
114
 void HAL_timer_disable_interrupt(const uint8_t timer_num);
108
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);
115
 bool HAL_timer_interrupt_enabled(const uint8_t timer_num);

+ 4
- 21
Marlin/src/module/stepper.cpp Parādīt failu

365
     _NEXT_ISR(ocr_val);
365
     _NEXT_ISR(ocr_val);
366
 
366
 
367
     #if DISABLED(LIN_ADVANCE)
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
       HAL_ENABLE_ISRs(); // re-enable ISRs
369
       HAL_ENABLE_ISRs(); // re-enable ISRs
374
     #endif
370
     #endif
375
 
371
 
731
   }
727
   }
732
 
728
 
733
   #if DISABLED(LIN_ADVANCE)
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
   #endif
731
   #endif
743
 
732
 
744
   // If current block is finished, reset pointer
733
   // If current block is finished, reset pointer
901
     }
890
     }
902
 
891
 
903
     // Don't run the ISR faster than possible
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
     // Restore original ISR settings
896
     // Restore original ISR settings
914
     HAL_ENABLE_ISRs();
897
     HAL_ENABLE_ISRs();

Notiek ielāde…
Atcelt
Saglabāt