/
tmc_util.h
392 lines (337 loc) · 13.6 KB
/
tmc_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#pragma once
#include "../inc/MarlinConfig.h"
#include "../lcd/ultralcd.h"
#if HAS_TRINAMIC_CONFIG
#include <TMCStepper.h>
#include "../module/planner.h"
#define CHOPPER_DEFAULT_12V { 3, -1, 1 }
#define CHOPPER_DEFAULT_19V { 4, 1, 1 }
#define CHOPPER_DEFAULT_24V { 4, 2, 1 }
#define CHOPPER_DEFAULT_36V { 5, 2, 4 }
#define CHOPPER_PRUSAMK3_24V { 3, -2, 6 }
#define CHOPPER_MARLIN_119 { 5, 2, 3 }
#define CHOPPER_09STEP_24V { 3, -1, 5 }
#if ENABLED(MONITOR_DRIVER_STATUS) && !defined(MONITOR_DRIVER_STATUS_INTERVAL_MS)
#define MONITOR_DRIVER_STATUS_INTERVAL_MS 500u
#endif
constexpr uint16_t _tmc_thrs(const uint16_t msteps, const uint32_t thrs, const uint32_t spmm) {
return 12650000UL * msteps / (256 * thrs * spmm);
}
template<char AXIS_LETTER, char DRIVER_ID>
class TMCStorage {
protected:
// Only a child class has access to constructor => Don't create on its own! "Poor man's abstract class"
TMCStorage() {}
public:
uint16_t val_mA = 0;
#if ENABLED(MONITOR_DRIVER_STATUS)
uint8_t otpw_count = 0,
error_count = 0;
bool flag_otpw = false;
inline bool getOTPW() { return flag_otpw; }
inline void clear_otpw() { flag_otpw = 0; }
#endif
inline uint16_t getMilliamps() { return val_mA; }
inline void printLabel() {
SERIAL_CHAR(AXIS_LETTER);
if (DRIVER_ID > '0') SERIAL_CHAR(DRIVER_ID);
}
struct {
TERN_(HAS_STEALTHCHOP, bool stealthChop_enabled = false);
TERN_(HYBRID_THRESHOLD, uint8_t hybrid_thrs = 0);
TERN_(USE_SENSORLESS, int16_t homing_thrs = 0);
} stored;
};
template<class TMC, char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
class TMCMarlin : public TMC, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
public:
TMCMarlin(const uint16_t cs_pin, const float RS) :
TMC(cs_pin, RS)
{}
TMCMarlin(const uint16_t cs_pin, const float RS, const uint8_t axis_chain_index) :
TMC(cs_pin, RS, axis_chain_index)
{}
TMCMarlin(const uint16_t CS, const float RS, const uint16_t pinMOSI, const uint16_t pinMISO, const uint16_t pinSCK) :
TMC(CS, RS, pinMOSI, pinMISO, pinSCK)
{}
TMCMarlin(const uint16_t CS, const float RS, const uint16_t pinMOSI, const uint16_t pinMISO, const uint16_t pinSCK, const uint8_t axis_chain_index) :
TMC(CS, RS, pinMOSI, pinMISO, pinSCK, axis_chain_index)
{}
inline uint16_t rms_current() { return TMC::rms_current(); }
inline void rms_current(uint16_t mA) {
this->val_mA = mA;
TMC::rms_current(mA);
}
inline void rms_current(const uint16_t mA, const float mult) {
this->val_mA = mA;
TMC::rms_current(mA, mult);
}
inline uint16_t get_microstep_counter() { return TMC::MSCNT(); }
#if HAS_STEALTHCHOP
inline void refresh_stepping_mode() { this->en_pwm_mode(this->stored.stealthChop_enabled); }
inline bool get_stealthChop_status() { return this->en_pwm_mode(); }
#endif
#if ENABLED(HYBRID_THRESHOLD)
uint32_t get_pwm_thrs() {
return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
}
void set_pwm_thrs(const uint32_t thrs) {
TMC::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
TERN_(HAS_LCD_MENU, this->stored.hybrid_thrs = thrs);
}
#endif
#if USE_SENSORLESS
inline int16_t homing_threshold() { return TMC::sgt(); }
void homing_threshold(int16_t sgt_val) {
sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
TMC::sgt(sgt_val);
TERN_(HAS_LCD_MENU, this->stored.homing_thrs = sgt_val);
}
#if ENABLED(SPI_ENDSTOPS)
bool test_stall_status();
#endif
#endif
#if HAS_LCD_MENU
inline void refresh_stepper_current() { rms_current(this->val_mA); }
#if ENABLED(HYBRID_THRESHOLD)
inline void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
#endif
#if USE_SENSORLESS
inline void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
#endif
#endif
static constexpr int8_t sgt_min = -64,
sgt_max = 63;
};
template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
class TMCMarlin<TMC2208Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2208Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
public:
TMCMarlin(Stream * SerialPort, const float RS, const uint8_t) :
TMC2208Stepper(SerialPort, RS)
{}
TMCMarlin(Stream * SerialPort, const float RS, uint8_t addr, const uint16_t mul_pin1, const uint16_t mul_pin2) :
TMC2208Stepper(SerialPort, RS, addr, mul_pin1, mul_pin2)
{}
TMCMarlin(const uint16_t RX, const uint16_t TX, const float RS, const uint8_t) :
TMC2208Stepper(RX, TX, RS)
{}
uint16_t rms_current() { return TMC2208Stepper::rms_current(); }
inline void rms_current(const uint16_t mA) {
this->val_mA = mA;
TMC2208Stepper::rms_current(mA);
}
inline void rms_current(const uint16_t mA, const float mult) {
this->val_mA = mA;
TMC2208Stepper::rms_current(mA, mult);
}
inline uint16_t get_microstep_counter() { return TMC2208Stepper::MSCNT(); }
#if HAS_STEALTHCHOP
inline void refresh_stepping_mode() { en_spreadCycle(!this->stored.stealthChop_enabled); }
inline bool get_stealthChop_status() { return !this->en_spreadCycle(); }
#endif
#if ENABLED(HYBRID_THRESHOLD)
uint32_t get_pwm_thrs() {
return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
}
void set_pwm_thrs(const uint32_t thrs) {
TMC2208Stepper::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
TERN_(HAS_LCD_MENU, this->stored.hybrid_thrs = thrs);
}
#endif
#if HAS_LCD_MENU
inline void refresh_stepper_current() { rms_current(this->val_mA); }
#if ENABLED(HYBRID_THRESHOLD)
inline void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
#endif
#endif
};
template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
class TMCMarlin<TMC2209Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2209Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
public:
TMCMarlin(Stream * SerialPort, const float RS, const uint8_t addr) :
TMC2209Stepper(SerialPort, RS, addr)
{}
TMCMarlin(const uint16_t RX, const uint16_t TX, const float RS, const uint8_t addr) :
TMC2209Stepper(RX, TX, RS, addr)
{}
uint8_t get_address() { return slave_address; }
uint16_t rms_current() { return TMC2209Stepper::rms_current(); }
inline void rms_current(const uint16_t mA) {
this->val_mA = mA;
TMC2209Stepper::rms_current(mA);
}
inline void rms_current(const uint16_t mA, const float mult) {
this->val_mA = mA;
TMC2209Stepper::rms_current(mA, mult);
}
inline uint16_t get_microstep_counter() { return TMC2209Stepper::MSCNT(); }
#if HAS_STEALTHCHOP
inline void refresh_stepping_mode() { en_spreadCycle(!this->stored.stealthChop_enabled); }
inline bool get_stealthChop_status() { return !this->en_spreadCycle(); }
#endif
#if ENABLED(HYBRID_THRESHOLD)
uint32_t get_pwm_thrs() {
return _tmc_thrs(this->microsteps(), this->TPWMTHRS(), planner.settings.axis_steps_per_mm[AXIS_ID]);
}
void set_pwm_thrs(const uint32_t thrs) {
TMC2209Stepper::TPWMTHRS(_tmc_thrs(this->microsteps(), thrs, planner.settings.axis_steps_per_mm[AXIS_ID]));
TERN_(HAS_LCD_MENU, this->stored.hybrid_thrs = thrs);
}
#endif
#if USE_SENSORLESS
inline int16_t homing_threshold() { return TMC2209Stepper::SGTHRS(); }
void homing_threshold(int16_t sgt_val) {
sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
TMC2209Stepper::SGTHRS(sgt_val);
TERN_(HAS_LCD_MENU, this->stored.homing_thrs = sgt_val);
}
#endif
#if HAS_LCD_MENU
inline void refresh_stepper_current() { rms_current(this->val_mA); }
#if ENABLED(HYBRID_THRESHOLD)
inline void refresh_hybrid_thrs() { set_pwm_thrs(this->stored.hybrid_thrs); }
#endif
#if USE_SENSORLESS
inline void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
#endif
#endif
static constexpr uint8_t sgt_min = 0,
sgt_max = 255;
};
template<char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
class TMCMarlin<TMC2660Stepper, AXIS_LETTER, DRIVER_ID, AXIS_ID> : public TMC2660Stepper, public TMCStorage<AXIS_LETTER, DRIVER_ID> {
public:
TMCMarlin(const uint16_t cs_pin, const float RS, const uint8_t) :
TMC2660Stepper(cs_pin, RS)
{}
TMCMarlin(const uint16_t CS, const float RS, const uint16_t pinMOSI, const uint16_t pinMISO, const uint16_t pinSCK, const uint8_t) :
TMC2660Stepper(CS, RS, pinMOSI, pinMISO, pinSCK)
{}
inline uint16_t rms_current() { return TMC2660Stepper::rms_current(); }
inline void rms_current(const uint16_t mA) {
this->val_mA = mA;
TMC2660Stepper::rms_current(mA);
}
inline uint16_t get_microstep_counter() { return TMC2660Stepper::mstep(); }
#if USE_SENSORLESS
inline int16_t homing_threshold() { return TMC2660Stepper::sgt(); }
void homing_threshold(int16_t sgt_val) {
sgt_val = (int16_t)constrain(sgt_val, sgt_min, sgt_max);
TMC2660Stepper::sgt(sgt_val);
TERN_(HAS_LCD_MENU, this->stored.homing_thrs = sgt_val);
}
#endif
#if HAS_LCD_MENU
inline void refresh_stepper_current() { rms_current(this->val_mA); }
#if USE_SENSORLESS
inline void refresh_homing_thrs() { homing_threshold(this->stored.homing_thrs); }
#endif
#endif
static constexpr int8_t sgt_min = -64,
sgt_max = 63;
};
template<typename TMC>
void tmc_print_current(TMC &st) {
st.printLabel();
SERIAL_ECHOLNPAIR(" driver current: ", st.getMilliamps());
}
#if ENABLED(MONITOR_DRIVER_STATUS)
template<typename TMC>
void tmc_report_otpw(TMC &st) {
st.printLabel();
SERIAL_ECHOPGM(" temperature prewarn triggered: ");
serialprint_truefalse(st.getOTPW());
SERIAL_EOL();
}
template<typename TMC>
void tmc_clear_otpw(TMC &st) {
st.clear_otpw();
st.printLabel();
SERIAL_ECHOLNPGM(" prewarn flag cleared");
}
#endif
#if ENABLED(HYBRID_THRESHOLD)
template<typename TMC>
void tmc_print_pwmthrs(TMC &st) {
st.printLabel();
SERIAL_ECHOLNPAIR(" stealthChop max speed: ", st.get_pwm_thrs());
}
#endif
#if USE_SENSORLESS
template<typename TMC>
void tmc_print_sgt(TMC &st) {
st.printLabel();
SERIAL_ECHOPGM(" homing sensitivity: ");
SERIAL_PRINTLN(st.homing_threshold(), DEC);
}
#endif
void monitor_tmc_drivers();
void test_tmc_connection(const bool test_x, const bool test_y, const bool test_z, const bool test_e);
#if ENABLED(TMC_DEBUG)
#if ENABLED(MONITOR_DRIVER_STATUS)
void tmc_set_report_interval(const uint16_t update_interval);
#endif
void tmc_report_all(const bool print_x, const bool print_y, const bool print_z, const bool print_e);
void tmc_get_registers(const bool print_x, const bool print_y, const bool print_z, const bool print_e);
#endif
/**
* TMC2130-specific sensorless homing using stallGuard2.
* stallGuard2 only works when in spreadCycle mode.
* spreadCycle and stealthChop are mutually-exclusive.
*
* Defined here because of limitations with templates and headers.
*/
#if USE_SENSORLESS
// Track enabled status of stealthChop and only re-enable where applicable
struct sensorless_t { bool x, y, z, x2, y2, z2, z3, z4; };
#if ENABLED(IMPROVE_HOMING_RELIABILITY)
extern millis_t sg_guard_period;
constexpr uint16_t default_sg_guard_duration = 400;
struct slow_homing_t {
xy_ulong_t acceleration;
TERN_(HAS_CLASSIC_JERK, xy_float_t jerk_xy);
};
#endif
bool tmc_enable_stallguard(TMC2130Stepper &st);
void tmc_disable_stallguard(TMC2130Stepper &st, const bool restore_stealth);
bool tmc_enable_stallguard(TMC2209Stepper &st);
void tmc_disable_stallguard(TMC2209Stepper &st, const bool restore_stealth);
bool tmc_enable_stallguard(TMC2660Stepper);
void tmc_disable_stallguard(TMC2660Stepper, const bool);
#if ENABLED(SPI_ENDSTOPS)
template<class TMC, char AXIS_LETTER, char DRIVER_ID, AxisEnum AXIS_ID>
bool TMCMarlin<TMC, AXIS_LETTER, DRIVER_ID, AXIS_ID>::test_stall_status() {
this->switchCSpin(LOW);
// read stallGuard flag from TMC library, will handle HW and SW SPI
TMC2130_n::DRV_STATUS_t drv_status{0};
drv_status.sr = this->DRV_STATUS();
this->switchCSpin(HIGH);
return drv_status.stallGuard;
}
#endif // SPI_ENDSTOPS
#endif // USE_SENSORLESS
#if HAS_TMC_SPI
void tmc_init_cs_pins();
#endif
#endif // HAS_TRINAMIC_CONFIG