2 Highly modifed from....
4 TMC26X.cpp - - TMC26X Stepper library for Wiring/Arduino
6 based on the stepper library by Tom Igoe, et. al.
8 Copyright (c) 2011, Interactive Matter, Marcus Nowotny
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:
17 The above copyright notice and this permission notice shall be included in
18 all copies or substantial portions of the Software.
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
32 #include "StreamOutput.h"
34 #include "libs/StreamOutputPool.h"
36 #include "StepperMotor.h"
38 //! return value for TMC26X.getOverTemperature() if there is a overtemperature situation in the TMC chip
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.
44 #define TMC26X_OVERTEMPERATURE_PREWARING 1
45 //! return value for TMC26X.getOverTemperature() if there is a overtemperature shutdown in the TMC chip
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.
52 #define TMC26X_OVERTEMPERATURE_SHUTDOWN 2
54 //which values can be read out
56 * Selects to readout the microstep position from the motor.
59 #define TMC26X_READOUT_POSITION 0
61 * Selects to read out the StallGuard value of the motor.
64 #define TMC26X_READOUT_STALLGUARD 1
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()
69 #define TMC26X_READOUT_CURRENT 3
72 * Define to set the minimum current for CoolStep operation to 1/2 of the selected CS minium.
73 *\sa setCoolStepConfiguration()
75 #define COOL_STEP_HALF_CS_LIMIT 0
77 * Define to set the minimum current for CoolStep operation to 1/4 of the selected CS minium.
78 *\sa setCoolStepConfiguration()
80 #define COOL_STEP_QUARTDER_CS_LIMIT 1
83 //some default values used in initialization
84 #define DEFAULT_MICROSTEPPING_VALUE 32
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
93 #define REGISTER_BIT_PATTERN 0xFFFFFul
95 //definitions for the driver control register DRVCTL
96 #define MICROSTEPPING_PATTERN 0x000Ful
97 #define STEP_INTERPOLATION 0x0200ul
98 #define DOUBLE_EDGE_STEP 0x0100ul
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
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
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
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
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
153 TMC26X::TMC26X(std::function
<int(uint8_t *b
, int cnt
, uint8_t *r
)> spi
) : spi(spi
)
155 //we are not started yet
157 //by default cool step is not enabled
158 cool_step_enabled
= false;
159 error_reported
.reset();
162 void TMC26X::setResistor(unsigned int resistor
)
164 //store the current sense resistor value for later use
165 this->resistor
= resistor
;
169 * configure the stepper driver
170 * just must be called.
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
;
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
);
191 //set to a conservative start value
192 setConstantOffTimeChopper(7, 54, 13, 12, 1);
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);
202 //set a nice microstepping value
203 setMicrosteps(DEFAULT_MICROSTEPPING_VALUE
);
205 // set stallguard to a conservative value so it doesn't trigger immediately
206 setStallGuardThreshold(10, 1);
209 void TMC26X::setCurrent(unsigned int current
)
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)
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
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
232 //do some sanity checks
233 if (current_scaling
> 31) {
234 current_scaling
= 31;
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
242 send262(driver_configuration_register_value
);
243 send262(stall_guard2_current_register_value
);
247 unsigned int TMC26X::getCurrent(void)
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
;
258 void TMC26X::setStallGuardThreshold(int8_t stall_guard_threshold
, int8_t stall_guard_filter_enabled
)
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;
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
;
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
277 send262(stall_guard2_current_register_value
);
281 int8_t TMC26X::getStallGuardThreshold(void)
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)) {
295 int8_t TMC26X::getStallGuardFilter(void)
297 if (stall_guard2_current_register_value
& STALL_GUARD_FILTER_ENABLED
) {
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
309 void TMC26X::setMicrosteps(int number_of_steps
)
311 long setting_pattern
;
313 if (number_of_steps
>= 256) {
316 } else if (number_of_steps
>= 128) {
319 } else if (number_of_steps
>= 64) {
322 } else if (number_of_steps
>= 32) {
325 } else if (number_of_steps
>= 16) {
328 } else if (number_of_steps
>= 8) {
331 } else if (number_of_steps
>= 4) {
334 } else if (number_of_steps
>= 2) {
337 //1 and 0 lead to full step
338 } else if (number_of_steps
<= 1) {
343 //delete the old value
344 this->driver_control_register_value
&= 0xFFFF0ul
;
346 this->driver_control_register_value
|= setting_pattern
;
348 //if started we directly send it to the motor
350 send262(driver_control_register_value
);
355 * returns the effective number of microsteps at the moment
357 int TMC26X::getMicrosteps(void)
362 void TMC26X::setStepInterpolation(int8_t value
)
365 driver_control_register_value
|= STEP_INTERPOLATION
;
367 driver_control_register_value
&= ~(STEP_INTERPOLATION
);
369 //if started we directly send it to the motor
371 send262(driver_control_register_value
);
375 void TMC26X::setDoubleEdge(int8_t value
)
378 driver_control_register_value
|= DOUBLE_EDGE_STEP
;
380 driver_control_register_value
&= ~(DOUBLE_EDGE_STEP
);
382 //if started we directly send it to the motor
384 send262(driver_control_register_value
);
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
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
397 * fast_decay_time_setting: Fast decay time setting. With CHM=1, these bits control the portion of fast decay for each chopper cycle.
399 * 1...15: duration of fast decay phase
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
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
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
)
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;
419 //save the constant off time
420 this->constant_off_time
= constant_off_time
;
422 //calculate the value acc to the clock cycles
423 if (blank_time
>= 54) {
425 } else if (blank_time
>= 36) {
427 } else if (blank_time
>= 24) {
432 this->blank_time
= blank_time
;
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;
439 if (sine_wave_offset
< -3) {
440 sine_wave_offset
= -3;
441 } else if (sine_wave_offset
> 12) {
442 sine_wave_offset
= 12;
444 //shift the sine_wave_offset
445 sine_wave_offset
+= 3;
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
458 chopper_config_register
|= (((unsigned long)(fast_decay_time_setting
& 0x8)) << HYSTERESIS_DECREMENT_SHIFT
);
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);
467 //if started we directly send it to the motor
469 send262(chopper_config_register
);
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
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
482 * hysteresis_start: Hysteresis start setting. Please remark, that this value is an offset to the hysteresis end value HEND.
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
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
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
)
495 h_start
= hysteresis_start
;
496 h_end
= hysteresis_end
;
497 h_decrement
= hysteresis_decrement
;
498 this->blank_time
= blank_time
;
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;
506 //save the constant off time
507 this->constant_off_time
= constant_off_time
;
509 //calculate the value acc to the clock cycles
510 if (blank_time
>= 54) {
512 } else if (blank_time
>= 36) {
514 } else if (blank_time
>= 24) {
520 if (hysteresis_start
< 1) {
521 hysteresis_start
= 1;
522 } else if (hysteresis_start
> 8) {
523 hysteresis_start
= 8;
527 if (hysteresis_end
< -3) {
529 } else if (hysteresis_end
> 12) {
532 //shift the hysteresis_end
535 if (hysteresis_decrement
< 0) {
536 hysteresis_decrement
= 0;
537 } else if (hysteresis_decrement
> 3) {
538 hysteresis_decrement
= 3;
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
);
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
;
555 //if started we directly send it to the motor
557 send262(chopper_config_register
);
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.
573 void TMC26X::setRandomOffTime(int8_t value
)
576 chopper_config_register
|= RANDOM_TOFF_TIME
;
578 chopper_config_register
&= ~(RANDOM_TOFF_TIME
);
580 //if started we directly send it to the motor
582 send262(chopper_config_register
);
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
)
589 //sanitize the input values
590 if (lower_SG_threshold
> 480) {
591 lower_SG_threshold
= 480;
594 lower_SG_threshold
>>= 5;
595 if (SG_hysteresis
> 480) {
600 if (current_decrement_step_size
> 3) {
601 current_decrement_step_size
= 3;
603 if (current_increment_step_size
> 3) {
604 current_increment_step_size
= 3;
606 if (lower_current_limit
> 1) {
607 lower_current_limit
= 1;
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;
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
;
623 send262(cool_step_register_value
);
627 void TMC26X::setCoolStepEnabled(bool enabled
)
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
633 cool_step_register_value
|= this->cool_step_lower_threshold
;
635 //and save the enabled status
636 this->cool_step_enabled
= enabled
;
637 //save the register value
639 send262(cool_step_register_value
);
643 bool TMC26X::isCoolStepEnabled(void)
645 return this->cool_step_enabled
;
648 unsigned int TMC26X::getCoolStepLowerSgThreshold()
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;
654 unsigned int TMC26X::getCoolStepUpperSgThreshold()
656 return (uint8_t)((cool_step_register_value
& SE_MAX_PATTERN
) >> 8) << 5;
659 uint8_t TMC26X::getCoolStepCurrentIncrementSize()
661 return (uint8_t)((cool_step_register_value
& CURRENT_DOWN_STEP_SPEED_PATTERN
) >> 13);
664 uint8_t TMC26X::getCoolStepNumberOfSGReadings()
666 return (uint8_t)((cool_step_register_value
& SE_CURRENT_STEP_WIDTH_PATTERN
) >> 5);
669 uint8_t TMC26X::getCoolStepLowerCurrentLimit()
671 return (uint8_t)((cool_step_register_value
& MINIMUM_CURRENT_FOURTH
) >> 15);
674 void TMC26X::setEnabled(bool enabled
)
676 //delete the t_off in the chopper config to get sure
677 chopper_config_register
&= ~(T_OFF_PATTERN
);
679 //and set the t_off time
680 chopper_config_register
|= this->constant_off_time
;
682 //if not enabled we don't have to do anything since we already delete t_off from the register
684 send262(chopper_config_register
);
688 bool TMC26X::isEnabled()
690 if (chopper_config_register
& T_OFF_PATTERN
) {
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.
702 void TMC26X::readStatus(int8_t read_value
)
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
;
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
);
719 //write the configuration to get the last status
720 send262(driver_configuration_register_value
);
723 //reads the stall guard setting from last status
724 //returns -1 if stallguard information is not present
725 int TMC26X::getCurrentStallGuardReading(void)
727 //if we don't yet started there cannot be a stall guard value
731 //not time optimal, but solution optiomal:
732 //first read out the stall guard value
733 readStatus(TMC26X_READOUT_STALLGUARD
);
734 return getReadoutValue();
737 uint8_t TMC26X::getCurrentCSReading(void)
739 //if we don't yet started there cannot be a stall guard value
744 //first read out the stall guard value
745 readStatus(TMC26X_READOUT_CURRENT
);
746 return (getReadoutValue() & 0x1f);
749 unsigned int TMC26X::getCoolstepCurrent(void)
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
);
759 return true if the stallguard threshold has been reached
761 bool TMC26X::isStallGuardOverThreshold(void)
763 if (!this->started
) {
766 return (driver_status_result
& STATUS_STALL_GUARD_STATUS
);
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.
775 int8_t TMC26X::getOverTemperature(void)
777 if (!this->started
) {
780 if (driver_status_result
& STATUS_OVER_TEMPERATURE_SHUTDOWN
) {
781 return TMC26X_OVERTEMPERATURE_SHUTDOWN
;
783 if (driver_status_result
& STATUS_OVER_TEMPERATURE_WARNING
) {
784 return TMC26X_OVERTEMPERATURE_PREWARING
;
789 //is motor channel A shorted to ground
790 bool TMC26X::isShortToGroundA(void)
792 if (!this->started
) {
795 return (driver_status_result
& STATUS_SHORT_TO_GROUND_A
);
798 //is motor channel B shorted to ground
799 bool TMC26X::isShortToGroundB(void)
801 if (!this->started
) {
804 return (driver_status_result
& STATUS_SHORT_TO_GROUND_B
);
807 //is motor channel A connected
808 bool TMC26X::isOpenLoadA(void)
810 if (!this->started
) {
813 return (driver_status_result
& STATUS_OPEN_LOAD_A
);
816 //is motor channel B connected
817 bool TMC26X::isOpenLoadB(void)
819 if (!this->started
) {
822 return (driver_status_result
& STATUS_OPEN_LOAD_B
);
825 //is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
826 bool TMC26X::isStandStill(void)
828 if (!this->started
) {
831 return (driver_status_result
& STATUS_STAND_STILL
);
834 //is chopper inactive since 2^20 clock cycles - defaults to ~0,08s
835 bool TMC26X::isStallGuardReached(void)
837 if (!this->started
) {
840 return (driver_status_result
& STATUS_STALL_GUARD_STATUS
);
843 //reads the stall guard setting from last status
844 //returns -1 if stallguard inforamtion is not present
845 int TMC26X::getReadoutValue(void)
847 return (int)(driver_status_result
>> 10);
850 int TMC26X::getResistor()
852 return this->resistor
;
855 bool TMC26X::isCurrentScalingHalfed()
857 if (this->driver_configuration_register_value
& VSENSE
) {
864 void TMC26X::dumpStatus(StreamOutput
*stream
, bool readable
)
867 stream
->printf("Chip type TMC26X\n");
869 check_error_status_bits(stream
);
871 if (this->isStallGuardReached()) {
872 stream
->printf("INFO: Stall Guard level reached!\n");
875 if (this->isStandStill()) {
876 stream
->printf("INFO: Motor is standing still.\n");
879 int value
= getReadoutValue();
880 stream
->printf("Microstep postion phase A: %d\n", value
);
882 value
= getCurrentStallGuardReading();
883 stream
->printf("Stall Guard value: %d\n", value
);
885 stream
->printf("Current setting: %dmA\n", getCurrent());
886 stream
->printf("Coolstep current: %dmA\n", getCoolstepCurrent());
888 stream
->printf("Microsteps: 1/%d\n", microsteps
);
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
);
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
903 stream
->printf("#sg%d,p%lu,k%u,r,", getCurrentStallGuardReading(), THEKERNEL
->robot
->actuators
[0]->get_stepped(), getCoolstepCurrent());
905 readStatus(TMC26X_READOUT_POSITION
); // get the status bits
906 stream
->printf("#s,");
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());
914 //print out the general cool step config
915 if (isCoolStepEnabled()) stream
->printf("Ke+,");
916 else stream
->printf("Ke-,");
918 stream
->printf("Kl%u,Ku%u,Kn%u,Ki%u,Km%u,",
919 getCoolStepLowerSgThreshold(), getCoolStepUpperSgThreshold(), getCoolStepNumberOfSGReadings(), getCoolStepCurrentIncrementSize(), getCoolStepLowerCurrentLimit());
921 //detect the winding status
923 stream
->printf("ao,");
924 } else if(isShortToGroundA()) {
925 stream
->printf("ag,");
927 stream
->printf("a-,");
929 //detect the winding status
931 stream
->printf("bo,");
932 } else if(isShortToGroundB()) {
933 stream
->printf("bg,");
935 stream
->printf("b-,");
938 char temperature
= getOverTemperature();
939 if (temperature
== 0) {
940 stream
->printf("x-,");
941 } else if (temperature
== TMC26X_OVERTEMPERATURE_PREWARING
) {
942 stream
->printf("xw,");
944 stream
->printf("xe,");
948 stream
->printf("e1,");
950 stream
->printf("e0,");
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
);
959 stream
->printf("\n");
963 // check error bits and report, only report once
964 bool TMC26X::check_error_status_bits(StreamOutput
*stream
)
967 readStatus(TMC26X_READOUT_POSITION
); // get the status bits
969 if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_PREWARING
) {
970 if(!error_reported
.test(0)) stream
->printf("WARNING: Overtemperature Prewarning!\n");
971 error_reported
.set(0);
973 error_reported
.reset(0);
976 if (this->getOverTemperature()&TMC26X_OVERTEMPERATURE_SHUTDOWN
) {
977 if(!error_reported
.test(1)) stream
->printf("ERROR: Overtemperature Shutdown!\n");
979 error_reported
.set(1);
981 error_reported
.reset(1);
984 if (this->isShortToGroundA()) {
985 if(!error_reported
.test(2)) stream
->printf("ERROR: SHORT to ground on channel A!\n");
987 error_reported
.set(2);
989 error_reported
.reset(2);
992 if (this->isShortToGroundB()) {
993 if(!error_reported
.test(3)) stream
->printf("ERROR: SHORT to ground on channel B!\n");
995 error_reported
.set(3);
997 error_reported
.reset(3);
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");
1004 error_reported
.set(4);
1006 error_reported
.reset(4);
1009 if (this->isOpenLoadB()) {
1010 if(!error_reported
.test(5)) stream
->printf("ERROR: Channel B seems to be unconnected!\n");
1012 error_reported
.set(5);
1014 error_reported
.reset(5);
1018 // stream->printf("%08X\n", driver_status_result);
1023 bool TMC26X::checkAlarm()
1025 return check_error_status_bits(THEKERNEL
->streams
);
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
)
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");
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;
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");
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
1067 void TMC26X::send262(unsigned long datagram
)
1069 uint8_t buf
[] {(uint8_t)(datagram
>> 16), (uint8_t)(datagram
>> 8), (uint8_t)(datagram
& 0xff)};
1072 //write/read the values
1076 unsigned long i_datagram
= ((rbuf
[0] << 16) | (rbuf
[1] << 8) | (rbuf
[2])) >> 4;
1078 //store the datagram as status result
1079 driver_status_result
= i_datagram
;
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]);
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
)
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
);
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'));
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'));
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'));
1115 } else if(s
== 2 && HAS('Z')) {
1116 setRandomOffTime(GET('Z'));
1119 } else if(s
== 3 && HAS('Z')) {
1120 setDoubleEdge(GET('Z'));
1123 } else if(s
== 4 && HAS('Z')) {
1124 setStepInterpolation(GET('Z'));
1127 } else if(s
== 5 && HAS('Z')) {
1128 setCoolStepEnabled(GET('Z') == 1);