-
Notifications
You must be signed in to change notification settings - Fork 8
/
trinamic.c
2509 lines (2104 loc) · 86.9 KB
/
trinamic.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
motors/trinamic.c - Trinamic stepper driver plugin
Part of grblHAL
Copyright (c) 2018-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef ARDUINO
#include "../driver.h"
#else
#include "driver.h"
#endif
#if TRINAMIC_ENABLE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "trinamic.h"
#if TRINAMIC_I2C
#include "../trinamic/tmc_i2c_interface.h"
#endif
#include "../grbl/nvs_buffer.h"
#include "../grbl/protocol.h"
#include "../grbl/state_machine.h"
#include "../grbl/report.h"
#include "../grbl/platform.h"
#ifndef TRINAMIC_POLL_INTERVAL
#define TRINAMIC_POLL_INTERVAL 250
#endif
static bool warning = false, is_homing = false, settings_loaded = false;
static volatile uint_fast16_t diag1_poll = 0;
static char sbuf[65]; // string buffer for reports
static char min_current[5], max_current[5];
static uint_fast8_t n_motors = 0;
static const tmchal_t *stepper[TMC_N_MOTORS_MAX];
static motor_map_t *motor_map;
static axes_signals_t homing = {0}, otpw_triggered = {0}, driver_enabled = {0};
#if TMC_POLL_STALLED
static limits_get_state_ptr limits_get_state = NULL;
#endif
static limits_enable_ptr limits_enable = NULL;
static stepper_pulse_start_ptr hal_stepper_pulse_start = NULL;
static nvs_address_t nvs_address;
static on_realtime_report_ptr on_realtime_report;
static on_report_options_ptr on_report_options;
static driver_setup_ptr driver_setup;
static settings_changed_ptr settings_changed;
static user_mcode_ptrs_t user_mcode;
static trinamic_driver_if_t driver_if = {0};
static trinamic_settings_t trinamic;
static trinamic_cfg_t cfg_cap;
#ifdef TRINAMIC_EXTENDED_SETTINGS
static trinamic_cfg_t *cfg_params = &trinamic.cfg_params;
#else
static trinamic_cfg_t params, *cfg_params = ¶ms;
#endif
#if TRINAMIC_POLL_STATUS
static TMC_drv_status_t status[TMC_N_MOTORS_MAX];
#endif
#if TRINAMIC_DYNAMIC_CURRENT
static uint16_t dynamic_current[TMC_N_MOTORS_MAX], reduced_current[TMC_N_MOTORS_MAX];
static stepper_pulse_start_ptr stepper_block_start = NULL;
#endif
static struct {
bool raw;
bool sg_status_enable;
volatile bool sg_status;
bool sfilt;
uint32_t sg_status_motor;
axes_signals_t sg_status_motormask;
uint32_t msteps;
} report = {0};
#if TRINAMIC_I2C
static stepper_enable_ptr stepper_enable = NULL;
TMCI2C_enable_dgr_t dgr_enable = {
.addr.value = TMC_I2CReg_ENABLE
};
TMCI2C_monitor_status_dgr_t dgr_monitor = {
.addr.value = TMC_I2CReg_MON_STATE
};
#endif
static void write_debug_report (uint_fast8_t axes);
// Wrapper for initializing physical interface
void trinamic_if_init (trinamic_driver_if_t *driver)
{
memcpy(&driver_if, driver, sizeof(trinamic_driver_if_t));
}
#if TRINAMIC_POLL_STATUS
static void trinamic_status_report (void *data)
{
uint_fast8_t motor = 0;
uint_fast16_t axis;
for(motor = 0; motor < n_motors; motor++) {
if(stepper[motor]) {
if(status[motor].stst) {
axis = stepper[motor]->get_config(motor)->motor.axis;
hal.stream.write("[MOST:");
hal.stream.write(uitoa(axis));
//hal.stream.write("]" ASCII_EOL);
hal.stream.write("[CURR:");
hal.stream.write(uitoa((trinamic.driver[axis].current)));
//hal.stream.write("]" ASCII_EOL);
//hal.delay_ms(15, NULL);
#if TRINAMIC_DYNAMIC_CURRENT
hal.stream.write("[STCR:");
hal.stream.write(uitoa(reduced_current[motor]));
#endif
hal.stream.write("]" ASCII_EOL);
//hal.delay_ms(15, NULL);
}
/*if(status[motor].stallguard){
strcpy(sbuf, "SG:M ");
strcat(sbuf, uitoa(motor));
report_message(sbuf, Message_Warning);
}*/
if(status[motor].otpw){
strcpy(sbuf, "Over-Temperature Motor: ");
strcat(sbuf, uitoa(motor));
report_message(sbuf, Message_Warning);
}
}
}
}
static void trinamic_poll_status (void *data)
{
static bool error_active = false, error_hold = false;
static uint32_t error_count = 0;
static hold_state_t holding_state = Hold_NotHolding;
uint_fast8_t motor = n_motors;
uint8_t stall_fault = 0, otpw_fault = 0;
sys_state_t current_state = state_get();
task_add_delayed(trinamic_poll_status, NULL, TRINAMIC_POLL_INTERVAL);
do {
if(stepper[--motor]) {
if(current_state == STATE_IDLE)
status[motor] = stepper[motor]->get_drv_status(motor);
else if(current_state == STATE_HOLD && holding_state == Hold_Pending && sys.holding_state == Hold_Complete) {
holding_state == Hold_Complete; // potentially dangerous if spindle is running and is lowering itself due to gravity pull,
st_go_idle(); // so disabled for now (see below).
}
if(status[motor].otpw) // overtemp?
otpw_fault |= (1 << motor);
/* if(status[motor].stallguard) // stalled?
stall_fault |= (1 << motor); */
#if TRINAMIC_DYNAMIC_CURRENT
uint_fast8_t axis = motor_map[motor].axis;
if((current_state == STATE_IDLE || (current_state & (STATE_ALARM|STATE_HOLD))) && dynamic_current[motor] == trinamic.driver[axis].current)
stepper[motor]->set_current(motor, (dynamic_current[motor] = reduced_current[motor]), trinamic.driver[axis].hold_current_pct);
#endif
}
} while(motor);
if(stall_fault || otpw_fault) {
if(++error_count > 2 && !error_active) {
error_active = true;
if(current_state & (STATE_CYCLE|STATE_HOMING|STATE_JOG)) {
// holding_state == Hold_Pending;
grbl.enqueue_realtime_command(CMD_FEED_HOLD);
}
task_add_immediate(trinamic_status_report, NULL);
}
} else if(error_active) {
error_active = false;
error_count = 0;
if(holding_state != Hold_NotHolding) {
holding_state = Hold_NotHolding;
st_wake_up();
}
}
}
#endif // TRINAMIC_POLL_STATUS
#if TRINAMIC_DYNAMIC_CURRENT
static void set_current_for_block (void *block)
{
uint_fast16_t axis = 0;
uint_fast8_t motor = n_motors;
do {
if(stepper[--motor]) {
axis = motor_map[motor].axis;
if((((stepper_t *)block)->steps[axis] ? trinamic.driver[axis].current : reduced_current[motor]) != dynamic_current[motor]) {
dynamic_current[motor] = ((stepper_t *)block)->steps[axis] ? trinamic.driver[axis].current : reduced_current[motor];
stepper[motor]->set_current(motor, dynamic_current[motor], trinamic.driver[axis].hold_current_pct);
}
#if TRINAMIC_POLL_STATUS
status[motor] = stepper[motor]->get_drv_status(motor);
#endif
}
} while(motor);
}
static void set_current_for_homing (void)
{
uint_fast16_t axis = 0;
uint_fast8_t motor = n_motors;
do {
axis = motor_map[--motor].axis;
if(stepper[motor] && dynamic_current[motor] != trinamic.driver[axis].current)
stepper[motor]->set_current(motor, (dynamic_current[motor] = trinamic.driver[axis].current), trinamic.driver[axis].hold_current_pct);
} while(motor);
}
static void dynamic_current_pulse_start (stepper_t *stepper)
{
if(stepper->new_block && !is_homing)
task_add_immediate(set_current_for_block, stepper);
stepper_block_start(stepper);
}
#endif // TRINAMIC_DYNAMIC_CURRENT
static bool trinamic_driver_config (motor_map_t motor, uint8_t seq)
{
bool ok = false;
trinamic_driver_config_t cfg = {
.address = motor.id,
.settings = &trinamic.driver[motor.axis]
};
if(driver_if.on_driver_preinit)
driver_if.on_driver_preinit(motor, &cfg);
#if TRINAMIC_ENABLE == 2209
ok = (stepper[motor.id] = TMC2209_AddMotor(motor, cfg.address, cfg.settings->current, cfg.settings->microsteps, cfg.settings->r_sense)) != NULL;
#elif TRINAMIC_ENABLE == 2660
uint8_t retries = 25;
do {
if(!(ok = (stepper[motor.id] = TMC2660_AddMotor(motor, cfg.settings->current, cfg.settings->microsteps, cfg.settings->r_sense)) != NULL))
hal.delay_ms(10, NULL);
} while(!ok && --retries);
#elif TRINAMIC_ENABLE == 2130
ok = (stepper[motor.id] = TMC2130_AddMotor(motor, cfg.settings->current, cfg.settings->microsteps, cfg.settings->r_sense)) != NULL;
#elif TRINAMIC_ENABLE == 5160
ok = (stepper[motor.id] = TMC5160_AddMotor(motor, cfg.settings->current, cfg.settings->microsteps, cfg.settings->r_sense)) != NULL;
#endif
if(!ok) {
protocol_enqueue_foreground_task(report_warning, "Could not communicate with stepper driver!");
// system_raise_alarm(Alarm_SelftestFailed);
return false;
}
stepper[motor.id]->get_config(motor.id)->motor.seq = seq; //
driver_enabled.mask |= bit(motor.axis);
switch(motor.axis) {
case X_AXIS:
#if TRINAMIC_I2C && TMC_X_MONITOR
dgr_enable.reg.monitor.x = TMC_X_MONITOR;
#endif
break;
case Y_AXIS:
#if TRINAMIC_I2C && TMC_Y_MONITOR
dgr_enable.reg.monitor.y = TMC_Y_MONITOR;
#endif
break;
case Z_AXIS:
#if TRINAMIC_I2C && TMC_Z_MONITOR
dgr_enable.reg.monitor.z = TMC_Z_MONITOR;
#endif
break;
#ifdef A_AXIS
case A_AXIS:
#if TRINAMIC_I2C && TMC_A_MONITOR
dgr_enable.reg.monitor.a = TMC_A_MONITOR;
#endif
break;
#endif
#ifdef B_AXIS
case B_AXIS:
#if TRINAMIC_I2C && TMC_B_MONITOR
dgr_enable.reg.monitor.b = TMC_B_MONITOR;
#endif
break;
#endif
#ifdef C_AXIS
case C_AXIS:
#if TRINAMIC_I2C && TMC_C_MONITOR
dgr_enable.reg.monitor.c = TMC_C_MONITOR;
#endif
break;
#endif
#ifdef U_AXIS
case U_AXIS:
#if TRINAMIC_I2C && TMC_U_MONITOR
dgr_enable.reg.monitor.u = TMC_U_MONITOR;
#endif
break;
#endif
#ifdef V_AXIS
case V_AXIS:
#if TRINAMIC_I2C && TMC_V_MONITOR
dgr_enable.reg.monitor.v = TMC_V_MONITOR;
#endif
break;
#endif
}
stepper[motor.id]->sg_filter(motor.id, 1);
stepper[motor.id]->coolconf(motor.id, cfg_params->coolconf);
stepper[motor.id]->chopper_timing(motor.id, cfg_params->chopconf);
if(stepper[motor.id]->stealthChop)
stepper[motor.id]->stealthChop(motor.id, cfg.settings->mode == TMCMode_StealthChop);
else if(cfg.settings->mode == TMCMode_StealthChop)
cfg.settings->mode = TMCMode_CoolStep;
#if PWM_THRESHOLD_VELOCITY > 0
stepper[motor.id]->set_tpwmthrs(motor.id, (float)PWM_THRESHOLD_VELOCITY / 60.0f, settings.axis[motor.axis].steps_per_mm);
#endif
stepper[motor.id]->set_current(motor.id, cfg.settings->current, cfg.settings->hold_current_pct);
stepper[motor.id]->set_microsteps(motor.id, cfg.settings->microsteps);
#if TRINAMIC_I2C
tmc_spi_write((trinamic_motor_t){0}, (TMC_spi_datagram_t *)&dgr_enable);
#endif
#if TRINAMIC_DYNAMIC_CURRENT
reduced_current[motor.id] = cfg.settings->current * cfg.settings->hold_current_pct / 100;;
#endif
if(driver_if.on_driver_postinit)
driver_if.on_driver_postinit(motor, stepper[motor.id]);
return true;
}
#if 1 // Region settings
static void trinamic_drivers_init (axes_signals_t axes)
{
bool ok = axes.value != 0;
uint_fast8_t motor = n_motors, n_enabled = 0, seq = 0;
memset(stepper, 0, sizeof(stepper));
do {
if(bit_istrue(axes.mask, bit(motor_map[--motor].axis)))
seq++;
} while(motor);
motor = n_motors;
*min_current = '\0';
do {
if(bit_istrue(axes.mask, bit(motor_map[--motor].axis))) {
if((ok = trinamic_driver_config(motor_map[motor], --seq))) {
n_enabled++;
if(*min_current == '\0') {
strcpy(min_current, uitoa(stepper[motor_map[motor].id]->get_current(motor_map[motor].id, TMCCurrent_Min)));
strcpy(max_current, uitoa(stepper[motor_map[motor].id]->get_current(motor_map[motor].id, TMCCurrent_Max)));
}
}
}
} while(ok && motor);
tmc_motors_set(ok ? n_enabled : 0);
if(!ok) {
driver_enabled.mask = 0;
memset(stepper, 0, sizeof(stepper));
}
#if TRINAMIC_POLL_STATUS || TRINAMIC_DYNAMIC_CURRENT
else {
#if TRINAMIC_POLL_STATUS
task_add_delayed(trinamic_poll_status, NULL, TRINAMIC_POLL_INTERVAL);
#endif
#if TRINAMIC_DYNAMIC_CURRENT
if(stepper_block_start == NULL) {
stepper_block_start = hal.stepper.pulse_start;
hal.stepper.pulse_start = dynamic_current_pulse_start;
}
#endif
}
#endif
}
static void trinamic_drivers_setup (void)
{
if(driver_if.on_drivers_init) {
uint8_t n_enabled = 0, motor = n_motors;
do {
if(bit_istrue(trinamic.driver_enable.mask, bit(motor_map[--motor].axis)))
n_enabled++;
} while(motor);
driver_if.on_drivers_init(n_enabled, trinamic.driver_enable);
}
trinamic_drivers_init(trinamic.driver_enable);
}
#if TRINAMIC_MIXED_DRIVERS
static status_code_t set_driver_enable (setting_id_t id, uint_fast16_t value)
{
if(trinamic.driver_enable.mask != (uint8_t)value) {
driver_enabled.mask = 0;
trinamic.driver_enable.mask = (uint8_t)value;
trinamic_drivers_setup();
}
return Status_OK;
}
static uint32_t get_driver_enable (setting_id_t setting)
{
return trinamic.driver_enable.mask;
}
#endif
// Parse and set driver specific parameters
static status_code_t set_axis_setting (setting_id_t setting, uint_fast16_t value)
{
uint_fast8_t axis, motor = n_motors;
status_code_t status = Status_OK;
switch(settings_get_axis_base(setting, &axis)) {
case Setting_AxisStepperCurrent:
trinamic.driver[axis].current = (uint16_t)value;
do {
motor--;
if(stepper[motor] && stepper[motor]->get_config(motor)->motor.axis == axis) {
stepper[motor]->set_current(motor, trinamic.driver[axis].current, trinamic.driver[axis].hold_current_pct);
#if TRINAMIC_DYNAMIC_CURRENT
reduced_current[motor] = trinamic.driver[axis].current * trinamic.driver[axis].hold_current_pct / 100;
#endif
}
} while(motor);
break;
case Setting_AxisExtended1: // Hold current percentage
if(value > 100)
value = 100;
trinamic.driver[axis].hold_current_pct = (uint16_t)value;
do {
motor--;
if(stepper[motor] && stepper[motor]->get_config(motor)->motor.axis == axis) {
stepper[motor]->set_current(motor, trinamic.driver[axis].current, trinamic.driver[axis].hold_current_pct);
#if TRINAMIC_DYNAMIC_CURRENT
reduced_current[motor] = trinamic.driver[axis].current * trinamic.driver[axis].hold_current_pct / 100;
#endif
}
} while(motor);
break;
case Setting_AxisMicroSteps:
do {
motor--;
if(stepper[motor] && stepper[motor]->get_config(motor)->motor.axis == axis) {
if(stepper[motor]->microsteps_isvalid(motor, (uint16_t)value)) {
trinamic.driver[axis].microsteps = value;
stepper[motor]->set_microsteps(motor, trinamic.driver[axis].microsteps);
if(report.sg_status_motormask.mask & bit(axis))
report.msteps = trinamic.driver[axis].microsteps;
} else {
status = Status_InvalidStatement;
break;
}
}
} while(motor);
break;
default:
status = Status_Unhandled;
break;
}
return status;
}
static uint32_t get_axis_setting (setting_id_t setting)
{
uint32_t value = 0;
uint_fast8_t idx;
switch(settings_get_axis_base(setting, &idx)) {
case Setting_AxisStepperCurrent:
value = trinamic.driver[idx].current;
break;
case Setting_AxisExtended1: // Hold current percentage
value = trinamic.driver[idx].hold_current_pct;
break;
case Setting_AxisMicroSteps:
value = trinamic.driver[idx].microsteps;
break;
default: // for stopping compiler warning
break;
}
return value;
}
// Parse and set driver specific parameters
static status_code_t set_axis_setting_float (setting_id_t setting, float value)
{
status_code_t status = Status_OK;
uint_fast8_t idx;
switch(settings_get_axis_base(setting, &idx)) {
case Setting_AxisHomingFeedRate:
trinamic.driver[idx].homing_feed_rate = value;
break;
case Setting_AxisHomingSeekRate:
trinamic.driver[idx].homing_seek_rate = value;
break;
case Setting_AxisExtended0:
trinamic.driver[idx].homing_seek_sensitivity = (int16_t)value;
break;
case Setting_AxisExtended2:
trinamic.driver[idx].homing_feed_sensitivity = (int16_t)value;
break;
default:
status = Status_Unhandled;
break;
}
return status;
}
static float get_axis_setting_float (setting_id_t setting)
{
float value = 0.0f;
uint_fast8_t idx;
switch(settings_get_axis_base(setting, &idx)) {
case Setting_AxisHomingFeedRate:
value = trinamic.driver[idx].homing_feed_rate;
break;
case Setting_AxisHomingSeekRate:
value = trinamic.driver[idx].homing_seek_rate;
break;
case Setting_AxisExtended0:
value = (float)trinamic.driver[idx].homing_seek_sensitivity;
break;
case Setting_AxisExtended2:
value = (float)trinamic.driver[idx].homing_feed_sensitivity;
break;
default: // for stopping compiler warning
break;
}
return value;
}
#ifdef TRINAMIC_EXTENDED_SETTINGS
static status_code_t set_extended (setting_id_t id, uint_fast16_t value)
{
switch(id) {
case Setting_Stepper1:
trinamic.cfg_params.chopconf.toff = value;
break;
case Setting_Stepper2:
trinamic.cfg_params.chopconf.tbl = value;
break;
case Setting_Stepper3:
trinamic.cfg_params.chopconf.chm = value;
break;
case Setting_Stepper4:
trinamic.cfg_params.chopconf.hstrt = value - 1;
break;
case Setting_Stepper6:
trinamic.cfg_params.chopconf.hdec = value;
break;
case Setting_Stepper7:
trinamic.cfg_params.chopconf.rndtf = value;
break;
case Setting_Stepper8:
trinamic.cfg_params.chopconf.tbl = value; //
break;
case Setting_Stepper9:
trinamic.cfg_params.coolconf.semin = value;
break;
case Setting_Stepper10:
trinamic.cfg_params.coolconf.seup = value;
break;
case Setting_Stepper11:
trinamic.cfg_params.coolconf.semax = value;
break;
case Setting_Stepper12:
trinamic.cfg_params.coolconf.seimin = value;
break;
case Setting_Stepper13:
trinamic.cfg_params.chopconf.tbl = value;
break;
default:
break;
}
uint_fast8_t motor = n_motors;
do {
if(stepper[--motor]) {
stepper[motor]->coolconf(motor, cfg_params->coolconf);
stepper[motor]->chopper_timing(motor, cfg_params->chopconf);
}
} while(motor);
return Status_OK;
}
static uint32_t get_extended (setting_id_t setting)
{
uint32_t value;
switch(setting) {
case Setting_Stepper1:
value = trinamic.cfg_params.chopconf.toff;
break;
case Setting_Stepper2:
value = trinamic.cfg_params.chopconf.tbl;
break;
case Setting_Stepper3:
value = trinamic.cfg_params.chopconf.chm;
break;
case Setting_Stepper4:
value = trinamic.cfg_params.chopconf.hstrt + 1;
break;
case Setting_Stepper6:
value = trinamic.cfg_params.chopconf.hdec;
break;
case Setting_Stepper7:
value = trinamic.cfg_params.chopconf.rndtf;
break;
case Setting_Stepper8:
value = trinamic.cfg_params.chopconf.tbl; //
break;
case Setting_Stepper9:
value = trinamic.cfg_params.coolconf.semin;
break;
case Setting_Stepper10:
value = trinamic.cfg_params.coolconf.seup;
break;
case Setting_Stepper11:
value = trinamic.cfg_params.coolconf.semax;
break;
case Setting_Stepper12:
value = trinamic.cfg_params.coolconf.seimin;
break;
case Setting_Stepper13:
value = trinamic.cfg_params.chopconf.tbl;
break;
default:
break;
}
return value;
}
static status_code_t set_extended_float (setting_id_t id, float value)
{
switch(id) {
case Setting_Stepper5:
if(isintf(value))
trinamic.cfg_params.chopconf.hend = (int8_t)(value) + 3;
else
return Status_BadNumberFormat;
break;
default:
break;
}
uint_fast8_t motor = n_motors;
do {
if(stepper[--motor])
stepper[motor]->chopper_timing(motor, cfg_params->chopconf);
} while(motor);
return Status_OK;
}
static float get_extended_float (setting_id_t setting)
{
uint32_t value;
switch(setting) {
case Setting_Stepper5:
value = (float)trinamic.cfg_params.chopconf.hend - 3.0f;
break;
default:
break;
}
return value;
}
static bool is_extended_available (const setting_detail_t *setting)
{
bool ok = false;
switch(setting->id) {
case Setting_Stepper1:
ok = !!cfg_cap.chopconf.toff;
break;
case Setting_Stepper2:
ok = !!cfg_cap.chopconf.tbl;
break;
case Setting_Stepper3:
ok = !!cfg_cap.chopconf.chm;
break;
case Setting_Stepper4:
ok = !!cfg_cap.chopconf.hstrt;
break;
case Setting_Stepper5:
ok = !!cfg_cap.chopconf.hend;
break;
case Setting_Stepper6:
ok = !!cfg_cap.chopconf.hdec;
break;
case Setting_Stepper7:
ok = !!cfg_cap.chopconf.rndtf;
break;
case Setting_Stepper8:
ok = !!cfg_cap.chopconf.tbl; //
break;
case Setting_Stepper9:
ok = !!cfg_cap.coolconf.semin;
break;
case Setting_Stepper10:
ok = !!cfg_cap.coolconf.seup;
break;
case Setting_Stepper11:
ok = !!cfg_cap.coolconf.semax;
break;
case Setting_Stepper12:
ok = !!cfg_cap.coolconf.seimin;
break;
case Setting_Stepper13:
ok = !!cfg_cap.chopconf.tbl;
break;
default:
break;
}
return ok;
}
#endif
#define AXIS_OPTS { .subgroups = On, .increment = 1 }
static const setting_detail_t trinamic_settings[] = {
#if TRINAMIC_MIXED_DRIVERS
{ Setting_TrinamicDriver, Group_MotorDriver, "Trinamic driver", NULL, Format_AxisMask, NULL, NULL, NULL, Setting_NonCoreFn, set_driver_enable, get_driver_enable, NULL },
#endif
{ Setting_TrinamicHoming, Group_MotorDriver, "Sensorless homing", NULL, Format_AxisMask, NULL, NULL, NULL, Setting_NonCore, &trinamic.homing_enable.mask, NULL, NULL },
{ Setting_AxisStepperCurrent, Group_Axis0, "-axis motor current", "mA", Format_Integer, "###0", min_current, max_current, Setting_NonCoreFn, set_axis_setting, get_axis_setting, NULL, AXIS_OPTS },
{ Setting_AxisMicroSteps, Group_Axis0, "-axis microsteps", "steps", Format_Integer, "###0", NULL, NULL, Setting_NonCoreFn, set_axis_setting, get_axis_setting, NULL, AXIS_OPTS },
{ Setting_AxisHomingFeedRate, Group_Axis0, "-axis homing locate feed rate", "mm/min", Format_Decimal, "###0", NULL, NULL, Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL, AXIS_OPTS },
{ Setting_AxisHomingSeekRate, Group_Axis0, "-axis homing search seek rate", "mm/min", Format_Decimal, "###0", NULL, NULL, Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL, AXIS_OPTS },
#if TMC_STALLGUARD == 4
{ Setting_AxisExtended0, Group_Axis0, "-axis StallGuard4 fast threshold", NULL, Format_Decimal, "##0", "0", "255", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL, AXIS_OPTS },
#else
{ Setting_AxisExtended0, Group_Axis0, "-axis StallGuard2 fast threshold", NULL, Format_Decimal, "-##0", "-64", "63", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL, AXIS_OPTS },
#endif
{ Setting_AxisExtended1, Group_Axis0, "-axis hold current", "%", Format_Int8, "##0", "5", "100", Setting_NonCoreFn, set_axis_setting, get_axis_setting, NULL, AXIS_OPTS },
#if TMC_STALLGUARD == 4
{ Setting_AxisExtended2, Group_Axis0, "-axis StallGuard4 slow threshold", NULL, Format_Decimal, "##0", "0", "255", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL, AXIS_OPTS },
#else
{ Setting_AxisExtended2, Group_Axis0, "-axis stallGuard2 slow threshold", NULL, Format_Decimal, "-##0", "-64", "63", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL, AXIS_OPTS },
#endif
#ifdef TRINAMIC_EXTENDED_SETTINGS
{ Setting_Stepper1, Group_MotorDriver, "Chopper toff", NULL, Format_Int8, "#0", "1", "15", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper2, Group_MotorDriver, "Chopper tbl", NULL, Format_Int8, "0", "0", "3", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper3, Group_MotorDriver, "Chopper mode", NULL, Format_RadioButtons, "Spreadcycle,Constant toff", NULL, NULL, Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper4, Group_MotorDriver, "Chopper hstrt", NULL, Format_Int8, "0", "1", "8", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper5, Group_MotorDriver, "Chopper hend", NULL, Format_Decimal, "-#0", "-3", "12", Setting_NonCoreFn, &set_extended_float, &get_extended_float, is_extended_available },
{ Setting_Stepper6, Group_MotorDriver, "Chopper hdec", NULL, Format_Int8, "0", "0", "3", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper7, Group_MotorDriver, "Chopper random TOFF", NULL, Format_Bool, NULL, NULL, NULL, Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
//?? { Setting_Stepper8, Group_MotorDriver, "THRESH", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available }, // == Setting_AxisExtended0 & Setting_AxisExtended1?
{ Setting_Stepper9, Group_MotorDriver, "CoolStep semin", NULL, Format_Int8, "#0", "0", "15", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper10, Group_MotorDriver, "CoolStep seup", NULL, Format_Int8, "0", "0", "3", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper11, Group_MotorDriver, "CoolStep semax", NULL, Format_Int8, "#0", "0", "15", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper12, Group_MotorDriver, "CoolStep sedn", NULL, Format_Int8, "0", "0", "3", Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper13, Group_MotorDriver, "CoolStep seimin", NULL, Format_RadioButtons, "0.5 x CS,.25 x CS", NULL, NULL, Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
{ Setting_Stepper14, Group_MotorDriver, "drvconf_reg", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCoreFn, &set_extended, &get_extended, is_extended_available },
#endif
};
#ifndef NO_SETTINGS_DESCRIPTIONS
static const setting_descr_t trinamic_settings_descr[] = {
#if TRINAMIC_MIXED_DRIVERS
{ Setting_TrinamicDriver, "Enable SPI or UART controlled Trinamic drivers for axes." },
#endif
{ Setting_TrinamicHoming, "Enable sensorless homing for axes. Requires SPI or UART controlled Trinamic drivers." },
{ Setting_AxisStepperCurrent, "Motor current in mA (RMS)." },
{ Setting_AxisMicroSteps, "Microsteps per fullstep." },
{ Setting_AxisExtended0, "StallGuard threshold for fast (seek) homing phase." },
{ Setting_AxisExtended1, "Motor current at standstill as a percentage of full current.\\n"
"NOTE: if grblHAL is configured to disable motors on standstill this setting has no use."
},
{ Setting_AxisExtended2, "StallGuard threshold for slow (feed) homing phase." },
{ Setting_AxisHomingFeedRate, "Feed rate to slowly engage limit switch to determine its location accurately.\\n"
"NOTE: only used for axes with Trinamic driver enabled, others use the $24 setting."
},
{ Setting_AxisHomingSeekRate, "Seek rate to quickly find the limit switch before the slower locating phase.\\n"
"NOTE: only used for axes with Trinamic driver enabled, others use the $25 setting."
},
#ifdef TRINAMIC_EXTENDED_SETTINGS
{ Setting_Stepper1, "Off time. Duration of slow decay phase as a multiple of system clock periods: NCLK= 24 + (32 x TOFF). This will limit the maximum chopper frequency (0-15).\\n"
"0: MOSFETs shut off, driver disabled.\\n"
"1: Use with TBL of minimum 24 clocks." },
{ Setting_Stepper2, "Blanking time interval in system clock periods (0-3 = 16,24,36,54). Needs to cover the switching event and the duration of the ringing on the sense resistor." },
{ Setting_Stepper3, "Chopper mode. Affects HDEC, HEND, and HSTRT parameters.\\n"
"0: Standard mode (SpreadCycle).\\n"
"1: Constant TOFF with fast decay time. Fast decay is after on time. Fast decay time is also terminated when the negative nominal current is reached." },
{ Setting_Stepper4, "CHM=0: Hysteresis start, offset from HEND (1-8). To be effective, HEND+HSTRT must be ≤15.\\n"
"CHM=1: Fast decay time. Three least-significant bits of the duration of the fast decay phase. The MSB is HDEC0. Fast decay time is a multiple of system clock periods: NCLK= 32 x (HDEC0+HSTRT)."},
{ Setting_Stepper5, "Can be either negative, zero, or positive, -3 to 12.\\n"
"CHM=0: Hysteresis end (low). Sets the hysteresis end value after a number of decrements, used for the hysteresis chopper and controlled by HDEC. HSTRT+HEND must be less than 16. 1/512 adds to the current setting.\\n"
"CHM=1: Sine wave offset. A positive offset corrects for zero crossing error. 1/512 adds to the absolute value of each sine wave entry." },
{ Setting_Stepper6, "CHM=0: Hysteresis decrement interval period in system clock periods. Determines the slope of the hysteresis during on time from fast to very slow (0-3 = 16,32,48,64).\\n"
"CHM=1: Fast decay mode." },
{ Setting_Stepper7, "Change from fixed to randomized TOFF times, by dNCLK= -24 to +6 clocks." },
// { Setting_Stepper8, "StallGuard threshold." },
{ Setting_Stepper9, "Lower CoolStep threshold. If the SG value falls below SEMIN x 32, the coil current scaling factor is increased (0-15).\\n"
"0: CoolStep disabled."},
{ Setting_Stepper10, "Number of increments of the coil current each time SG is sampled below the lower threshold (0-3 = 1,2,4,8)." },
{ Setting_Stepper11, "Upper CoolStep threshold offset from lower threshold. If SG is sampled above (SEMIN+SEMAX+1)x32 enough times, the coil current scaling factor is decremented (0-15)." },
{ Setting_Stepper12, "Number of times SG must be sampled above the upper threshold before the coil current is decremented (0-3 = 32,8,2,1)." },
{ Setting_Stepper13, "Minimum CoolStep current as a factor of the set motor current\\n"
"0: 1/2, 1: 1/4" },
{ Setting_Stepper14, "DRVCONF register." },
#endif
};
#endif
static void trinamic_settings_save (void)
{
hal.nvs.memcpy_to_nvs(nvs_address, (uint8_t *)&trinamic, sizeof(trinamic_settings_t), true);
}
static void trinamic_settings_get_defaults (bool cap_only)
{
const trinamic_cfg_params_t *params;
#if TRINAMIC_ENABLE == 2209
params = TMC2209_GetConfigDefaults();
#elif TRINAMIC_ENABLE == 2660
params = TMC2660_GetConfigDefaults();
#elif TRINAMIC_ENABLE == 2130
params = TMC2130_GetConfigDefaults();
#elif TRINAMIC_ENABLE == 5160
params = TMC5160_GetConfigDefaults();
#endif
memcpy(&cfg_cap, ¶ms->dflt, sizeof(trinamic_cfg_t));
if(!cap_only)
memcpy(cfg_params, ¶ms->dflt, sizeof(trinamic_cfg_t));
#ifdef TMC_DRVCONF
cfg_params->drvconf = TMC_DRVCONF;
#endif
#ifdef TMC_COOLCONF_SEMIN
cfg_params->coolconf.semin = TMC_COOLCONF_SEMIN & cfg_cap.coolconf.semin;
#endif
#ifdef TMC_COOLCONF_SEMAX
cfg_params->coolconf.semax = TMC_COOLCONF_SEMAX & cfg_cap.coolconf.semax;
#endif
#ifdef TMC_COOLCONF_SEDN
cfg_params->coolconf.sedn = TMC_COOLCONF_SEDN & cfg_cap.coolconf.sedn;
#endif
#ifdef TMC_COOLCONF_SEUP
cfg_params->coolconf.seup = TMC_COOLCONF_SEUP & cfg_cap.coolconf.seup;
#endif
#ifdef TMC_COOLCONF_SEIMIN
cfg_params->coolconf.seimin = TMC_COOLCONF_SEIMIN & cfg_cap.coolconf.seimin;
#endif
#ifdef TMC_CHOPCONF_HSTRT
cfg_params->chopconf.hstrt = (TMC_CHOPCONF_HSTRT - 1) & cfg_cap.chopconf.hstrt;
#endif
#ifdef TMC_CHOPCONF_HEND
cfg_params->chopconf.hend = (TMC_CHOPCONF_HEND + 3) & cfg_cap.chopconf.hend;
#endif
#ifdef TMC_CHOPCONF_TBL
cfg_params->chopconf.tbl = TMC_CHOPCONF_TBL & cfg_cap.chopconf.tbl;
#endif