forked from grblHAL/Plugins_motor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrinamic.h
338 lines (280 loc) · 8.55 KB
/
trinamic.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
/*
motors/trinamic.h - Trinamic stepper driver plugin
Part of grblHAL
Copyright (c) 2018-2023 Terje Io
Grbl 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.
Grbl 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 Grbl. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TRINAMIC_H_
#define _TRINAMIC_H_
#ifdef ARDUINO
#include "../driver.h"
#else
#include "driver.h"
#endif
#if TRINAMIC_ENABLE
#if TRINAMIC_ENABLE == 2130
#define R_SENSE 110
#include "../trinamic/tmc2130hal.h"
#endif
#if TRINAMIC_ENABLE == 2209
#define R_SENSE 110
#include "../trinamic/tmc2209hal.h"
#endif
#if TRINAMIC_ENABLE == 5160
#define R_SENSE 75
#include "../trinamic/tmc5160hal.h"
#endif
#if TRINAMIC_ENABLE == 2660
//#define R_SENSE 50
#include "../trinamic/tmc2660hal.h"
//#include "../trinamic/tmc2660.h"
#ifdef SLB_ORANGE_BOARD
#define R_SENSE 100
#else
#define R_SENSE 50
#endif
#endif
#ifndef TMC_POLL_STALLED
#if TRINAMIC_I2C
#define TMC_POLL_STALLED 1
#else
#define TMC_POLL_STALLED 0
#endif
#endif
#ifndef PWM_THRESHOLD_VELOCITY
#define PWM_THRESHOLD_VELOCITY 0 // mm/min - 0 to disable, should be set > homing seek rate when enabled (use M913 to set at run time)
#endif
#ifndef TMC_STEALTHCHOP
#define TMC_STEALTHCHOP 1 // 0 = CoolStep, 1 = StealthChop
#endif
#if TRINAMIC_ENABLE == 2209
#define TMC_STALLGUARD 4 // Do not change!
#else
#define TMC_STALLGUARD 2 // Do not change!
#endif
//#define TMC_HOMING_ACCELERATION 50.0f // NOT tested... Reduce acceleration during homing to avoid falsely triggering DIAG output
#if TRINAMIC_ENABLE == 2660
static const TMC_coolconf_t coolconf = { .semin = TMC2660_SEMIN, .semax = TMC2660_SEMAX, .sedn = TMC2660_SEDN };
#else
static const TMC_coolconf_t coolconf = { .semin = 5, .semax = 2, .sedn = 1 };
#endif
#if TRINAMIC_ENABLE == 2209
static const TMC_chopper_timing_t chopper_timing = { .hstrt = 1, .hend = -1, .tbl = 1, .toff = 3 };
#elif TRINAMIC_ENABLE == 2660
static const TMC_chopper_timing_t chopper_timing = { .hstrt = TMC2660_HSTR, .hend = TMC2660_HEND, .tbl = TMC2660_BLANK_TIME, .toff = TMC2660_CONSTANT_OFF_TIME };
#else
static const TMC_chopper_timing_t chopper_timing = { .hstrt = 1, .hend = -1, .tbl = 1, .toff = 5 };
#endif
// General
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_X_ENABLE
#define TMC_X_ENABLE 0
#endif
#else
#define TMC_X_ENABLE 1 // Do not change
#endif
#define TMC_X_MONITOR 1
#ifndef TRINAMIC_DEFAULT_MICROSTEPS
#define TRINAMIC_DEFAULT_MICROSTEPS 8
#endif
#define TMC_X_MICROSTEPS TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_X_R_SENSE R_SENSE // mOhm
#ifndef DEFAULT_X_CURRENT
#define TMC_X_CURRENT 500 // mA RMS
#else
#define TMC_X_CURRENT DEFAULT_X_CURRENT // mA RMS
#endif
#ifndef TMC_X_HOLD_CURRENT_PCT
#define TMC_X_HOLD_CURRENT_PCT 10
#endif
#define TMC_X_HOMING_SEEK_SGT 22
#define TMC_X_HOMING_FEED_SGT 22
#define TMC_X_STEALTHCHOP TMC_STEALTHCHOP
#define TMC_X_ADVANCED(motor) \
stepper[motor]->sg_filter(motor, 1); \
stepper[motor]->coolconf(motor, coolconf); \
stepper[motor]->chopper_timing(motor, chopper_timing);
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_Y_ENABLE
#define TMC_Y_ENABLE 0
#endif
#else
#define TMC_Y_ENABLE 1 // Do not change
#endif
#define TMC_Y_MONITOR 1
#define TMC_Y_MICROSTEPS TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_Y_R_SENSE R_SENSE // mOhm
#ifndef DEFAULT_Y_CURRENT
#define TMC_Y_CURRENT 500 // mA RMS
#else
#define TMC_Y_CURRENT DEFAULT_Y_CURRENT // mA RMS
#endif
#ifndef TMC_Y_HOLD_CURRENT_PCT
#define TMC_Y_HOLD_CURRENT_PCT 10
#endif
#define TMC_Y_HOMING_SEEK_SGT 22
#define TMC_Y_HOMING_FEED_SGT 22
#define TMC_Y_STEALTHCHOP TMC_STEALTHCHOP
#define TMC_Y_ADVANCED(motor) \
stepper[motor]->sg_filter(motor, 1); \
stepper[motor]->coolconf(motor, coolconf); \
stepper[motor]->chopper_timing(motor, chopper_timing);
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_Z_ENABLE
#define TMC_Z_ENABLE 0
#endif
#else
#define TMC_Z_ENABLE 1 // Do not change
#endif
#define TMC_Z_MONITOR 1
#define TMC_Z_MICROSTEPS TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_Z_R_SENSE R_SENSE // mOhm
#ifndef DEFAULT_Z_CURRENT
#define TMC_Z_CURRENT 500 // mA RMS
#else
#define TMC_Z_CURRENT DEFAULT_Z_CURRENT // mA RMS
#endif
#ifndef TMC_Z_HOLD_CURRENT_PCT
#define TMC_Z_HOLD_CURRENT_PCT 10
#endif
#define TMC_Z_HOMING_SEEK_SGT 22
#define TMC_Z_HOMING_FEED_SGT 22
#define TMC_Z_STEALTHCHOP TMC_STEALTHCHOP
#define TMC_Z_ADVANCED(motor) \
stepper[motor]->sg_filter(motor, 1); \
stepper[motor]->coolconf(motor, coolconf); \
stepper[motor]->chopper_timing(motor, chopper_timing);
#ifdef A_AXIS
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_A_ENABLE
#define TMC_A_ENABLE 0
#endif
#else
#define TMC_A_ENABLE 1 // Do not change
#endif
#define TMC_A_MONITOR 1
#define TMC_A_MICROSTEPS 16
#define TMC_A_R_SENSE R_SENSE // mOhm
#ifndef DEFAULT_A_CURRENT
#define TMC_A_CURRENT 500 // mA RMS
#else
#define TMC_A_CURRENT DEFAULT_A_CURRENT // mA RMS
#endif
#define TMC_A_HOLD_CURRENT_PCT 50
#define TMC_A_HOMING_SEEK_SGT 22
#define TMC_A_HOMING_FEED_SGT 22
#define TMC_A_STEALTHCHOP TMC_STEALTHCHOP
#define TMC_A_ADVANCED(motor) \
stepper[motor]->sg_filter(motor, 1); \
stepper[motor]->coolconf(motor, coolconf); \
stepper[motor]->chopper_timing(motor, chopper_timing);
#endif
#ifdef B_AXIS
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_B_ENABLE
#define TMC_B_ENABLE 0
#endif
#else
#define TMC_B_ENABLE 1 // Do not change
#endif
#define TMC_B_MONITOR 1
#define TMC_B_MICROSTEPS 16
#define TMC_B_R_SENSE R_SENSE // mOhm
#ifndef DEFAULT_B_CURRENT
#define TMC_B_CURRENT 500 // mA RMS
#else
#define TMC_B_CURRENT DEFAULT_B_CURRENT // mA RMS
#endif
#define TMC_B_HOLD_CURRENT_PCT 50
#define TMC_B_HOMING_SEEK_SGT 22
#define TMC_B_HOMING_FEED_SGT 22
#define TMC_B_STEALTHCHOP TMC_STEALTHCHOP
#define TMC_B_ADVANCED(motor) \
stepper[motor]->sg_filter(motor, 1); \
stepper[motor]->coolconf(motor, coolconf); \
stepper[motor]->chopper_timing(motor, chopper_timing);
#endif
#ifdef C_AXIS
#if TRINAMIC_MIXED_DRIVERS
#define TMC_C_ENABLE 0
#else
#define TMC_C_ENABLE 1 // Do not change
#endif
#define TMC_C_MONITOR 1
#define TMC_C_MICROSTEPS 16
#define TMC_C_R_SENSE R_SENSE // mOhm
#ifndef DEFAULT_C_CURRENT
#define TMC_C_CURRENT 500 // mA RMS
#else
#define TMC_C_CURRENT DEFAULT_C_CURRENT // mA RMS
#endif
#define TMC_C_HOLD_CURRENT_PCT 50
#define TMC_C_HOMING_SEEK_SGT 22
#define TMC_C_HOMING_FEED_SGT 22
#define TMC_C_STEALTHCHOP TMC_STEALTHCHOP
#define TMC_C_ADVANCED(motor) \
stepper[motor]->sg_filter(motor, 1); \
stepper[motor]->coolconf(motor, coolconf); \
stepper[motor]->chopper_timing(motor, chopper_timing);
#endif
//
typedef struct {
uint16_t current; // mA
uint8_t hold_current_pct;
uint16_t r_sense; // mOhm
uint16_t microsteps;
trinamic_mode_t mode;
float homing_seek_rate;
float homing_feed_rate;
int16_t homing_seek_sensitivity;
int16_t homing_feed_sensitivity;
} motor_settings_t;
typedef struct {
axes_signals_t driver_enable;
axes_signals_t homing_enable;
motor_settings_t driver[N_AXIS];
#if (BOARD_LONGBOARD32)
TMC2660_settings_t tmc2660_settings;
/*uint8_t toff;
uint8_t tbl;
uint8_t chm;
uint8_t hstr;
uint8_t hend;
uint8_t hdec;
uint8_t rndtf;
uint8_t thresh;
uint8_t semin;
uint8_t seup;
uint8_t semax;
uint8_t sedn;
uint8_t seimin;
uint32_t drvconf;*/
#endif
} trinamic_settings_t;
typedef struct {
uint8_t address; // slave address, for UART Single Wire Interface drivers - can be overridden by driver interface
motor_settings_t *settings; // for info only, do not modify
} trinamic_driver_config_t;
typedef void (*trinamic_on_drivers_init_ptr)(uint8_t n_motors, axes_signals_t enabled);
typedef void (*trinamic_on_driver_preinit_ptr)(motor_map_t motor, trinamic_driver_config_t *config);
typedef void (*trinamic_on_driver_postinit_ptr)(motor_map_t motor, const tmchal_t *driver);
typedef struct {
trinamic_on_drivers_init_ptr on_drivers_init;
trinamic_on_driver_preinit_ptr on_driver_preinit;
trinamic_on_driver_postinit_ptr on_driver_postinit;
} trinamic_driver_if_t;
bool trinamic_init (void);
void trinamic_fault_handler (void);
void trinamic_warn_handler (void);
void trinamic_if_init (trinamic_driver_if_t *driver);
#endif // TRINAMIC_ENABLE
#endif