-
Notifications
You must be signed in to change notification settings - Fork 22
/
tmc2130.c
346 lines (282 loc) · 12.2 KB
/
tmc2130.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
/*
* tmc2130.c - interface for Trinamic TMC2130 stepper driver
*
* v0.0.11 / 2024-11-17
*/
/*
Copyright (c) 2018-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.
*/
/*
* Reference for calculations:
* https://www.trinamic.com/fileadmin/assets/Products/ICs_Documents/TMC5130_TMC2130_TMC2100_Calculations.xlsx
*
*/
#include <math.h>
#include <string.h>
#include "tmc2130.h"
static const trinamic_cfg_params_t cfg_params = {
.vsense[0] = 325.0f,
.vsense[1] = 180.0f,
.cap.drvconf = 0,
.cap.coolconf.seup = 0b11,
.cap.coolconf.sedn = 0b11,
.cap.coolconf.semax = 0b1111,
.cap.coolconf.semin = 0b1111,
.cap.coolconf.seimin = 1,
.cap.chopconf.toff = 0b1111,
.cap.chopconf.hstrt = 0b111,
.cap.chopconf.hend = 0b1111,
.cap.chopconf.rndtf = 1,
.cap.chopconf.chm = 1,
.cap.chopconf.tbl = 0b11,
.dflt.drvconf = 0,
.dflt.coolconf.seup = TMC2130_SEUP,
.dflt.coolconf.sedn = TMC2130_SEDN,
.dflt.coolconf.semax = TMC2130_SEMAX,
.dflt.coolconf.semin = TMC2130_SEMIN,
.dflt.coolconf.seimin = TMC2130_SEIMIN,
.dflt.chopconf.toff = TMC2130_TOFF,
.dflt.chopconf.hstrt = TMC2130_HSTRT - 1,
.dflt.chopconf.hend = TMC2130_HEND + 3,
.dflt.chopconf.rndtf = TMC2130_RNDTF,
.dflt.chopconf.chm = TMC2130_CHM,
.dflt.chopconf.tbl = TMC2130_TBL
};
static const TMC2130_t tmc2130_defaults = {
.config.f_clk = TMC2130_F_CLK,
.config.mode = TMC2130_MODE,
.config.r_sense = TMC2130_R_SENSE,
.config.current = TMC2130_CURRENT,
.config.hold_current_pct = TMC2130_HOLD_CURRENT_PCT,
.config.microsteps = TMC2130_MICROSTEPS,
// register adresses
.gconf.addr.reg = TMC2130Reg_GCONF,
.gconf.reg.en_pwm_mode = TMC2130_EN_PWM_MODE,
.gstat.addr.reg = TMC2130Reg_GSTAT,
.ioin.addr.reg = TMC2130Reg_IOIN,
.ihold_irun.addr.reg = TMC2130Reg_IHOLD_IRUN,
.ihold_irun.reg.iholddelay = TMC2130_IHOLDDELAY,
.tpowerdown.addr.reg = TMC2130Reg_TPOWERDOWN,
.tpowerdown.reg.tpowerdown = TMC2130_TPOWERDOWN,
.tstep.addr.reg = TMC2130Reg_TSTEP,
.tpwmthrs.addr.reg = TMC2130Reg_TPWMTHRS,
.tpwmthrs.reg.tpwmthrs = TMC2130_TPWM_THRS,
.tcoolthrs.addr.reg = TMC2130Reg_TCOOLTHRS,
.tcoolthrs.reg.tcoolthrs = TMC2130_COOLSTEP_THRS,
.thigh.addr.reg = TMC2130Reg_THIGH,
.vdcmin.addr.reg = TMC2130Reg_VDCMIN,
.mscnt.addr.reg = TMC2130Reg_MSCNT,
.mscuract.addr.reg = TMC2130Reg_MSCURACT,
.chopconf.addr.reg = TMC2130Reg_CHOPCONF,
.chopconf.reg.intpol = TMC2130_INTPOL,
.chopconf.reg.toff = TMC2130_TOFF,
.chopconf.reg.chm = TMC2130_CHM,
.chopconf.reg.tbl = TMC2130_TBL,
.chopconf.reg.rndtf = TMC2130_RNDTF,
.chopconf.reg.hend = TMC2130_HEND + 3,
#if TMC2130_CHM == 0
.chopconf.reg.hstrt = TMC2130_HSTRT - 1,
#else
.chopconf.reg.fd3 = (TMC2130_TFD & 0x08) >> 3,
.chopconf.reg.hstrt = TMC2130_TFD & 0x07,
#endif
.coolconf.addr.reg = TMC2130Reg_COOLCONF,
.coolconf.reg.semin = TMC2130_SEMIN,
.coolconf.reg.seup = TMC2130_SEUP,
.coolconf.reg.semax = TMC2130_SEMAX,
.coolconf.reg.sedn = TMC2130_SEDN,
.coolconf.reg.seimin = TMC2130_SEIMIN,
.dcctrl.addr.reg = TMC2130Reg_DCCTRL,
.drv_status.addr.reg = TMC2130Reg_DRV_STATUS,
.pwmconf.addr.reg = TMC2130Reg_PWMCONF,
.pwmconf.reg.pwm_autoscale = TMC2130_PWM_AUTOSCALE,
.pwmconf.reg.pwm_ampl = TMC2130_PWM_AMPL,
.pwmconf.reg.pwm_grad = TMC2130_PWM_GRAD,
.pwmconf.reg.pwm_freq = TMC2130_PWM_FREQ,
.pwm_scale.addr.reg = TMC2130Reg_PWM_SCALE,
.lost_steps.addr.reg = TMC2130Reg_LOST_STEPS,
#ifdef TMC2130_COMPLETE
.xdirect.addr.reg = TMC2130Reg_XDIRECT,
.mslut[0].addr.reg = TMC2130Reg_MSLUT_BASE,
.mslut[1].addr.reg = (tmc2130_regaddr_t)(TMC2130Reg_MSLUT_BASE + 1),
.mslut[2].addr.reg = (tmc2130_regaddr_t)(TMC2130Reg_MSLUT_BASE + 2),
.mslut[3].addr.reg = (tmc2130_regaddr_t)(TMC2130Reg_MSLUT_BASE + 3),
.mslut[4].addr.reg = (tmc2130_regaddr_t)(TMC2130Reg_MSLUT_BASE + 4),
.mslut[5].addr.reg = (tmc2130_regaddr_t)(TMC2130Reg_MSLUT_BASE + 5),
.mslut[6].addr.reg = (tmc2130_regaddr_t)(TMC2130Reg_MSLUT_BASE + 6),
.mslut[7].addr.reg = (tmc2130_regaddr_t)(TMC2130Reg_MSLUT_BASE + 7),
.mslutsel.addr.reg = TMC2130Reg_MSLUTSEL,
.mslutstart.addr.reg = TMC2130Reg_MSLUTSTART,
.encm_ctrl.addr.reg = TMC2130Reg_ENCM_CTRL,
#endif
};
const trinamic_cfg_params_t *TMC2130_GetConfigDefaults (void)
{
return &cfg_params;
}
static void _set_rms_current (TMC2130_t *driver)
{
float maxv = (((float)(driver->config.r_sense + 20)) * (float)(32UL * driver->config.current)) * 1.41421f / 1000.0f;
int8_t current_scaling = (int8_t)(maxv / cfg_params.vsense[0]) - 1;
// If the current scaling is too low set the vsense bit and recalculate the current setting
if ((driver->chopconf.reg.vsense = (current_scaling < 16)))
current_scaling = (int8_t)(maxv / cfg_params.vsense[1]) - 1;
driver->ihold_irun.reg.irun = current_scaling > 31 ? 31 : current_scaling;
driver->ihold_irun.reg.ihold = (driver->ihold_irun.reg.irun * driver->config.hold_current_pct) / 100;
}
void TMC2130_SetDefaults (TMC2130_t *driver)
{
memcpy(driver, &tmc2130_defaults, sizeof(TMC2130_t));
_set_rms_current(driver);
driver->chopconf.reg.mres = tmc_microsteps_to_mres(driver->config.microsteps);
}
bool TMC2130_Init (TMC2130_t *driver)
{
// Read drv_status to check if driver is online
tmc_spi_read(driver->config.motor, (TMC_spi_datagram_t *)&driver->drv_status);
if(driver->drv_status.reg.value == 0 || driver->drv_status.reg.value == 0xFFFFFFFF)
return false;
// Perform a status register read to clear reset flag
tmc_spi_read(driver->config.motor, (TMC_spi_datagram_t *)&driver->gstat);
driver->chopconf.reg.mres = tmc_microsteps_to_mres(driver->config.microsteps);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->gconf);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->coolconf);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->pwmconf);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->tpowerdown);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->tpwmthrs);
TMC2130_SetCurrent(driver, driver->config.current, driver->config.hold_current_pct);
// Read back chopconf to check if driver is online
uint32_t chopconf = driver->chopconf.reg.value;
tmc_spi_read(driver->config.motor, (TMC_spi_datagram_t *)&driver->chopconf);
return driver->chopconf.reg.value == chopconf;
}
uint16_t TMC2130_GetCurrent (TMC2130_t *driver, trinamic_current_t type)
{
uint8_t cs;
bool vsense;
switch(type) {
case TMCCurrent_Max:
cs = 31;
vsense = 0;
break;
case TMCCurrent_Actual:
cs = driver->ihold_irun.reg.irun;
vsense = driver->chopconf.reg.vsense;
break;
case TMCCurrent_Hold:
cs = driver->ihold_irun.reg.ihold;
vsense = driver->chopconf.reg.vsense;
break;
default: // TMCCurrent_Min:
cs = 0;
vsense = 1;
break;
}
return (uint16_t)ceilf((float)(cs + 1) / 32.0f * cfg_params.vsense[vsense] / (float)(driver->config.r_sense + 20) / 1.41421f * 1000.0f);
}
// r_sense = mOhm, Vsense = mV, current = mA (RMS)
void TMC2130_SetCurrent (TMC2130_t *driver, uint16_t mA, uint8_t hold_pct)
{
driver->config.current = mA;
driver->config.hold_current_pct = hold_pct;
_set_rms_current(driver);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->chopconf);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->ihold_irun);
}
float TMC2130_GetTPWMTHRS (TMC2130_t *driver, float steps_mm)
{
return tmc_calc_tstep_inv(&driver->config, driver->tpwmthrs.reg.tpwmthrs, steps_mm);
}
void TMC2130_SetTPWMTHRS (TMC2130_t *driver, float mm_sec, float steps_mm)
{
driver->tpwmthrs.reg.tpwmthrs = tmc_calc_tstep(&driver->config, mm_sec, steps_mm);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->tpwmthrs);
}
void TMC2130_SetTHIGH (TMC2130_t *driver, float mm_sec, float steps_mm) // -> pwm threshold
{
driver->thigh.reg.thigh = tmc_calc_tstep(&driver->config, mm_sec, steps_mm);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->thigh);
}
void TMC2130_SetTCOOLTHRS (TMC2130_t *driver, float mm_sec, float steps_mm) // -> pwm threshold
{
driver->tcoolthrs.reg.tcoolthrs = tmc_calc_tstep(&driver->config, mm_sec, steps_mm);
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->tcoolthrs);
}
// 1 - 256 in steps of 2^value is valid for TMC2130
bool TMC2130_MicrostepsIsValid (uint16_t usteps)
{
return tmc_microsteps_validate(usteps);
}
void TMC2130_SetMicrosteps (TMC2130_t *driver, tmc2130_microsteps_t msteps)
{
driver->chopconf.reg.mres = tmc_microsteps_to_mres(msteps);
driver->config.microsteps = (tmc2130_microsteps_t)(1 << (8 - driver->chopconf.reg.mres));
// TODO: recalc and set hybrid threshold if enabled?
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->chopconf);
}
void TMC2130_SetConstantOffTimeChopper (TMC2130_t *driver, uint8_t constant_off_time, uint8_t blank_time, uint8_t fast_decay_time, int8_t sine_wave_offset, bool use_current_comparator)
{
//calculate the value acc to the clock cycles
if (blank_time >= 54)
blank_time = 3;
else if (blank_time >= 36)
blank_time = 2;
else if (blank_time >= 24)
blank_time = 1;
else
blank_time = 0;
if (fast_decay_time > 15)
fast_decay_time = 15;
if(driver->chopconf.reg.chm)
driver->chopconf.reg.fd3 = (fast_decay_time & 0x8) >> 3;
driver->chopconf.reg.tbl = blank_time;
driver->chopconf.reg.toff = constant_off_time < 2 ? 2 : (constant_off_time > 15 ? 15 : constant_off_time);
driver->chopconf.reg.hstrt = fast_decay_time & 0x7;
driver->chopconf.reg.hend = (sine_wave_offset < -3 ? -3 : (sine_wave_offset > 12 ? 12 : sine_wave_offset)) + 3;
driver->chopconf.reg.rndtf = !use_current_comparator;
tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)&driver->chopconf);
}
TMC2130_status_t TMC2130_WriteRegister (TMC2130_t *driver, TMC2130_datagram_t *reg)
{
TMC2130_status_t status;
status.value = tmc_spi_write(driver->config.motor, (TMC_spi_datagram_t *)reg);
return status;
}
TMC2130_status_t TMC2130_ReadRegister (TMC2130_t *driver, TMC2130_datagram_t *reg)
{
TMC2130_status_t status;
status.value = tmc_spi_read(driver->config.motor, (TMC_spi_datagram_t *)reg);
return status;
}
// Returns pointer to shadow register or NULL if not found
TMC2130_datagram_t *TMC2130_GetRegPtr (TMC2130_t *driver, tmc2130_regaddr_t reg)
{
TMC2130_datagram_t *ptr = (TMC2130_datagram_t *)driver;
while(ptr && ptr->addr.reg != reg) {
ptr++;
if(ptr->addr.reg == TMC2130Reg_LOST_STEPS && ptr->addr.reg != reg)
ptr = NULL;
}
return ptr;
}