-
Notifications
You must be signed in to change notification settings - Fork 8
/
trinamic.h
367 lines (309 loc) · 9.08 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
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
/*
motors/trinamic.h - Trinamic stepper driver plugin
Part of grblHAL
Copyright (c) 2018-2024 Terje Io
grblHAL 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.
grblHAL 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 grblHAL. 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
#ifdef TRINAMIC_R_SENSE
#define R_SENSE TRINAMIC_R_SENSE
#endif
#ifndef TRINAMIC_DEFAULT_MICROSTEPS
#define TRINAMIC_DEFAULT_MICROSTEPS 16
#endif
#ifndef TRINAMIC_EXTENDED_SETTINGS
#define TRINAMIC_EXTENDED_SETTINGS 0
#endif
#if TRINAMIC_ENABLE == 2130
#ifndef R_SENSE
#define R_SENSE 110
#endif
#include "../trinamic/tmc2130hal.h"
#endif
#if TRINAMIC_ENABLE == 2209
#ifndef R_SENSE
#define R_SENSE 110
#endif
#ifdef TMC_STEALTHCHOP
#undef TMC_STEALTHCHOP
#endif
#define TMC_STEALTHCHOP 0 // not supported
#include "../trinamic/tmc2209hal.h"
#endif
#if TRINAMIC_ENABLE == 2660
#ifndef R_SENSE
#define R_SENSE 50
#endif
#include "../trinamic/tmc2660hal.h"
#endif
#if TRINAMIC_ENABLE == 5160
#ifndef R_SENSE
#define R_SENSE 75
#endif
#include "../trinamic/tmc5160hal.h"
#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
#if PWM_THRESHOLD_VELOCITY > 0 && TRINAMIC_ENABLE == 2660
#error "TMC2660 does not support PWM_THRESHOLD_VELOCITY"
#endif
#ifndef TMC_STEALTHCHOP
#define TMC_STEALTHCHOP 0 // 0 = CoolStep, 1 = StealthChop
#endif
#if TMC_STEALTHCHOP > 0 && TRINAMIC_ENABLE == 2660
#error "TMC2660 does not support StealthChop mode"
#endif
#if TRINAMIC_ENABLE == 2209
#define TMC_STALLGUARD 4 // Do not change!
#else
#define TMC_STALLGUARD 2 // Do not change!
#endif
#ifndef TRINAMIC_POLL_STATUS
#define TRINAMIC_POLL_STATUS 0
#endif
#ifndef TRINAMIC_DYNAMIC_CURRENT
#define TRINAMIC_DYNAMIC_CURRENT 0
#endif
//#define TMC_HOMING_ACCELERATION 50.0f // NOT tested... Reduce acceleration during homing to avoid falsely triggering DIAG output
// The following parameters will default to driver specific values.
//
//#define TMC_DRVCONF 0x? // consult the driver datasheet to determine the value to use.
//#define TMC_COOLCONF_SEMIN 5 // Range: 0 - 15, 0 = CoolStep off
//#define TMC_COOLCONF_SEMAX 2 // Range: 0 - 15
//#define TMC_COOLCONF_SEDN 1 // Range: 0 - 3
//#define TMC_COOLCONF_SEUP 1 // Range: 0 - 3
//#define TMC_COOLCONF_SEIMIN 0 // 0 or 1
//#define TMC_CHOPCONF_HSTRT 1 // Range: 1 - 8
//#define TMC_CHOPCONF_HEND 2 // Range: -3 - 12
//#define TMC_CHOPCONF_TBL 1 // 0 = 16, 1 = 24, 2 = 36, 3 = 54 clocks
//#define TMC_CHOPCONF_TOFF 5 // Range: 1 - 15
//#define TMC_CHOPCONF_RDNTF 0 // 0 = fixed, 1 = random
//#define TMC_CHOPCONF_CHM 0 // 0 = Spreadcycle, 1 = constant off time
//#define TMC_CHOPCONF_HDEC 0 // TMC2260 only
//#define TMC_CHOPCONF_TFD 0 // Range: 0 - 15
//#define TMC_CHOPCONF_INTPOL 1 // 0 or 1
//
// 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
#define TMC_X_MICROSTEPS TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_X_R_SENSE R_SENSE // mOhm
#ifndef TMC_X_CURRENT
#define TMC_X_CURRENT DEFAULT_X_CURRENT // mA RMS
#endif
#ifndef TMC_X_HOLD_CURRENT_PCT
#define TMC_X_HOLD_CURRENT_PCT 50
#endif
#define TMC_X_HOMING_SEEK_SGT 22
#define TMC_X_HOMING_FEED_SGT 22
#define TMC_X_STEALTHCHOP TMC_STEALTHCHOP
#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 TMC_Y_CURRENT
#define TMC_Y_CURRENT DEFAULT_Y_CURRENT // mA RMS
#endif
#ifndef TMC_Y_HOLD_CURRENT_PCT
#define TMC_Y_HOLD_CURRENT_PCT 50
#endif
#define TMC_Y_HOMING_SEEK_SGT 22
#define TMC_Y_HOMING_FEED_SGT 22
#define TMC_Y_STEALTHCHOP TMC_STEALTHCHOP
#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 TMC_Z_CURRENT
#define TMC_Z_CURRENT DEFAULT_Z_CURRENT // mA RMS
#endif
#ifndef TMC_Z_HOLD_CURRENT_PCT
#define TMC_Z_HOLD_CURRENT_PCT 50
#endif
#define TMC_Z_HOMING_SEEK_SGT 22
#define TMC_Z_HOMING_FEED_SGT 22
#define TMC_Z_STEALTHCHOP TMC_STEALTHCHOP
#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 TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_A_R_SENSE R_SENSE // mOhm
#ifndef TMC_A_CURRENT
#define TMC_A_CURRENT DEFAULT_A_CURRENT // mA RMS
#endif
#ifndef TMC_A_HOLD_CURRENT_PCT
#define TMC_A_HOLD_CURRENT_PCT 50
#endif
#define TMC_A_HOMING_SEEK_SGT 22
#define TMC_A_HOMING_FEED_SGT 22
#define TMC_A_STEALTHCHOP TMC_STEALTHCHOP
#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 TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_B_R_SENSE R_SENSE // mOhm
#ifndef TMC_B_CURRENT
#define TMC_B_CURRENT DEFAULT_B_CURRENT // mA RMS
#endif
#ifndef TMC_B_HOLD_CURRENT_PCT
#define TMC_B_HOLD_CURRENT_PCT 50
#endif
#define TMC_B_HOMING_SEEK_SGT 22
#define TMC_B_HOMING_FEED_SGT 22
#define TMC_B_STEALTHCHOP TMC_STEALTHCHOP
#endif
#ifdef C_AXIS
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_C_ENABLE
#define TMC_C_ENABLE 0
#endif
#else
#define TMC_C_ENABLE 1 // Do not change
#endif
#define TMC_C_MONITOR 1
#define TMC_C_MICROSTEPS TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_C_R_SENSE R_SENSE // mOhm
#ifndef TMC_C_CURRENT
#define TMC_C_CURRENT DEFAULT_C_CURRENT // mA RMS
#endif
#ifndef TMC_C_HOLD_CURRENT_PCT
#define TMC_C_HOLD_CURRENT_PCT 50
#endif
#define TMC_C_HOMING_SEEK_SGT 22
#define TMC_C_HOMING_FEED_SGT 22
#define TMC_C_STEALTHCHOP TMC_STEALTHCHOP
#endif
#ifdef U_AXIS
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_U_ENABLE
#define TMC_U_ENABLE 0
#endif
#else
#define TMC_U_ENABLE 1 // Do not change
#endif
#define TMC_U_MONITOR 1
#define TMC_U_MICROSTEPS TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_U_R_SENSE R_SENSE // mOhm
#ifndef TMC_U_CURRENT
#define TMC_U_CURRENT DEFAULT_U_CURRENT // mA RMS
#endif
#ifndef TMC_U_HOLD_CURRENT_PCT
#define TMC_U_HOLD_CURRENT_PCT 50
#endif
#define TMC_U_HOMING_SEEK_SGT 22
#define TMC_U_HOMING_FEED_SGT 22
#define TMC_U_STEALTHCHOP TMC_STEALTHCHOP
#endif
#ifdef V_AXIS
#if TRINAMIC_MIXED_DRIVERS
#ifndef TMC_V_ENABLE
#define TMC_V_ENABLE 0
#endif
#else
#define TMC_V_ENABLE 1 // Do not change
#endif
#define TMC_V_MONITOR 1
#define TMC_V_MICROSTEPS TRINAMIC_DEFAULT_MICROSTEPS
#define TMC_V_R_SENSE R_SENSE // mOhm
#ifndef TMC_V_CURRENT
#define TMC_V_CURRENT DEFAULT_V_CURRENT // mA RMS
#endif
#ifndef TMC_V_HOLD_CURRENT_PCT
#define TMC_V_HOLD_CURRENT_PCT 50
#endif
#define TMC_V_HOMING_SEEK_SGT 22
#define TMC_V_HOMING_FEED_SGT 22
#define TMC_V_STEALTHCHOP TMC_STEALTHCHOP
#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 TRINAMIC_EXTENDED_SETTINGS
trinamic_cfg_t cfg_params;
#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