-
Notifications
You must be signed in to change notification settings - Fork 21
/
tmc2209hal.c
378 lines (293 loc) · 11.6 KB
/
tmc2209hal.c
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
/*
* tmc2209hal.c - interface for Trinamic TMC2209 stepper driver
*
* v0.0.10 / 2024-09-28
*/
/*
Copyright (c) 2021-2024, Terje Io
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission..
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include "grbl/hal.h"
#include "tmc2209.h"
#include "tmchal.h"
static TMC2209_t *tmcdriver[6];
static trinamic_config_t *getConfig (uint8_t motor)
{
return &tmcdriver[motor]->config;
}
static bool isValidMicrosteps (uint8_t motor, uint16_t msteps)
{
return tmc_microsteps_validate(msteps);
}
static void setMicrosteps (uint8_t motor, uint16_t msteps)
{
TMC2209_SetMicrosteps(tmcdriver[motor], (tmc2209_microsteps_t)msteps);
}
static void setCurrent (uint8_t motor, uint16_t mA, uint8_t hold_pct)
{
TMC2209_SetCurrent(tmcdriver[motor], mA, hold_pct);
}
static uint16_t getCurrent (uint8_t motor, trinamic_current_t type)
{
return TMC2209_GetCurrent(tmcdriver[motor], type);
}
static TMC_chopconf_t getChopconf (uint8_t motor)
{
TMC_chopconf_t chopconf;
TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->chopconf);
chopconf.mres = tmcdriver[motor]->chopconf.reg.mres;
chopconf.toff = tmcdriver[motor]->chopconf.reg.toff;
chopconf.tbl = tmcdriver[motor]->chopconf.reg.tbl;
chopconf.hend = tmcdriver[motor]->chopconf.reg.hend;
chopconf.hstrt = tmcdriver[motor]->chopconf.reg.hstrt;
return chopconf;
}
static uint32_t getStallGuardResult (uint8_t motor)
{
TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->sg_result);
return (uint32_t)tmcdriver[motor]->sg_result.reg.result;
}
static TMC_drv_status_t getDriverStatus (uint8_t motor)
{
TMC_drv_status_t drv_status = {0};
TMC2209_status_t status;
TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->sg_result);
status.value = TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->drv_status);
drv_status.driver_error = status.driver_error;
drv_status.sg_result = tmcdriver[motor]->sg_result.reg.result;
drv_status.ot = tmcdriver[motor]->drv_status.reg.ot;
drv_status.otpw = tmcdriver[motor]->drv_status.reg.otpw;
drv_status.cs_actual = tmcdriver[motor]->drv_status.reg.cs_actual;
drv_status.stst = tmcdriver[motor]->drv_status.reg.stst;
// drv_status.fsactive = tmcdriver[motor]->drv_status.reg.fsactive;
drv_status.ola = tmcdriver[motor]->drv_status.reg.ola;
drv_status.olb = tmcdriver[motor]->drv_status.reg.olb;
drv_status.s2ga = tmcdriver[motor]->drv_status.reg.s2ga;
drv_status.s2gb = tmcdriver[motor]->drv_status.reg.s2gb;
return drv_status;
}
static TMC_ihold_irun_t getIholdIrun (uint8_t motor)
{
TMC_ihold_irun_t ihold_irun;
ihold_irun.ihold = tmcdriver[motor]->ihold_irun.reg.ihold;
ihold_irun.irun = tmcdriver[motor]->ihold_irun.reg.irun;
ihold_irun.iholddelay = tmcdriver[motor]->ihold_irun.reg.iholddelay;
return ihold_irun;
}
static uint32_t getDriverStatusRaw (uint8_t motor)
{
TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->drv_status);
return tmcdriver[motor]->drv_status.reg.value;
}
static uint32_t getTStep (uint8_t motor)
{
TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->tstep);
return (uint32_t)tmcdriver[motor]->tstep.reg.tstep;
}
static void setTCoolThrs (uint8_t motor, float mm_sec, float steps_mm)
{
TMC2209_SetTCOOLTHRS(tmcdriver[motor], mm_sec, steps_mm);
}
static void setTCoolThrsRaw (uint8_t motor, uint32_t value)
{
tmcdriver[motor]->tcoolthrs.reg.tcoolthrs = value;
TMC2209_WriteRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->tcoolthrs);
}
static void stallGuardEnable (uint8_t motor, float feed_rate, float steps_mm, int16_t sensitivity)
{
TMC2209_t *driver = tmcdriver[motor];
driver->gconf.reg.en_spreadcycle = false; // stealthChop on
TMC2209_WriteRegister(driver, (TMC2209_datagram_t *)&driver->gconf);
driver->pwmconf.reg.pwm_autoscale = true;
TMC2209_WriteRegister(driver, (TMC2209_datagram_t *)&driver->pwmconf);
TMC2209_SetTCOOLTHRS(driver, feed_rate / (60.0f * 1.5f), steps_mm);
driver->sgthrs.reg.threshold = (uint8_t)sensitivity;
TMC2209_WriteRegister(driver, (TMC2209_datagram_t *)&driver->sgthrs);
}
static void stealthChopEnable (uint8_t motor)
{
TMC2209_t *driver = tmcdriver[motor];
driver->gconf.reg.en_spreadcycle = false; // stealthChop on
TMC2209_WriteRegister(driver, (TMC2209_datagram_t *)&driver->gconf);
driver->pwmconf.reg.pwm_autoscale = true;
TMC2209_WriteRegister(driver, (TMC2209_datagram_t *)&driver->pwmconf);
setTCoolThrsRaw(motor, 0);
}
static void coolStepEnable (uint8_t motor)
{
TMC2209_t *driver = tmcdriver[motor];
driver->gconf.reg.en_spreadcycle = true; // stealthChop off
TMC2209_WriteRegister(driver, (TMC2209_datagram_t *)&driver->gconf);
driver->pwmconf.reg.pwm_autoscale = false;
TMC2209_WriteRegister(driver, (TMC2209_datagram_t *)&driver->pwmconf);
setTCoolThrsRaw(motor, 0);
}
static float getTPWMThrs (uint8_t motor, float steps_mm)
{
return TMC2209_GetTPWMTHRS(tmcdriver[motor], steps_mm);
}
static uint32_t getTPWMThrsRaw (uint8_t motor)
{
return tmcdriver[motor]->tpwmthrs.reg.tpwmthrs;
}
static void setTPWMThrs (uint8_t motor, float mm_sec, float steps_mm)
{
TMC2209_SetTPWMTHRS(tmcdriver[motor], mm_sec, steps_mm);
}
static void stealthChop (uint8_t motor, bool on)
{
tmcdriver[motor]->config.mode = on ? TMCMode_StealthChop : TMCMode_CoolStep;
if(on)
stealthChopEnable(motor);
else
coolStepEnable(motor);
}
static bool stealthChopGet (uint8_t motor)
{
return !tmcdriver[motor]->gconf.reg.en_spreadcycle && tmcdriver[motor]->pwmconf.reg.pwm_autoscale;
}
// coolconf
static void sg_filter (uint8_t motor, bool val)
{
// tmcdriver[motor]->sgthrs.reg.threshold = val;
// TMC2209_WriteRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->coolconf);
}
static void sg_stall_value (uint8_t motor, int16_t val)
{
tmcdriver[motor]->sgthrs.reg.threshold = (uint8_t)val;
TMC2209_WriteRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->sgthrs);
}
static int16_t get_sg_stall_value (uint8_t motor)
{
return (int16_t)tmcdriver[motor]->sgthrs.reg.threshold;
}
static void coolconf (uint8_t motor, trinamic_coolconf_t coolconf)
{
TMC2209_t *driver = tmcdriver[motor];
driver->coolconf.reg.semin = coolconf.semin;
driver->coolconf.reg.semax = coolconf.semax;
driver->coolconf.reg.sedn = coolconf.sedn;
driver->coolconf.reg.seimin = coolconf.seimin;
driver->coolconf.reg.seup = coolconf.seup;
TMC2209_WriteRegister(tmcdriver[motor], (TMC2209_datagram_t *)&driver->coolconf);
}
// chopconf
static void chopper_timing (uint8_t motor, trinamic_chopconf_t chopconf)
{
TMC2209_t *driver = tmcdriver[motor];
driver->chopconf.reg.hstrt = chopconf.hstrt;
driver->chopconf.reg.hend = chopconf.hend;
driver->chopconf.reg.tbl = chopconf.tbl;
driver->chopconf.reg.toff = chopconf.toff;
TMC2209_WriteRegister(tmcdriver[motor], (TMC2209_datagram_t *)&driver->chopconf);
}
static uint8_t pwm_scale (uint8_t motor)
{
TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->pwm_scale);
return tmcdriver[motor]->pwm_scale.reg.pwm_scale_sum;
}
static bool vsense (uint8_t motor)
{
TMC2209_ReadRegister(tmcdriver[motor], (TMC2209_datagram_t *)&tmcdriver[motor]->chopconf);
return tmcdriver[motor]->chopconf.reg.vsense;
}
static bool read_register (uint8_t motor, uint8_t addr, uint32_t *val)
{
TMC2209_datagram_t reg;
reg.addr.reg = (tmc2209_regaddr_t)addr;
reg.addr.write = 1;
TMC2209_ReadRegister(tmcdriver[motor], ®);
*val = reg.payload.value;
return true;
}
static bool write_register (uint8_t motor, uint8_t addr, uint32_t val)
{
TMC2209_datagram_t reg;
reg.addr.reg = (tmc2209_regaddr_t)addr;
reg.addr.write = 0;
reg.payload.value = val;
TMC2209_WriteRegister(tmcdriver[motor], ®);
return true;
}
static void *get_register_addr (uint8_t motor, uint8_t addr)
{
return TMC2209_GetRegPtr(tmcdriver[motor], (tmc2209_regaddr_t)addr);
}
static const tmchal_t tmc_hal = {
.driver = TMC2209,
.name = "TMC2209",
.get_config = getConfig,
.microsteps_isvalid = isValidMicrosteps,
.set_microsteps = setMicrosteps,
.set_current = setCurrent,
.get_current = getCurrent,
.get_chopconf = getChopconf,
.get_tstep = getTStep,
.get_drv_status = getDriverStatus,
.get_drv_status_raw = getDriverStatusRaw,
.set_tcoolthrs = setTCoolThrs,
.set_tcoolthrs_raw = setTCoolThrsRaw,
.set_thigh = NULL,
.set_thigh_raw = NULL,
.stallguard_enable = stallGuardEnable,
.stealthchop_enable = stealthChopEnable,
.coolstep_enable = coolStepEnable,
.get_sg_result = getStallGuardResult,
.get_tpwmthrs = getTPWMThrs,
.get_tpwmthrs_raw = getTPWMThrsRaw,
.set_tpwmthrs = setTPWMThrs,
.get_en_pwm_mode = stealthChopGet,
.get_ihold_irun = getIholdIrun,
.stealthChop = stealthChop,
.sg_filter = sg_filter,
.sg_stall_value = sg_stall_value,
.get_sg_stall_value = get_sg_stall_value,
.coolconf = coolconf,
.vsense = vsense,
.pwm_scale = pwm_scale,
.chopper_timing = chopper_timing,
.get_register_addr = get_register_addr,
.read_register = read_register,
.write_register = write_register
};
const tmchal_t *TMC2209_AddMotor (motor_map_t motor, uint8_t address, uint16_t current, uint8_t microsteps, uint8_t r_sense)
{
bool ok = !!tmcdriver[motor.id];
if(ok || (ok = (tmcdriver[motor.id] = malloc(sizeof(TMC2209_t))) != NULL)) {
TMC2209_SetDefaults(tmcdriver[motor.id]);
tmcdriver[motor.id]->config.motor.id = motor.id;
tmcdriver[motor.id]->config.motor.address = address;
tmcdriver[motor.id]->config.motor.axis = motor.axis;
tmcdriver[motor.id]->config.current = current;
tmcdriver[motor.id]->config.microsteps = microsteps;
tmcdriver[motor.id]->config.r_sense = r_sense;
tmcdriver[motor.id]->chopconf.reg.mres = tmc_microsteps_to_mres(microsteps);
}
if(ok && !(ok = TMC2209_Init(tmcdriver[motor.id]))) {
free(tmcdriver[motor.id]);
tmcdriver[motor.id] = NULL;
}
return ok ? &tmc_hal : NULL;
}