a20bd6dbf73b5a4468445901953b242b3e8bc8b0
[clinton/Smoothieware.git] / src / modules / utils / motordrivercontrol / drivers / TMC26X / TMC26X.cpp
1 /*
2 Highly modifed from....
3
4 TMC26X.cpp - - TMC26X Stepper library for Wiring/Arduino
5
6 based on the stepper library by Tom Igoe, et. al.
7
8 Copyright (c) 2011, Interactive Matter, Marcus Nowotny
9
10 Permission is hereby granted, free of charge, to any person obtaining a copy
11 of this software and associated documentation files (the "Software"), to deal
12 in the Software without restriction, including without limitation the rights
13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 copies of the Software, and to permit persons to whom the Software is
15 furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 THE SOFTWARE.
27
28 */
29
30 #include "TMC26X.h"
31 #include "mbed.h"
32 #include "StreamOutput.h"
33 #include "Kernel.h"
34 #include "libs/StreamOutputPool.h"
35 #include "Robot.h"
36 #include "StepperMotor.h"
37
38 //! return value for TMC26X.getOverTemperature() if there is a overtemperature situation in the TMC chip
39 /*!
40 * This warning indicates that the TCM chip is too warm.
41 * It is still working but some parameters may be inferior.
42 * You should do something against it.
43 */
44 #define TMC26X_OVERTEMPERATURE_PREWARING 1
45 //! return value for TMC26X.getOverTemperature() if there is a overtemperature shutdown in the TMC chip
46 /*!
47 * This warning indicates that the TCM chip is too warm to operate and has shut down to prevent damage.
48 * It will stop working until it cools down again.
49 * If you encouter this situation you must do something against it. Like reducing the current or improving the PCB layout
50 * and/or heat management.
51 */
52 #define TMC26X_OVERTEMPERATURE_SHUTDOWN 2
53
54 //which values can be read out
55 /*!
56 * Selects to readout the microstep position from the motor.
57 *\sa readStatus()
58 */
59 #define TMC26X_READOUT_POSITION 0
60 /*!
61 * Selects to read out the StallGuard value of the motor.
62 *\sa readStatus()
63 */
64 #define TMC26X_READOUT_STALLGUARD 1
65 /*!
66 * Selects to read out the current current setting (acc. to CoolStep) and the upper bits of the StallGuard value from the motor.
67 *\sa readStatus(), setCurrent()
68 */
69 #define TMC26X_READOUT_CURRENT 3
70
71 /*!
72 * Define to set the minimum current for CoolStep operation to 1/2 of the selected CS minium.
73 *\sa setCoolStepConfiguration()
74 */
75 #define COOL_STEP_HALF_CS_LIMIT 0
76 /*!
77 * Define to set the minimum current for CoolStep operation to 1/4 of the selected CS minium.
78 *\sa setCoolStepConfiguration()
79 */
80 #define COOL_STEP_QUARTDER_CS_LIMIT 1
81
82
83 //some default values used in initialization
84 #define DEFAULT_MICROSTEPPING_VALUE 32
85
86 //TMC26X register definitions
87 #define DRIVER_CONTROL_REGISTER 0x00000ul
88 #define CHOPPER_CONFIG_REGISTER 0x80000ul
89 #define COOL_STEP_REGISTER 0xA0000ul
90 #define STALL_GUARD2_LOAD_MEASURE_REGISTER 0xC0000ul
91 #define DRIVER_CONFIG_REGISTER 0xE0000ul
92
93 #define REGISTER_BIT_PATTERN 0xFFFFFul
94
95 //definitions for the driver control register DRVCTL
96 #define MICROSTEPPING_PATTERN 0x000Ful
97 #define STEP_INTERPOLATION 0x0200ul
98 #define DOUBLE_EDGE_STEP 0x0100ul
99
100 //definitions for the driver config register DRVCONF
101 #define READ_MICROSTEP_POSITION 0x0000ul
102 #define READ_STALL_GUARD_READING 0x0010ul
103 #define READ_STALL_GUARD_AND_COOL_STEP 0x0020ul
104 #define READ_SELECTION_PATTERN 0x0030ul
105 #define VSENSE 0x0040ul
106
107 //definitions for the chopper config register
108 #define CHOPPER_MODE_STANDARD 0x00000ul
109 #define CHOPPER_MODE_T_OFF_FAST_DECAY 0x04000ul
110 #define T_OFF_PATTERN 0x0000ful
111 #define RANDOM_TOFF_TIME 0x02000ul
112 #define BLANK_TIMING_PATTERN 0x18000ul
113 #define BLANK_TIMING_SHIFT 15
114 #define HYSTERESIS_DECREMENT_PATTERN 0x01800ul
115 #define HYSTERESIS_DECREMENT_SHIFT 11
116 #define HYSTERESIS_LOW_VALUE_PATTERN 0x00780ul
117 #define HYSTERESIS_LOW_SHIFT 7
118 #define HYSTERESIS_START_VALUE_PATTERN 0x00078ul
119 #define HYSTERESIS_START_VALUE_SHIFT 4
120 #define T_OFF_TIMING_PATERN 0x0000Ful
121
122 //definitions for cool step register
123 #define MINIMUM_CURRENT_FOURTH 0x8000ul
124 #define CURRENT_DOWN_STEP_SPEED_PATTERN 0x6000ul
125 #define SE_MAX_PATTERN 0x0F00ul
126 #define SE_CURRENT_STEP_WIDTH_PATTERN 0x0060ul
127 #define SE_MIN_PATTERN 0x000Ful
128
129 //definitions for stall guard2 current register
130 #define STALL_GUARD_FILTER_ENABLED 0x10000ul
131 #define STALL_GUARD_TRESHHOLD_VALUE_PATTERN 0x17F00ul
132 #define CURRENT_SCALING_PATTERN 0x0001Ful
133 #define STALL_GUARD_CONFIG_PATTERN 0x17F00ul
134 #define STALL_GUARD_VALUE_PATTERN 0x07F00ul
135
136 //definitions for the input from the TCM260
137 #define STATUS_STALL_GUARD_STATUS 0x00001ul
138 #define STATUS_OVER_TEMPERATURE_SHUTDOWN 0x00002ul
139 #define STATUS_OVER_TEMPERATURE_WARNING 0x00004ul
140 #define STATUS_SHORT_TO_GROUND_A 0x00008ul
141 #define STATUS_SHORT_TO_GROUND_B 0x00010ul
142 #define STATUS_OPEN_LOAD_A 0x00020ul
143 #define STATUS_OPEN_LOAD_B 0x00040ul
144 #define STATUS_STAND_STILL 0x00080ul
145 #define READOUT_VALUE_PATTERN 0xFFC00ul
146
147 //debuging output
148 //#define DEBUG
149
150 /*
151 * Constructor
152 */
153 TMC26X::TMC26X(std::function<int(uint8_t *b, int cnt, uint8_t *r)> spi) : spi(spi)
154 {
155 //we are not started yet
156 started = false;
157 //by default cool step is not enabled
158 cool_step_enabled = false;
159 error_reported.reset();
160 }
161
162 void TMC26X::setResistor(unsigned int resistor)
163 {
164 //store the current sense resistor value for later use
165 this->resistor = resistor;
166 }
167
168 /*
169 * configure the stepper driver
170 * just must be called.
171 */
172 void TMC26X::init()
173 {
174 //setting the default register values
175 driver_control_register_value = DRIVER_CONTROL_REGISTER;
176 chopper_config_register = CHOPPER_CONFIG_REGISTER;
177 cool_step_register_value = COOL_STEP_REGISTER;
178 stall_guard2_current_register_value = STALL_GUARD2_LOAD_MEASURE_REGISTER;
179 driver_configuration_register_value = DRIVER_CONFIG_REGISTER | READ_STALL_GUARD_READING;
180
181 //set the initial values
182 send262(driver_control_register_value);
183 send262(chopper_config_register);
184 send262(cool_step_register_value);
185 send262(stall_guard2_current_register_value);
186 send262(driver_configuration_register_value);
187
188 started = true;
189
190 #if 1
191 //set to a conservative start value
192 setConstantOffTimeChopper(7, 54, 13, 12, 1);
193 #else
194 // for 1.5amp kysan @ 12v
195 setSpreadCycleChopper(5, 54, 5, 0, 0);
196 // for 4amp Nema24 @ 12v
197 //setSpreadCycleChopper(5, 54, 4, 0, 0);
198 #endif
199
200 setEnabled(false);
201
202 //set a nice microstepping value
203 setMicrosteps(DEFAULT_MICROSTEPPING_VALUE);
204
205 // set stallguard to a conservative value so it doesn't trigger immediately
206 setStallGuardThreshold(10, 1);
207 }
208
209 void TMC26X::setCurrent(unsigned int current)
210 {
211 uint8_t current_scaling = 0;
212 //calculate the current scaling from the max current setting (in mA)
213 double mASetting = (double)current;
214 double resistor_value = (double) this->resistor;
215 // remove vesense flag
216 this->driver_configuration_register_value &= ~(VSENSE);
217 //this is derrived from I=(cs+1)/32*(Vsense/Rsense)
218 //leading to cs = CS = 32*R*I/V (with V = 0,31V oder 0,165V and I = 1000*current)
219 //with Rsense=0,15
220 //for vsense = 0,310V (VSENSE not set)
221 //or vsense = 0,165V (VSENSE set)
222 current_scaling = (uint8_t)((resistor_value * mASetting * 32.0F / (0.31F * 1000.0F * 1000.0F)) - 0.5F); //theoretically - 1.0 for better rounding it is 0.5
223
224 //check if the current scaling is too low
225 if (current_scaling < 16) {
226 //set the csense bit to get a use half the sense voltage (to support lower motor currents)
227 this->driver_configuration_register_value |= VSENSE;
228 //and recalculate the current setting
229 current_scaling = (uint8_t)((resistor_value * mASetting * 32.0F / (0.165F * 1000.0F * 1000.0F)) - 0.5F); //theoretically - 1.0 for better rounding it is 0.5
230 }
231
232 //do some sanity checks
233 if (current_scaling > 31) {
234 current_scaling = 31;
235 }
236 //delete the old value
237 stall_guard2_current_register_value &= ~(CURRENT_SCALING_PATTERN);
238 //set the new current scaling
239 stall_guard2_current_register_value |= current_scaling;
240 //if started we directly send it to the motor
241 if (started) {
242 send262(driver_configuration_register_value);
243 send262(stall_guard2_current_register_value);
244 }
245 }
246
247 unsigned int TMC26X::getCurrent(void)
248 {
249 //we calculate the current according to the datasheet to be on the safe side
250 //this is not the fastest but the most accurate and illustrative way
251 double result = (double)(stall_guard2_current_register_value & CURRENT_SCALING_PATTERN);
252 double resistor_value = (double)this->resistor;
253 double voltage = (driver_configuration_register_value & VSENSE) ? 0.165F : 0.31F;
254 result = (result + 1.0F) / 32.0F * voltage / resistor_value * 1000.0F * 1000.0F;
255 return (unsigned int)result;
256 }
257
258 void TMC26X::setStallGuardThreshold(int8_t stall_guard_threshold, int8_t stall_guard_filter_enabled)
259 {
260 if (stall_guard_threshold < -64) {
261 stall_guard_threshold = -64;
262 //We just have 5 bits
263 } else if (stall_guard_threshold > 63) {
264 stall_guard_threshold = 63;
265 }
266 //add trim down to 7 bits
267 stall_guard_threshold &= 0x7f;
268 //delete old stall guard settings
269 stall_guard2_current_register_value &= ~(STALL_GUARD_CONFIG_PATTERN);
270 if (stall_guard_filter_enabled) {
271 stall_guard2_current_register_value |= STALL_GUARD_FILTER_ENABLED;
272 }
273 //Set the new stall guard threshold
274 stall_guard2_current_register_value |= (((unsigned long)stall_guard_threshold << 8) & STALL_GUARD_CONFIG_PATTERN);
275 //if started we directly send it to the motor
276 if (started) {
277 send262(stall_guard2_current_register_value);
278 }
279 }
280
281 int8_t TMC26X::getStallGuardThreshold(void)
282 {
283 unsigned long stall_guard_threshold = stall_guard2_current_register_value & STALL_GUARD_VALUE_PATTERN;
284 //shift it down to bit 0
285 stall_guard_threshold >>= 8;
286 //convert the value to an int to correctly handle the negative numbers
287 int8_t result = stall_guard_threshold;
288 //check if it is negative and fill it up with leading 1 for proper negative number representation
289 if (result & (1 << 6)) {
290 result |= 0xC0;
291 }
292 return result;
293 }
294
295 int8_t TMC26X::getStallGuardFilter(void)
296 {
297 if (stall_guard2_current_register_value & STALL_GUARD_FILTER_ENABLED) {
298 return -1;
299 } else {
300 return 0;
301 }
302 }
303 /*
304 * Set the number of microsteps per step.
305 * 0,2,4,8,16,32,64,128,256 is supported
306 * any value in between will be mapped to the next smaller value
307 * 0 and 1 set the motor in full step mode
308 */
309 void TMC26X::setMicrosteps(int number_of_steps)
310 {
311 long setting_pattern;
312 //poor mans log
313 if (number_of_steps >= 256) {
314 setting_pattern = 0;
315 microsteps = 256;
316 } else if (number_of_steps >= 128) {
317 setting_pattern = 1;
318 microsteps = 128;
319 } else if (number_of_steps >= 64) {
320 setting_pattern = 2;
321 microsteps = 64;
322 } else if (number_of_steps >= 32) {
323 setting_pattern = 3;
324 microsteps = 32;
325 } else if (number_of_steps >= 16) {
326 setting_pattern = 4;
327 microsteps = 16;
328 } else if (number_of_steps >= 8) {
329 setting_pattern = 5;
330 microsteps = 8;
331 } else if (number_of_steps >= 4) {
332 setting_pattern = 6;
333 microsteps = 4;
334 } else if (number_of_steps >= 2) {
335 setting_pattern = 7;
336 microsteps = 2;
337 //1 and 0 lead to full step
338 } else if (number_of_steps <= 1) {
339 setting_pattern = 8;
340 microsteps = 1;
341 }
342
343 //delete the old value
344 this->driver_control_register_value &= 0xFFFF0ul;
345 //set the new value
346 this->driver_control_register_value |= setting_pattern;
347
348 //if started we directly send it to the motor
349 if (started) {
350 send262(driver_control_register_value);
351 }
352 }
353
354 /*
355 * returns the effective number of microsteps at the moment
356 */
357 int TMC26X::getMicrosteps(void)
358 {
359 return microsteps;
360 }
361
362 void TMC26X::setStepInterpolation(int8_t value)
363 {
364 if (value) {
365 driver_control_register_value |= STEP_INTERPOLATION;
366 } else {
367 driver_control_register_value &= ~(STEP_INTERPOLATION);
368 }
369 //if started we directly send it to the motor
370 if (started) {
371 send262(driver_control_register_value);
372 }
373 }
374
375 void TMC26X::setDoubleEdge(int8_t value)
376 {
377 if (value) {
378 driver_control_register_value |= DOUBLE_EDGE_STEP;
379 } else {
380 driver_control_register_value &= ~(DOUBLE_EDGE_STEP);
381 }
382 //if started we directly send it to the motor
383 if (started) {
384 send262(driver_control_register_value);
385 }
386 }
387
388 /*
389 * constant_off_time: The off time setting controls the minimum chopper frequency.
390 * For most applications an off time within the range of 5μs to 20μs will fit.
391 * 2...15: off time setting
392 *
393 * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the
394 * duration of the ringing on the sense resistor. For
395 * 0: min. setting 3: max. setting
396 *
397 * fast_decay_time_setting: Fast decay time setting. With CHM=1, these bits control the portion of fast decay for each chopper cycle.
398 * 0: slow decay only
399 * 1...15: duration of fast decay phase
400 *
401 * sine_wave_offset: Sine wave offset. With CHM=1, these bits control the sine wave offset.
402 * A positive offset corrects for zero crossing error.
403 * -3..-1: negative offset 0: no offset 1...12: positive offset
404 *
405 * use_current_comparator: Selects usage of the current comparator for termination of the fast decay cycle.
406 * If current comparator is enabled, it terminates the fast decay cycle in case the current
407 * reaches a higher negative value than the actual positive value.
408 * 1: enable comparator termination of fast decay cycle
409 * 0: end by time only
410 */
411 void TMC26X::setConstantOffTimeChopper(int8_t constant_off_time, int8_t blank_time, int8_t fast_decay_time_setting, int8_t sine_wave_offset, uint8_t use_current_comparator)
412 {
413 //perform some sanity checks
414 if (constant_off_time < 2) {
415 constant_off_time = 2;
416 } else if (constant_off_time > 15) {
417 constant_off_time = 15;
418 }
419 //save the constant off time
420 this->constant_off_time = constant_off_time;
421 int8_t blank_value;
422 //calculate the value acc to the clock cycles
423 if (blank_time >= 54) {
424 blank_value = 3;
425 } else if (blank_time >= 36) {
426 blank_value = 2;
427 } else if (blank_time >= 24) {
428 blank_value = 1;
429 } else {
430 blank_value = 0;
431 }
432 this->blank_time = blank_time;
433
434 if (fast_decay_time_setting < 0) {
435 fast_decay_time_setting = 0;
436 } else if (fast_decay_time_setting > 15) {
437 fast_decay_time_setting = 15;
438 }
439 if (sine_wave_offset < -3) {
440 sine_wave_offset = -3;
441 } else if (sine_wave_offset > 12) {
442 sine_wave_offset = 12;
443 }
444 //shift the sine_wave_offset
445 sine_wave_offset += 3;
446
447 //calculate the register setting
448 //first of all delete all the values for this
449 chopper_config_register &= ~((1 << 12) | BLANK_TIMING_PATTERN | HYSTERESIS_DECREMENT_PATTERN | HYSTERESIS_LOW_VALUE_PATTERN | HYSTERESIS_START_VALUE_PATTERN | T_OFF_TIMING_PATERN);
450 //set the constant off pattern
451 chopper_config_register |= CHOPPER_MODE_T_OFF_FAST_DECAY;
452 //set the blank timing value
453 chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
454 //setting the constant off time
455 chopper_config_register |= constant_off_time;
456 //set the fast decay time
457 //set msb
458 chopper_config_register |= (((unsigned long)(fast_decay_time_setting & 0x8)) << HYSTERESIS_DECREMENT_SHIFT);
459 //other bits
460 chopper_config_register |= (((unsigned long)(fast_decay_time_setting & 0x7)) << HYSTERESIS_START_VALUE_SHIFT);
461 //set the sine wave offset
462 chopper_config_register |= (unsigned long)sine_wave_offset << HYSTERESIS_LOW_SHIFT;
463 //using the current comparator?
464 if (!use_current_comparator) {
465 chopper_config_register |= (1 << 12);
466 }
467 //if started we directly send it to the motor
468 if (started) {
469 send262(chopper_config_register);
470 }
471 }
472
473 /*
474 * constant_off_time: The off time setting controls the minimum chopper frequency.
475 * For most applications an off time within the range of 5μs to 20μs will fit.
476 * 2...15: off time setting
477 *
478 * blank_time: Selects the comparator blank time. This time needs to safely cover the switching event and the
479 * duration of the ringing on the sense resistor. For
480 * 0: min. setting 3: max. setting
481 *
482 * hysteresis_start: Hysteresis start setting. Please remark, that this value is an offset to the hysteresis end value HEND.
483 * 1...8
484 *
485 * hysteresis_end: Hysteresis end setting. Sets the hysteresis end value after a number of decrements. Decrement interval time is controlled by HDEC.
486 * The sum HSTRT+HEND must be <16. At a current setting CS of max. 30 (amplitude reduced to 240), the sum is not limited.
487 * -3..-1: negative HEND 0: zero HEND 1...12: positive HEND
488 *
489 * hysteresis_decrement: Hysteresis decrement setting. This setting determines the slope of the hysteresis during on time and during fast decay time.
490 * 0: fast decrement 3: very slow decrement
491 */
492
493 void TMC26X::setSpreadCycleChopper(int8_t constant_off_time, int8_t blank_time, int8_t hysteresis_start, int8_t hysteresis_end, int8_t hysteresis_decrement)
494 {
495 h_start = hysteresis_start;
496 h_end = hysteresis_end;
497 h_decrement = hysteresis_decrement;
498 this->blank_time = blank_time;
499
500 //perform some sanity checks
501 if (constant_off_time < 2) {
502 constant_off_time = 2;
503 } else if (constant_off_time > 15) {
504 constant_off_time = 15;
505 }
506 //save the constant off time
507 this->constant_off_time = constant_off_time;
508 int8_t blank_value;
509 //calculate the value acc to the clock cycles
510 if (blank_time >= 54) {
511 blank_value = 3;
512 } else if (blank_time >= 36) {
513 blank_value = 2;
514 } else if (blank_time >= 24) {
515 blank_value = 1;
516 } else {
517 blank_value = 0;
518 }
519
520 if (hysteresis_start < 1) {
521 hysteresis_start = 1;
522 } else if (hysteresis_start > 8) {
523 hysteresis_start = 8;
524 }
525 hysteresis_start--;
526
527 if (hysteresis_end < -3) {
528 hysteresis_end = -3;
529 } else if (hysteresis_end > 12) {
530 hysteresis_end = 12;
531 }
532 //shift the hysteresis_end
533 hysteresis_end += 3;
534
535 if (hysteresis_decrement < 0) {
536 hysteresis_decrement = 0;
537 } else if (hysteresis_decrement > 3) {
538 hysteresis_decrement = 3;
539 }
540
541 //first of all delete all the values for this
542 chopper_config_register &= ~(CHOPPER_MODE_T_OFF_FAST_DECAY | BLANK_TIMING_PATTERN | HYSTERESIS_DECREMENT_PATTERN | HYSTERESIS_LOW_VALUE_PATTERN | HYSTERESIS_START_VALUE_PATTERN | T_OFF_TIMING_PATERN);
543
544 //set the blank timing value
545 chopper_config_register |= ((unsigned long)blank_value) << BLANK_TIMING_SHIFT;
546 //setting the constant off time
547 chopper_config_register |= constant_off_time;
548 //set the hysteresis_start
549 chopper_config_register |= ((unsigned long)hysteresis_start) << HYSTERESIS_START_VALUE_SHIFT;
550 //set the hysteresis end
551 chopper_config_register |= ((unsigned long)hysteresis_end) << HYSTERESIS_LOW_SHIFT;
552 //set the hystereis decrement
553 chopper_config_register |= ((unsigned long)hysteresis_decrement) << HYSTERESIS_DECREMENT_SHIFT;
554
555 //if started we directly send it to the motor
556 if (started) {
557 send262(chopper_config_register);
558 }
559 }
560
561 /*
562 * In a constant off time chopper scheme both coil choppers run freely, i.e. are not synchronized.
563 * The frequency of each chopper mainly depends on the coil current and the position dependant motor coil inductivity, thus it depends on the microstep position.
564 * With some motors a slightly audible beat can occur between the chopper frequencies, especially when they are near to each other. This typically occurs at a
565 * few microstep positions within each quarter wave. This effect normally is not audible when compared to mechanical noise generated by ball bearings, etc.
566 * Further factors which can cause a similar effect are a poor layout of sense resistor GND connection.
567 * Hint: A common factor, which can cause motor noise, is a bad PCB layout causing coupling of both sense resistor voltages
568 * (please refer to sense resistor layout hint in chapter 8.1).
569 * In order to minimize the effect of a beat between both chopper frequencies, an internal random generator is provided.
570 * It modulates the slow decay time setting when switched on by the RNDTF bit. The RNDTF feature further spreads the chopper spectrum,
571 * reducing electromagnetic emission on single frequencies.
572 */
573 void TMC26X::setRandomOffTime(int8_t value)
574 {
575 if (value) {
576 chopper_config_register |= RANDOM_TOFF_TIME;
577 } else {
578 chopper_config_register &= ~(RANDOM_TOFF_TIME);
579 }
580 //if started we directly send it to the motor
581 if (started) {
582 send262(chopper_config_register);
583 }
584 }
585
586 void TMC26X::setCoolStepConfiguration(unsigned int lower_SG_threshold, unsigned int SG_hysteresis, uint8_t current_decrement_step_size,
587 uint8_t current_increment_step_size, uint8_t lower_current_limit)
588 {
589 //sanitize the input values
590 if (lower_SG_threshold > 480) {
591 lower_SG_threshold = 480;
592 }
593 //divide by 32
594 lower_SG_threshold >>= 5;
595 if (SG_hysteresis > 480) {
596 SG_hysteresis = 480;
597 }
598 //divide by 32
599 SG_hysteresis >>= 5;
600 if (current_decrement_step_size > 3) {
601 current_decrement_step_size = 3;
602 }
603 if (current_increment_step_size > 3) {
604 current_increment_step_size = 3;
605 }
606 if (lower_current_limit > 1) {
607 lower_current_limit = 1;
608 }
609 //store the lower level in order to enable/disable the cool step
610 this->cool_step_lower_threshold = lower_SG_threshold;
611 //if cool step is not enabled we delete the lower value to keep it disabled
612 if (!this->cool_step_enabled) {
613 lower_SG_threshold = 0;
614 }
615 //the good news is that we can start with a complete new cool step register value
616 //and simply set the values in the register
617 cool_step_register_value = ((unsigned long)lower_SG_threshold) | (((unsigned long)SG_hysteresis) << 8) | (((unsigned long)current_decrement_step_size) << 5)
618 | (((unsigned long)current_increment_step_size) << 13) | (((unsigned long)lower_current_limit) << 15)
619 //and of course we have to include the signature of the register
620 | COOL_STEP_REGISTER;
621
622 if (started) {
623 send262(cool_step_register_value);
624 }
625 }
626
627 void TMC26X::setCoolStepEnabled(bool enabled)
628 {
629 //simply delete the lower limit to disable the cool step
630 cool_step_register_value &= ~SE_MIN_PATTERN;
631 //and set it to the proper value if cool step is to be enabled
632 if (enabled) {
633 cool_step_register_value |= this->cool_step_lower_threshold;
634 }
635 //and save the enabled status
636 this->cool_step_enabled = enabled;
637 //save the register value
638 if (started) {
639 send262(cool_step_register_value);
640 }
641 }
642
643 bool TMC26X::isCoolStepEnabled(void)
644 {
645 return this->cool_step_enabled;
646 }
647
648 unsigned int TMC26X::getCoolStepLowerSgThreshold()
649 {
650 //we return our internally stored value - in order to provide the correct setting even if cool step is not enabled
651 return this->cool_step_lower_threshold << 5;
652 }
653
654 unsigned int TMC26X::getCoolStepUpperSgThreshold()
655 {
656 return (uint8_t)((cool_step_register_value & SE_MAX_PATTERN) >> 8) << 5;
657 }
658
659 uint8_t TMC26X::getCoolStepCurrentIncrementSize()
660 {
661 return (uint8_t)((cool_step_register_value & CURRENT_DOWN_STEP_SPEED_PATTERN) >> 13);
662 }
663
664 uint8_t TMC26X::getCoolStepNumberOfSGReadings()
665 {
666 return (uint8_t)((cool_step_register_value & SE_CURRENT_STEP_WIDTH_PATTERN) >> 5);
667 }
668
669 uint8_t TMC26X::getCoolStepLowerCurrentLimit()
670 {
671 return (uint8_t)((cool_step_register_value & MINIMUM_CURRENT_FOURTH) >> 15);
672 }
673
674 void TMC26X::setEnabled(bool enabled)
675 {
676 //delete the t_off in the chopper config to get sure
677 chopper_config_register &= ~(T_OFF_PATTERN);
678 if (enabled) {
679 //and set the t_off time
680 chopper_config_register |= this->constant_off_time;
681 }
682 //if not enabled we don't have to do anything since we already delete t_off from the register
683 if (started) {
684 send262(chopper_config_register);
685 }
686 }
687
688 bool TMC26X::isEnabled()
689 {
690 if (chopper_config_register & T_OFF_PATTERN) {
691 return true;
692 } else {
693 return false;
694 }
695 }
696
697 /*
698 * reads a value from the TMC26X status register. The value is not obtained directly but can then
699 * be read by the various status routines.
700 *
701 */
702 void TMC26X::readStatus(int8_t read_value)
703 {
704 unsigned long old_driver_configuration_register_value = driver_configuration_register_value;
705 //reset the readout configuration
706 driver_configuration_register_value &= ~(READ_SELECTION_PATTERN);
707 //this now equals TMC26X_READOUT_POSITION - so we just have to check the other two options
708 if (read_value == TMC26X_READOUT_STALLGUARD) {
709 driver_configuration_register_value |= READ_STALL_GUARD_READING;
710 } else if (read_value == TMC26X_READOUT_CURRENT) {
711 driver_configuration_register_value |= READ_STALL_GUARD_AND_COOL_STEP;
712 }
713 //all other cases are ignored to prevent funny values
714 //check if the readout is configured for the value we are interested in
715 if (driver_configuration_register_value != old_driver_configuration_register_value) {
716 //because then we need to write the value twice - one time for configuring, second time to get the value, see below
717 send262(driver_configuration_register_value);
718 }
719 //write the configuration to get the last status
720 send262(driver_configuration_register_value);
721 }
722
723 //reads the stall guard setting from last status
724 //returns -1 if stallguard information is not present
725 int TMC26X::getCurrentStallGuardReading(void)
726 {
727 //if we don't yet started there cannot be a stall guard value
728 if (!started) {
729 return -1;
730 }
731 //not time optimal, but solution optiomal:
732 //first read out the stall guard value
733 readStatus(TMC26X_READOUT_STALLGUARD);
734 return getReadoutValue();
735 }
736
737 uint8_t TMC26X::getCurrentCSReading(void)
738 {
739 //if we don't yet started there cannot be a stall guard value
740 if (!started) {
741 return 0;
742 }
743
744 //first read out the stall guard value
745 readStatus(TMC26X_READOUT_CURRENT);
746 return (getReadoutValue() & 0x1f);
747 }
748
749 unsigned int TMC26X::getCoolstepCurrent(void)
750 {
751 float result = (float)getCurrentCSReading();
752 float resistor_value = (float)this->resistor;
753 float voltage = (driver_configuration_register_value & VSENSE) ? 0.165F : 0.31F;
754 result = (result + 1.0F) / 32.0F * voltage / resistor_value * 1000.0F * 1000.0F;
755 return (unsigned int)roundf(result);
756 }
757
758 /*
759 return true if the stallguard threshold has been reached
760 */
761 bool TMC26X::isStallGuardOverThreshold(void)
762 {
763 if (!this->started) {
764 return false;
765 }
766 return (driver_status_result & STATUS_STALL_GUARD_STATUS);
767 }
768
769 /*
770 returns if there is any over temperature condition:
771 OVER_TEMPERATURE_PREWARING if pre warning level has been reached
772 OVER_TEMPERATURE_SHUTDOWN if the temperature is so hot that the driver is shut down
773 Any of those levels are not too good.
774 */
775 int8_t TMC26X::getOverTemperature(void)
776 {
777 if (!this->started) {
778 return 0;
779 }
780 if (driver_status_result & STATUS_OVER_TEMPERATURE_SHUTDOWN) {
781 return TMC26X_OVERTEMPERATURE_SHUTDOWN;
782 }
783 if (driver_status_result & STATUS_OVER_TEMPERATURE_WARNING) {
784 return TMC26X_OVERTEMPERATURE_PREWARING;
785 }
786 return 0;
787 }
788
789 //is motor channel A shorted to ground
790 bool TMC26X::isShortToGroundA(void)
791 {
792 if (!this->started) {
793 return false;
794 }
795 return (driver_status_result & STATUS_SHORT_TO_GROUND_A);
796 }
797
798 //is motor channel B shorted to ground
799 bool TMC26X::isShortToGroundB(void)
800 {
801 if (!this->started) {
802 return false;
803 }
804 return (driver_status_result & STATUS_SHORT_TO_GROUND_B);
805 }
806
807 //is motor channel A connected
808 bool TMC26X::isOpenLoadA(void)
809 {
810 if (!this->started) {
811 return false;
812 }
813 return (driver_status_result & STATUS_OPEN_LOAD_A);
814 }
815
816 //is motor channel B connected
817 bool TMC26X::isOpenLoadB(void)
818 {
819 if (!this->started) {
820 return false;
821 }
822 return (driver_status_result & STATUS_OPEN_LOAD_B);
823 }
824
825 //is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
826 bool TMC26X::isStandStill(void)
827 {
828 if (!this->started) {
829 return false;
830 }
831 return (driver_status_result & STATUS_STAND_STILL);
832 }
833
834 //is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
835 bool TMC26X::isStallGuardReached(void)
836 {
837 if (!this->started) {
838 return false;
839 }
840 return (driver_status_result & STATUS_STALL_GUARD_STATUS);
841 }
842
843 //reads the stall guard setting from last status
844 //returns -1 if stallguard inforamtion is not present
845 int TMC26X::getReadoutValue(void)
846 {
847 return (int)(driver_status_result >> 10);
848 }
849
850 int TMC26X::getResistor()
851 {
852 return this->resistor;
853 }
854
855 bool TMC26X::isCurrentScalingHalfed()
856 {
857 if (this->driver_configuration_register_value & VSENSE) {
858 return true;
859 } else {
860 return false;
861 }
862 }
863
864 void TMC26X::dumpStatus(StreamOutput *stream, bool readable)
865 {
866 if (readable) {
867 stream->printf("Chip type TMC26X\n");
868
869 check_error_status_bits(stream);
870
871 if (this->isStallGuardReached()) {
872 stream->printf("INFO: Stall Guard level reached!\n");
873 }
874
875 if (this->isStandStill()) {
876 stream->printf("INFO: Motor is standing still.\n");
877 }
878
879 int value = getReadoutValue();
880 stream->printf("Microstep postion phase A: %d\n", value);
881
882 value = getCurrentStallGuardReading();
883 stream->printf("Stall Guard value: %d\n", value);
884
885 stream->printf("Current setting: %dmA\n", getCurrent());
886 stream->printf("Coolstep current: %dmA\n", getCoolstepCurrent());
887
888 stream->printf("Microsteps: 1/%d\n", microsteps);
889
890 stream->printf("Register dump:\n");
891 stream->printf(" driver control register: %08lX(%ld)\n", driver_control_register_value, driver_control_register_value);
892 stream->printf(" chopper config register: %08lX(%ld)\n", chopper_config_register, chopper_config_register);
893 stream->printf(" cool step register: %08lX(%ld)\n", cool_step_register_value, cool_step_register_value);
894 stream->printf(" stall guard2 current register: %08lX(%ld)\n", stall_guard2_current_register_value, stall_guard2_current_register_value);
895 stream->printf(" driver configuration register: %08lX(%ld)\n", driver_configuration_register_value, driver_configuration_register_value);
896 stream->printf(" motor_driver_control.xxx.reg %05lX,%05lX,%05lX,%05lX,%05lX\n", driver_control_register_value, chopper_config_register, cool_step_register_value, stall_guard2_current_register_value, driver_configuration_register_value);
897
898 } else {
899 // TODO hardcoded for X need to select ABC as needed
900 bool moving = THEKERNEL->robot->actuators[0]->is_moving();
901 // dump out in the format that the processing script needs
902 if (moving) {
903 stream->printf("#sg%d,p%lu,k%u,r,", getCurrentStallGuardReading(), THEKERNEL->robot->actuators[0]->get_stepped(), getCoolstepCurrent());
904 } else {
905 readStatus(TMC26X_READOUT_POSITION); // get the status bits
906 stream->printf("#s,");
907 }
908 stream->printf("d%d,", THEKERNEL->robot->actuators[0]->which_direction() ? 1 : -1);
909 stream->printf("c%u,m%d,", getCurrent(), getMicrosteps());
910 // stream->printf('S');
911 // stream->printf(tmc26XStepper.getSpeed(), DEC);
912 stream->printf("t%d,f%d,", getStallGuardThreshold(), getStallGuardFilter());
913
914 //print out the general cool step config
915 if (isCoolStepEnabled()) stream->printf("Ke+,");
916 else stream->printf("Ke-,");
917
918 stream->printf("Kl%u,Ku%u,Kn%u,Ki%u,Km%u,",
919 getCoolStepLowerSgThreshold(), getCoolStepUpperSgThreshold(), getCoolStepNumberOfSGReadings(), getCoolStepCurrentIncrementSize(), getCoolStepLowerCurrentLimit());
920
921 //detect the winding status
922 if (isOpenLoadA()) {
923 stream->printf("ao,");
924 } else if(isShortToGroundA()) {
925 stream->printf("ag,");
926 } else {
927 stream->printf("a-,");
928 }
929 //detect the winding status
930 if (isOpenLoadB()) {
931 stream->printf("bo,");
932 } else if(isShortToGroundB()) {
933 stream->printf("bg,");
934 } else {
935 stream->printf("b-,");
936 }
937
938 char temperature = getOverTemperature();
939 if (temperature == 0) {
940 stream->printf("x-,");
941 } else if (temperature == TMC26X_OVERTEMPERATURE_PREWARING) {
942 stream->printf("xw,");
943 } else {
944 stream->printf("xe,");
945 }
946
947 if (isEnabled()) {
948 stream->printf("e1,");
949 } else {
950 stream->printf("e0,");
951 }
952
953 //write out the current chopper config
954 stream->printf("Cm%d,", (chopper_config_register & CHOPPER_MODE_T_OFF_FAST_DECAY) != 0);
955 stream->printf("Co%d,Cb%d,", constant_off_time, blank_time);
956 if ((chopper_config_register & CHOPPER_MODE_T_OFF_FAST_DECAY) == 0) {
957 stream->printf("Cs%d,Ce%d,Cd%d,", h_start, h_end, h_decrement);
958 }
959 stream->printf("\n");
960 }
961 }
962
963 // check error bits and report, only report once
964 bool TMC26X::check_error_status_bits(StreamOutput *stream)
965 {
966 bool error= false;
967 readStatus(TMC26X_READOUT_POSITION); // get the status bits
968
969 if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_PREWARING) {
970 if(!error_reported.test(0)) stream->printf("WARNING: Overtemperature Prewarning!\n");
971 error_reported.set(0);
972 }else{
973 error_reported.reset(0);
974 }
975
976 if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_SHUTDOWN) {
977 if(!error_reported.test(1)) stream->printf("ERROR: Overtemperature Shutdown!\n");
978 error=true;
979 error_reported.set(1);
980 }else{
981 error_reported.reset(1);
982 }
983
984 if (this->isShortToGroundA()) {
985 if(!error_reported.test(2)) stream->printf("ERROR: SHORT to ground on channel A!\n");
986 error=true;
987 error_reported.set(2);
988 }else{
989 error_reported.reset(2);
990 }
991
992 if (this->isShortToGroundB()) {
993 if(!error_reported.test(3)) stream->printf("ERROR: SHORT to ground on channel B!\n");
994 error=true;
995 error_reported.set(3);
996 }else{
997 error_reported.reset(3);
998 }
999
1000 // these seem to be triggered when moving so ignore them for now
1001 if (this->isOpenLoadA()) {
1002 if(!error_reported.test(4)) stream->printf("ERROR: Channel A seems to be unconnected!\n");
1003 error=true;
1004 error_reported.set(4);
1005 }else{
1006 error_reported.reset(4);
1007 }
1008
1009 if (this->isOpenLoadB()) {
1010 if(!error_reported.test(5)) stream->printf("ERROR: Channel B seems to be unconnected!\n");
1011 error=true;
1012 error_reported.set(5);
1013 }else{
1014 error_reported.reset(5);
1015 }
1016
1017 // if(error) {
1018 // stream->printf("%08X\n", driver_status_result);
1019 // }
1020 return error;
1021 }
1022
1023 bool TMC26X::checkAlarm()
1024 {
1025 return check_error_status_bits(THEKERNEL->streams);
1026 }
1027
1028 // sets a raw register to the value specified, for advanced settings
1029 // register 0 writes them, 255 displays what registers are mapped to what
1030 // FIXME status registers not reading back correctly, check docs
1031 bool TMC26X::setRawRegister(StreamOutput *stream, uint32_t reg, uint32_t val)
1032 {
1033 switch(reg) {
1034 case 255:
1035 send262(driver_control_register_value);
1036 send262(chopper_config_register);
1037 send262(cool_step_register_value);
1038 send262(stall_guard2_current_register_value);
1039 send262(driver_configuration_register_value);
1040 stream->printf("Registers written\n");
1041 break;
1042
1043
1044 case 1: driver_control_register_value = val; stream->printf("driver control register set to %08lX\n", val); break;
1045 case 2: chopper_config_register = val; stream->printf("chopper config register set to %08lX\n", val); break;
1046 case 3: cool_step_register_value = val; stream->printf("cool step register set to %08lX\n", val); break;
1047 case 4: stall_guard2_current_register_value = val; stream->printf("stall guard2 current register set to %08lX\n", val); break;
1048 case 5: driver_configuration_register_value = val; stream->printf("driver configuration register set to %08lX\n", val); break;
1049
1050 default:
1051 stream->printf("1: driver control register\n");
1052 stream->printf("2: chopper config register\n");
1053 stream->printf("3: cool step register\n");
1054 stream->printf("4: stall guard2 current register\n");
1055 stream->printf("5: driver configuration register\n");
1056 stream->printf("255: update all registers\n");
1057 return false;
1058 }
1059 return true;
1060 }
1061
1062 /*
1063 * send register settings to the stepper driver via SPI
1064 * returns the current status
1065 * sends 20bits, the last 20 bits of the 24bits is taken as the command
1066 */
1067 void TMC26X::send262(unsigned long datagram)
1068 {
1069 uint8_t buf[] {(uint8_t)(datagram >> 16), (uint8_t)(datagram >> 8), (uint8_t)(datagram & 0xff)};
1070 uint8_t rbuf[3];
1071
1072 //write/read the values
1073 spi(buf, 3, rbuf);
1074
1075 // construct reply
1076 unsigned long i_datagram = ((rbuf[0] << 16) | (rbuf[1] << 8) | (rbuf[2])) >> 4;
1077
1078 //store the datagram as status result
1079 driver_status_result = i_datagram;
1080
1081 //THEKERNEL->streams->printf("sent: %02X, %02X, %02X received: %02X, %02X, %02X \n", buf[0], buf[1], buf[2], rbuf[0], rbuf[1], rbuf[2]);
1082 }
1083
1084 #define HAS(X) (options.find(X) != options.end())
1085 #define GET(X) (options.at(X))
1086 bool TMC26X::set_options(const options_t& options)
1087 {
1088 bool set = false;
1089 if(HAS('O') || HAS('Q')) {
1090 // void TMC26X::setStallGuardThreshold(int8_t stall_guard_threshold, int8_t stall_guard_filter_enabled)
1091 int8_t o = HAS('O') ? GET('O') : getStallGuardThreshold();
1092 int8_t q = HAS('Q') ? GET('Q') : getStallGuardFilter();
1093 setStallGuardThreshold(o, q);
1094 set = true;
1095 }
1096
1097 if(HAS('H') && HAS('I') && HAS('J') && HAS('K') && HAS('L')) {
1098 //void TMC26X::setCoolStepConfiguration(unsigned int lower_SG_threshold, unsigned int SG_hysteresis, uint8_t current_decrement_step_size, uint8_t current_increment_step_size, uint8_t lower_current_limit)
1099 setCoolStepConfiguration(GET('H'), GET('I'), GET('J'), GET('K'), GET('L'));
1100 set = true;
1101 }
1102
1103 if(HAS('S')) {
1104 uint32_t s = GET('S');
1105 if(s == 0 && HAS('U') && HAS('V') && HAS('W') && HAS('X') && HAS('Y')) {
1106 //void TMC26X::setConstantOffTimeChopper(int8_t constant_off_time, int8_t blank_time, int8_t fast_decay_time_setting, int8_t sine_wave_offset, uint8_t use_current_comparator)
1107 setConstantOffTimeChopper(GET('U'), GET('V'), GET('W'), GET('X'), GET('Y'));
1108 set = true;
1109
1110 } else if(s == 1 && HAS('U') && HAS('V') && HAS('W') && HAS('X') && HAS('Y')) {
1111 //void TMC26X::setSpreadCycleChopper(int8_t constant_off_time, int8_t blank_time, int8_t hysteresis_start, int8_t hysteresis_end, int8_t hysteresis_decrement);
1112 setSpreadCycleChopper(GET('U'), GET('V'), GET('W'), GET('X'), GET('Y'));
1113 set = true;
1114
1115 } else if(s == 2 && HAS('Z')) {
1116 setRandomOffTime(GET('Z'));
1117 set = true;
1118
1119 } else if(s == 3 && HAS('Z')) {
1120 setDoubleEdge(GET('Z'));
1121 set = true;
1122
1123 } else if(s == 4 && HAS('Z')) {
1124 setStepInterpolation(GET('Z'));
1125 set = true;
1126
1127 } else if(s == 5 && HAS('Z')) {
1128 setCoolStepEnabled(GET('Z') == 1);
1129 set = true;
1130 }
1131 }
1132
1133 return set;
1134 }