|
@@ -43,19 +43,169 @@
|
43
|
43
|
#ifndef STEPPER_H
|
44
|
44
|
#define STEPPER_H
|
45
|
45
|
|
|
46
|
+#include "../inc/MarlinConfig.h"
|
|
47
|
+
|
|
48
|
+// Disable multiple steps per ISR
|
|
49
|
+//#define DISABLE_MULTI_STEPPING
|
|
50
|
+
|
|
51
|
+//
|
|
52
|
+// Estimate the amount of time the Stepper ISR will take to execute
|
|
53
|
+//
|
|
54
|
+
|
|
55
|
+#ifndef MINIMUM_STEPPER_PULSE
|
|
56
|
+ #define MINIMUM_STEPPER_PULSE 0
|
|
57
|
+#endif
|
|
58
|
+
|
|
59
|
+#ifndef MAXIMUM_STEPPER_RATE
|
|
60
|
+ #if MINIMUM_STEPPER_PULSE
|
|
61
|
+ #define MAXIMUM_STEPPER_RATE (1000000UL / (2UL * (MINIMUM_STEPPER_PULSE)))
|
|
62
|
+ #else
|
|
63
|
+ #define MAXIMUM_STEPPER_RATE 500000UL
|
|
64
|
+ #endif
|
|
65
|
+#endif
|
|
66
|
+
|
|
67
|
+#ifdef CPU_32_BIT
|
|
68
|
+
|
|
69
|
+ // The base ISR takes 792 cycles
|
|
70
|
+ #define ISR_BASE_CYCLES 792UL
|
|
71
|
+
|
|
72
|
+ // Linear advance base time is 64 cycles
|
|
73
|
+ #if ENABLED(LIN_ADVANCE)
|
|
74
|
+ #define ISR_LA_BASE_CYCLES 64UL
|
|
75
|
+ #else
|
|
76
|
+ #define ISR_LA_BASE_CYCLES 0UL
|
|
77
|
+ #endif
|
|
78
|
+
|
|
79
|
+ // S curve interpolation adds 40 cycles
|
|
80
|
+ #if ENABLED(S_CURVE_ACCELERATION)
|
|
81
|
+ #define ISR_S_CURVE_CYCLES 40UL
|
|
82
|
+ #else
|
|
83
|
+ #define ISR_S_CURVE_CYCLES 0UL
|
|
84
|
+ #endif
|
|
85
|
+
|
|
86
|
+ // Stepper Loop base cycles
|
|
87
|
+ #define ISR_LOOP_BASE_CYCLES 4UL
|
|
88
|
+
|
|
89
|
+ // And each stepper takes 16 cycles
|
|
90
|
+ #define ISR_STEPPER_CYCLES 16UL
|
|
91
|
+
|
|
92
|
+#else
|
|
93
|
+
|
|
94
|
+ // The base ISR takes 752 cycles
|
|
95
|
+ #define ISR_BASE_CYCLES 752UL
|
|
96
|
+
|
|
97
|
+ // Linear advance base time is 32 cycles
|
|
98
|
+ #if ENABLED(LIN_ADVANCE)
|
|
99
|
+ #define ISR_LA_BASE_CYCLES 32UL
|
|
100
|
+ #else
|
|
101
|
+ #define ISR_LA_BASE_CYCLES 0UL
|
|
102
|
+ #endif
|
|
103
|
+
|
|
104
|
+ // S curve interpolation adds 160 cycles
|
|
105
|
+ #if ENABLED(S_CURVE_ACCELERATION)
|
|
106
|
+ #define ISR_S_CURVE_CYCLES 160UL
|
|
107
|
+ #else
|
|
108
|
+ #define ISR_S_CURVE_CYCLES 0UL
|
|
109
|
+ #endif
|
|
110
|
+
|
|
111
|
+ // Stepper Loop base cycles
|
|
112
|
+ #define ISR_LOOP_BASE_CYCLES 32UL
|
|
113
|
+
|
|
114
|
+ // And each stepper takes 88 cycles
|
|
115
|
+ #define ISR_STEPPER_CYCLES 88UL
|
|
116
|
+
|
|
117
|
+#endif
|
|
118
|
+
|
|
119
|
+// Add time for each stepper
|
|
120
|
+#ifdef HAS_X_STEP
|
|
121
|
+ #define ISR_X_STEPPER_CYCLES ISR_STEPPER_CYCLES
|
|
122
|
+#else
|
|
123
|
+ #define ISR_X_STEPPER_CYCLES 0UL
|
|
124
|
+#endif
|
|
125
|
+#ifdef HAS_Y_STEP
|
|
126
|
+ #define ISR_Y_STEPPER_CYCLES ISR_STEPPER_CYCLES
|
|
127
|
+#else
|
|
128
|
+ #define ISR_Y_STEPPER_CYCLES 0UL
|
|
129
|
+#endif
|
|
130
|
+#ifdef HAS_Z_STEP
|
|
131
|
+ #define ISR_Z_STEPPER_CYCLES ISR_STEPPER_CYCLES
|
|
132
|
+#else
|
|
133
|
+ #define ISR_Z_STEPPER_CYCLES 0UL
|
|
134
|
+#endif
|
|
135
|
+
|
|
136
|
+// E is always interpolated, even for mixing extruders
|
|
137
|
+#define ISR_E_STEPPER_CYCLES ISR_STEPPER_CYCLES
|
|
138
|
+
|
|
139
|
+// If linear advance is disabled, then the loop also handles them
|
|
140
|
+#if DISABLED(LIN_ADVANCE) && ENABLED(MIXING_EXTRUDER)
|
|
141
|
+ #define ISR_MIXING_STEPPER_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
|
|
142
|
+#else
|
|
143
|
+ #define ISR_MIXING_STEPPER_CYCLES 0UL
|
|
144
|
+#endif
|
|
145
|
+
|
|
146
|
+// And the total minimum loop time, not including the base
|
|
147
|
+#define MIN_ISR_LOOP_CYCLES (ISR_X_STEPPER_CYCLES + ISR_Y_STEPPER_CYCLES + ISR_Z_STEPPER_CYCLES + ISR_E_STEPPER_CYCLES + ISR_MIXING_STEPPER_CYCLES)
|
|
148
|
+
|
|
149
|
+// Calculate the minimum MPU cycles needed per pulse to enforce, limited to the max stepper rate
|
|
150
|
+#define _MIN_STEPPER_PULSE_CYCLES(N) max((F_CPU) / (MAXIMUM_STEPPER_RATE), ((F_CPU) / 500000UL) * (N))
|
|
151
|
+#if MINIMUM_STEPPER_PULSE
|
|
152
|
+ #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(MINIMUM_STEPPER_PULSE)
|
|
153
|
+#else
|
|
154
|
+ #define MIN_STEPPER_PULSE_CYCLES _MIN_STEPPER_PULSE_CYCLES(1)
|
|
155
|
+#endif
|
|
156
|
+
|
|
157
|
+#define MIN_PULSE_TICKS ((PULSE_TIMER_TICKS_PER_US) * (MINIMUM_STEPPER_PULSE))
|
|
158
|
+#define ADDED_STEP_TICKS ((MIN_STEPPER_PULSE_CYCLES) / (PULSE_TIMER_PRESCALE) - MIN_PULSE_TICKS)
|
|
159
|
+
|
|
160
|
+// But the user could be enforcing a minimum time, so the loop time is
|
|
161
|
+#define ISR_LOOP_CYCLES (ISR_LOOP_BASE_CYCLES + max(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LOOP_CYCLES))
|
|
162
|
+
|
|
163
|
+// If linear advance is enabled, then it is handled separately
|
|
164
|
+#if ENABLED(LIN_ADVANCE)
|
|
165
|
+
|
|
166
|
+ // Estimate the minimum LA loop time
|
|
167
|
+ #if ENABLED(MIXING_EXTRUDER)
|
|
168
|
+ #define MIN_ISR_LA_LOOP_CYCLES ((MIXING_STEPPERS) * (ISR_STEPPER_CYCLES))
|
|
169
|
+ #else
|
|
170
|
+ #define MIN_ISR_LA_LOOP_CYCLES ISR_STEPPER_CYCLES
|
|
171
|
+ #endif
|
|
172
|
+
|
|
173
|
+ // And the real loop time
|
|
174
|
+ #define ISR_LA_LOOP_CYCLES max(MIN_STEPPER_PULSE_CYCLES, MIN_ISR_LA_LOOP_CYCLES)
|
|
175
|
+
|
|
176
|
+#else
|
|
177
|
+ #define ISR_LA_LOOP_CYCLES 0UL
|
|
178
|
+#endif
|
|
179
|
+
|
|
180
|
+// Now estimate the total ISR execution time in cycles given a step per ISR multiplier
|
|
181
|
+#define ISR_EXECUTION_CYCLES(R) (((ISR_BASE_CYCLES + ISR_S_CURVE_CYCLES + (ISR_LOOP_CYCLES) * (R) + ISR_LA_BASE_CYCLES + ISR_LA_LOOP_CYCLES)) / (R))
|
|
182
|
+
|
|
183
|
+// The maximum allowable stepping frequency when doing x128-x1 stepping (in Hz)
|
|
184
|
+#define MAX_STEP_ISR_FREQUENCY_128X ((F_CPU) / ISR_EXECUTION_CYCLES(128))
|
|
185
|
+#define MAX_STEP_ISR_FREQUENCY_64X ((F_CPU) / ISR_EXECUTION_CYCLES(64))
|
|
186
|
+#define MAX_STEP_ISR_FREQUENCY_32X ((F_CPU) / ISR_EXECUTION_CYCLES(32))
|
|
187
|
+#define MAX_STEP_ISR_FREQUENCY_16X ((F_CPU) / ISR_EXECUTION_CYCLES(16))
|
|
188
|
+#define MAX_STEP_ISR_FREQUENCY_8X ((F_CPU) / ISR_EXECUTION_CYCLES(8))
|
|
189
|
+#define MAX_STEP_ISR_FREQUENCY_4X ((F_CPU) / ISR_EXECUTION_CYCLES(4))
|
|
190
|
+#define MAX_STEP_ISR_FREQUENCY_2X ((F_CPU) / ISR_EXECUTION_CYCLES(2))
|
|
191
|
+#define MAX_STEP_ISR_FREQUENCY_1X ((F_CPU) / ISR_EXECUTION_CYCLES(1))
|
|
192
|
+
|
|
193
|
+// The minimum allowable frequency for step smoothing will be 1/10 of the maximum nominal frequency (in Hz)
|
|
194
|
+#define MIN_STEP_ISR_FREQUENCY MAX_STEP_ISR_FREQUENCY_1X
|
|
195
|
+
|
|
196
|
+//
|
|
197
|
+// Stepper class definition
|
|
198
|
+//
|
|
199
|
+
|
46
|
200
|
#include "stepper_indirection.h"
|
47
|
201
|
|
48
|
202
|
#ifdef __AVR__
|
49
|
203
|
#include "speed_lookuptable.h"
|
50
|
204
|
#endif
|
51
|
205
|
|
52
|
|
-#include "../inc/MarlinConfig.h"
|
53
|
206
|
#include "../module/planner.h"
|
54
|
207
|
#include "../core/language.h"
|
55
|
208
|
|
56
|
|
-class Stepper;
|
57
|
|
-extern Stepper stepper;
|
58
|
|
-
|
59
|
209
|
class Stepper {
|
60
|
210
|
|
61
|
211
|
public:
|
|
@@ -303,14 +453,14 @@ class Stepper {
|
303
|
453
|
|
304
|
454
|
// The stepping frequency limits for each multistepping rate
|
305
|
455
|
static const uint32_t limit[] PROGMEM = {
|
306
|
|
- ( MAX_1X_STEP_ISR_FREQUENCY ),
|
307
|
|
- ( MAX_2X_STEP_ISR_FREQUENCY >> 1),
|
308
|
|
- ( MAX_4X_STEP_ISR_FREQUENCY >> 2),
|
309
|
|
- ( MAX_8X_STEP_ISR_FREQUENCY >> 3),
|
310
|
|
- ( MAX_16X_STEP_ISR_FREQUENCY >> 4),
|
311
|
|
- ( MAX_32X_STEP_ISR_FREQUENCY >> 5),
|
312
|
|
- ( MAX_64X_STEP_ISR_FREQUENCY >> 6),
|
313
|
|
- (MAX_128X_STEP_ISR_FREQUENCY >> 7)
|
|
456
|
+ ( MAX_STEP_ISR_FREQUENCY_1X ),
|
|
457
|
+ ( MAX_STEP_ISR_FREQUENCY_2X >> 1),
|
|
458
|
+ ( MAX_STEP_ISR_FREQUENCY_4X >> 2),
|
|
459
|
+ ( MAX_STEP_ISR_FREQUENCY_8X >> 3),
|
|
460
|
+ ( MAX_STEP_ISR_FREQUENCY_16X >> 4),
|
|
461
|
+ ( MAX_STEP_ISR_FREQUENCY_32X >> 5),
|
|
462
|
+ ( MAX_STEP_ISR_FREQUENCY_64X >> 6),
|
|
463
|
+ (MAX_STEP_ISR_FREQUENCY_128X >> 7)
|
314
|
464
|
};
|
315
|
465
|
|
316
|
466
|
// Select the proper multistepping
|
|
@@ -321,7 +471,7 @@ class Stepper {
|
321
|
471
|
++idx;
|
322
|
472
|
};
|
323
|
473
|
#else
|
324
|
|
- NOMORE(step_rate, uint32_t(MAX_1X_STEP_ISR_FREQUENCY));
|
|
474
|
+ NOMORE(step_rate, uint32_t(MAX_STEP_ISR_FREQUENCY_1X));
|
325
|
475
|
#endif
|
326
|
476
|
*loops = multistep;
|
327
|
477
|
|
|
@@ -367,4 +517,6 @@ class Stepper {
|
367
|
517
|
|
368
|
518
|
};
|
369
|
519
|
|
|
520
|
+extern Stepper stepper;
|
|
521
|
+
|
370
|
522
|
#endif // STEPPER_H
|