forked from grblHAL/Plugins_motor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trinamic.c
1589 lines (1326 loc) · 54.3 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-2021 Terje Io
Grbl 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.
Grbl 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 Grbl. 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"
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 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};
static limits_get_state_ptr limits_get_state = NULL;
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 trinamic_on_drivers_init_ptr on_drivers_init;
static on_homing_rate_set_ptr on_homing_rate_set;
static user_mcode_ptrs_t user_mcode;
static trinamic_settings_t trinamic;
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)
{
on_drivers_init = driver->on_drivers_init;
}
#if 1 // Region settings
static void trinamic_drivers_init (axes_signals_t axes);
static void trinamic_settings_load (void);
static void trinamic_settings_restore (void);
static void trinamic_on_homing_rate_set(axes_signals_t cycle, float homing_rate);
static status_code_t set_axis_setting (setting_id_t setting, uint_fast16_t value);
static uint32_t get_axis_setting (setting_id_t setting);
static status_code_t set_axis_setting_float (setting_id_t setting, float value);
static float get_axis_setting_float (setting_id_t setting);
#if TRINAMIC_MIXED_DRIVERS
static status_code_t set_driver_enable (setting_id_t id, uint_fast16_t value);
static uint32_t get_driver_enable (setting_id_t setting);
#endif
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", NULL, NULL, Setting_NonCoreFn, set_axis_setting, get_axis_setting, NULL },
{ Setting_AxisMicroSteps, Group_Axis0, "?-axis microsteps", "steps", Format_Integer, "###0", NULL, NULL, Setting_NonCoreFn, set_axis_setting, get_axis_setting, NULL },
#if TMC_STALLGUARD == 4
{ Setting_AxisExtended0, Group_Axis0, "?-axis stallGuard4 threshold", NULL, Format_Decimal, "##0", "0", "255", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL },
#else
{ Setting_AxisExtended0, Group_Axis0, "?-axis stallGuard2 threshold", NULL, Format_Decimal, "-##0", "-64", "63", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL },
#endif
{ Setting_AxisExtended1, Group_Axis0, "?-axis hold current", "%", Format_Int8, "##0", "5", "100", Setting_NonCoreFn, set_axis_setting, get_axis_setting, NULL },
#if TMC_STALLGUARD == 4
{ Setting_AxisExtended2, Group_Axis0, "?-axis homing feed stallGuard4 threshold", NULL, Format_Decimal, "##0", "0", "255", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL }
#else
{ Setting_AxisExtended2, Group_Axis0, "?-axis homing feed stallGuard2 threshold", NULL, Format_Decimal, "-##0", "-64", "63", Setting_NonCoreFn, set_axis_setting_float, get_axis_setting_float, NULL }
#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 axis. Requires SPI controlled Trinamic drivers." },
{ Setting_AxisStepperCurrent, "Axis motor current in mA (RMS)." },
{ Setting_AxisMicroSteps, "Axis microsteps per fullstep." },
{ Setting_AxisExtended0, "Axis stallGuard threshold" },
{ 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, "Axis stallGuard threshold for homing feed" }
};
#endif
static void trinamic_settings_save (void)
{
hal.nvs.memcpy_to_nvs(nvs_address, (uint8_t *)&trinamic, sizeof(trinamic_settings_t), true);
}
static setting_details_t details = {
.settings = trinamic_settings,
.n_settings = sizeof(trinamic_settings) / sizeof(setting_detail_t),
#ifndef NO_SETTINGS_DESCRIPTIONS
.descriptions = trinamic_settings_descr,
.n_descriptions = sizeof(trinamic_settings_descr) / sizeof(setting_descr_t),
#endif
.load = trinamic_settings_load,
.save = trinamic_settings_save,
.restore = trinamic_settings_restore
};
static setting_details_t *on_get_settings (void)
{
return &details;
}
static void trinamic_drivers_setup (void)
{
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);
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);
} 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);
} 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[motor].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_AxisExtended0:
trinamic.driver[idx].homing_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_AxisExtended0:
value = (float)trinamic.driver[idx].homing_sensitivity;
break;
case Setting_AxisExtended2:
value = (float)trinamic.driver[idx].homing_feed_sensitivity;
break;
default: // for stopping compiler warning
break;
}
return value;
}
// Initialize default EEPROM settings
static void trinamic_settings_restore (void)
{
uint_fast8_t idx = N_AXIS;
trinamic.driver_enable.mask = driver_enabled.mask = 0;
trinamic.homing_enable.mask = 0;
do {
trinamic.driver[idx].mode = TMCMode_StealthChop;
switch(--idx) {
case X_AXIS:
trinamic.driver_enable.x = TMC_X_ENABLE;
trinamic.driver[idx].current = TMC_X_CURRENT;
trinamic.driver[idx].hold_current_pct = TMC_X_HOLD_CURRENT_PCT;
trinamic.driver[idx].microsteps = TMC_X_MICROSTEPS;
trinamic.driver[idx].r_sense = TMC_X_R_SENSE;
trinamic.driver[idx].homing_sensitivity = TMC_X_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_X_HOME_FEED_SGT;
break;
case Y_AXIS:
trinamic.driver_enable.y = TMC_Y_ENABLE;
trinamic.driver[idx].current = TMC_Y_CURRENT;
trinamic.driver[idx].hold_current_pct = TMC_Y_HOLD_CURRENT_PCT;
trinamic.driver[idx].microsteps = TMC_Y_MICROSTEPS;
trinamic.driver[idx].r_sense = TMC_Y_R_SENSE;
trinamic.driver[idx].homing_sensitivity = TMC_Y_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_Y_HOME_FEED_SGT;
break;
case Z_AXIS:
trinamic.driver_enable.z = TMC_Z_ENABLE;
trinamic.driver[idx].current = TMC_Z_CURRENT;
trinamic.driver[idx].hold_current_pct = TMC_Z_HOLD_CURRENT_PCT;
trinamic.driver[idx].microsteps = TMC_Z_MICROSTEPS;
trinamic.driver[idx].r_sense = TMC_Z_R_SENSE;
trinamic.driver[idx].homing_sensitivity = TMC_Z_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_Z_HOME_FEED_SGT;
break;
#ifdef A_AXIS
case A_AXIS:
trinamic.driver_enable.z = TMC_A_ENABLE;
trinamic.driver[idx].current = TMC_A_CURRENT;
trinamic.driver[idx].hold_current_pct = TMC_A_HOLD_CURRENT_PCT;
trinamic.driver[idx].microsteps = TMC_A_MICROSTEPS;
trinamic.driver[idx].r_sense = TMC_A_R_SENSE;
trinamic.driver[idx].homing_sensitivity = TMC_A_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_A_HOME_FEED_SGT;
break;
#endif
#ifdef B_AXIS
case B_AXIS:
trinamic.driver_enable.z = TMC_B_ENABLE;
trinamic.driver[idx].current = TMC_B_CURRENT;
trinamic.driver[idx].hold_current_pct = TMC_B_HOLD_CURRENT_PCT;
trinamic.driver[idx].microsteps = TMC_B_MICROSTEPS;
trinamic.driver[idx].r_sense = TMC_B_R_SENSE;
trinamic.driver[idx].homing_sensitivity = TMC_B_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_B_HOME_FEED_SGT;
break;
#endif
#ifdef C_AXIS
case C_AXIS:
trinamic.driver_enable.z = TMC_C_ENABLE;
trinamic.driver[idx].current = TMC_C_CURRENT;
trinamic.driver[idx].hold_current_pct = TMC_C_HOLD_CURRENT_PCT;
trinamic.driver[idx].microsteps = TMC_C_MICROSTEPS;
trinamic.driver[idx].r_sense = TMC_C_R_SENSE;
trinamic.driver[idx].homing_sensitivity = TMC_C_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_C_HOME_FEED_SGT;
break;
#endif
}
} while(idx);
hal.nvs.memcpy_to_nvs(nvs_address, (uint8_t *)&trinamic, sizeof(trinamic_settings_t), true);
if(settings_loaded)
trinamic_drivers_setup();
}
static void trinamic_settings_load (void)
{
if(hal.nvs.memcpy_from_nvs((uint8_t *)&trinamic, nvs_address, sizeof(trinamic_settings_t), true) != NVS_TransferResult_OK)
trinamic_settings_restore();
else {
uint_fast8_t idx = N_AXIS;
do {
#if TMC_STALLGUARD == 4
if(trinamic.driver[--idx].homing_sensitivity < 0)
trinamic.driver[idx].homing_sensitivity = 0;
if(trinamic.driver[idx].homing_feed_sensitivity < 0)
trinamic.driver[idx].homing_feed_sensitivity = 0;
#else
if(trinamic.driver[--idx].homing_sensitivity > 64)
trinamic.driver[idx].homing_sensitivity = 0;
if(trinamic.driver[--idx].homing_feed_sensitivity > 64)
trinamic.driver[idx].homing_feed_sensitivity = 0;
#endif
} while(idx);
}
#if !TRINAMIC_MIXED_DRIVERS
trinamic.driver_enable.mask = AXES_BITMASK;
#endif
settings_loaded = true;
}
static void on_settings_changed (settings_t *settings)
{
static bool init_ok = false;
static float steps_per_mm[N_AXIS];
uint_fast8_t idx = N_AXIS;
settings_changed(settings);
if(init_ok) {
do {
idx--;
if(steps_per_mm[idx] != settings->axis[idx].steps_per_mm) {
steps_per_mm[idx] = settings->axis[idx].steps_per_mm;
#if PWM_THRESHOLD_VELOCITY > 0
uint8_t motor = n_motors;
do {
motor--;
if(bit_istrue(driver_enabled.mask, bit(idx)) && idx == motor_map[motor].axis)
stepper[motor]->set_tpwmthrs(motor, (float)PWM_THRESHOLD_VELOCITY / 60.0f, steps_per_mm[idx]);
} while(motor);
#endif
}
} while(idx);
} else {
init_ok = true;
do {
idx--;
steps_per_mm[idx] = settings->axis[idx].steps_per_mm;
} while(idx);
}
}
#endif // End region settings
static void pos_failed (sys_state_t state)
{
report_message("Could not communicate with stepper driver!", Message_Warning);
}
static bool trinamic_driver_config (motor_map_t motor, uint8_t seq)
{
bool ok = false;
#if TRINAMIC_ENABLE == 2209
ok = (stepper[motor.id] = TMC2209_AddMotor(motor, trinamic.driver[motor.axis].current, trinamic.driver[motor.axis].microsteps, trinamic.driver[motor.axis].r_sense)) != NULL;
#elif TRINAMIC_ENABLE == 2130
ok = (stepper[motor.id] = TMC2130_AddMotor(motor, trinamic.driver[motor.axis].current, trinamic.driver[motor.axis].microsteps, trinamic.driver[motor.axis].r_sense)) != NULL;
#elif TRINAMIC_ENABLE == 5160
ok = (stepper[motor.id] = TMC5160_AddMotor(motor, trinamic.driver[motor.axis].current, trinamic.driver[motor.axis].microsteps, trinamic.driver[motor.axis].r_sense)) != NULL;
#endif
if(!ok) {
protocol_enqueue_rt_command(pos_failed);
// 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:
#ifdef TMC_X_ADVANCED
TMC_X_ADVANCED(motor.id)
#endif
#if TRINAMIC_I2C && TMC_X_MONITOR
dgr_enable.reg.monitor.x = TMC_X_MONITOR;
#endif
break;
case Y_AXIS:
#ifdef TMC_Y_ADVANCED
TMC_Y_ADVANCED(motor.id)
#endif
#if TRINAMIC_I2C && TMC_Y_MONITOR
dgr_enable.reg.monitor.x = TMC_Y_MONITOR;
#endif
break;
case Z_AXIS:
#ifdef TMC_Z_ADVANCED
TMC_Z_ADVANCED(motor.id)
#endif
#if TRINAMIC_I2C && TMC_Z_MONITOR
dgr_enable.reg.monitor.x = TMC_Z_MONITOR;
#endif
break;
#ifdef A_AXIS
case A_AXIS:
#ifdef TMC_A_ADVANCED
TMC_A_ADVANCED(motor.id)
#endif
#if TRINAMIC_I2C && TMC_A_MONITOR
dgr_enable.reg.monitor.x = TMC_A_MONITOR;
#endif
break;
#endif
#ifdef B_AXIS
case B_AXIS:
#ifdef TMC_B_ADVANCED
TMC_B_ADVANCED(motor.id)
#endif
#if TRINAMIC_I2C && TMC_B_MONITOR
dgr_enable.reg.monitor.x = TMC_B_MONITOR;
#endif
break;
#endif
#ifdef C_AXIS
case C_AXIS:
#ifdef TMC_C_ADVANCED
TMC_C_ADVANCED(motor.id)
#endif
#if TRINAMIC_I2C && TMC_C_MONITOR
dgr_enable.reg.monitor.x = TMC_C_MONITOR;
#endif
break;
#endif
}
#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, trinamic.driver[motor.axis].current, trinamic.driver[motor.axis].hold_current_pct);
stepper[motor.id]->set_microsteps(motor.id, trinamic.driver[motor.axis].microsteps);
#if TRINAMIC_I2C
tmc_spi_write((trinamic_motor_t){0}, (TMC_spi_datagram_t *)&dgr_enable);
#endif
return true;
}
static void trinamic_drivers_init (axes_signals_t axes)
{
bool ok = axes.value != 0;
uint_fast8_t motor = n_motors, n_enabled = 0;
memset(stepper, 0, sizeof(stepper));
do {
if(bit_istrue(axes.mask, bit(motor_map[--motor].axis))) {
if((ok = trinamic_driver_config(motor_map[motor], motor)))
n_enabled++;
}
} while(ok && motor);
tmc_motors_set(ok ? n_enabled : 0);
if(!ok) {
driver_enabled.mask = 0;
memset(stepper, 0, sizeof(stepper));
}
}
// Add warning info to next realtime report when warning flag set by drivers
static void trinamic_realtime_report (stream_write_ptr stream_write, report_tracking_flags_t report)
{
if(warning) {
warning = false;
#if TRINAMIC_I2C
TMC_spi_status_t status = tmc_spi_read((trinamic_motor_t){0}, (TMC_spi_datagram_t *)&dgr_monitor);
otpw_triggered.mask |= dgr_monitor.reg.otpw.mask;
sprintf(sbuf, "|TMCMON:%d:%d:%d:%d:%d", status, dgr_monitor.reg.ot.mask, dgr_monitor.reg.otpw.mask, dgr_monitor.reg.otpw_cnt.mask, dgr_monitor.reg.error.mask);
stream_write(sbuf);
#endif
}
if(on_realtime_report)
on_realtime_report(stream_write, report);
}
// Return pointer to end of string
static char *append (char *s)
{
while(*s) s++;
return s;
}
// Write CRLF terminated string to current stream
static void write_line (char *s)
{
strcat(s, ASCII_EOL);
hal.stream.write(s);
}
//
static void report_sg_status (sys_state_t state)
{
hal.stream.write("[SG:");
hal.stream.write(uitoa(stepper[report.sg_status_motor]->get_sg_result(report.sg_status_motor)));
hal.stream.write("]" ASCII_EOL);
}
static void stepper_pulse_start (stepper_t *motors)
{
static uint32_t step_count = 0;
hal_stepper_pulse_start(motors);
if(motors->step_outbits.mask & report.sg_status_motormask.mask) {
uint32_t ms = hal.get_elapsed_ticks();
if(ms - step_count >= 20) {
step_count = ms;
protocol_enqueue_rt_command(report_sg_status);
}
/* step_count++;
if(step_count >= report.msteps * 4) {
step_count = 0;
protocol_enqueue_rt_command(report_sg_status);
} */
}
}
#if TRINAMIC_DEV
static void report_sg_params (void)
{
sprintf(sbuf, "[SGPARAMS:%ld:%d:%d:%d]" ASCII_EOL, report.sg_status_motor, stepper[report.sg_status_motor].coolconf.reg.sfilt, stepper[report.sg_status_motor].coolconf.reg.semin, stepper[report.sg_status_motor].coolconf.reg.semax);
hal.stream.write(sbuf);
}
#endif
static char *get_axisname (motor_map_t motor)
{
static char axisname[3] = {0};
axisname[0] = axis_letter[motor.axis][0];
axisname[1] = motor.id == motor.axis ? '\0' : '2';
return axisname;
}
// Validate M-code axis parameters
// Sets value to NAN (Not A Number) and returns false if driver not installed
static bool check_params (parser_block_t *gc_block)
{
static const parameter_words_t wordmap[] = {
{ .x = On },
{ .y = On },
{ .z = On }
#if N_AXIS > 3
, { .a = On },
{ .b = On },
{ .c = On }
#endif
};
uint_fast8_t n_found = 0, n_ok = 0, idx = N_AXIS;
do {
idx--;
if(gc_block->words.mask & wordmap[idx].mask) {
n_found++;
if(bit_istrue(driver_enabled.mask, bit(idx)) && !isnan(gc_block->values.xyz[idx])) {
n_ok++;
gc_block->words.mask &= ~wordmap[idx].mask;
}
} else
gc_block->values.xyz[idx] = NAN;
} while(idx);
return n_ok > 0 && n_ok == n_found;
}
#define Trinamic_StallGuardParams 123
#define Trinamic_WriteRegister 124
// Check if given M-code is handled here
static user_mcode_t trinamic_MCodeCheck (user_mcode_t mcode)
{
#if TRINAMIC_DEV
if(mcode == Trinamic_StallGuardParams || mcode == Trinamic_WriteRegister)
return mcode;
#endif
return mcode == Trinamic_DebugReport ||
(driver_enabled.mask && (mcode == Trinamic_StepperCurrent || mcode == Trinamic_ReportPrewarnFlags || mcode == Trinamic_ClearPrewarnFlags ||
mcode == Trinamic_HybridThreshold || mcode == Trinamic_HomingSensitivity))
? mcode
: (user_mcode.check ? user_mcode.check(mcode) : UserMCode_Ignore);
}
// Validate driver specific M-code parameters
static status_code_t trinamic_MCodeValidate (parser_block_t *gc_block, parameter_words_t *deprecated)
{
status_code_t state = Status_GcodeValueWordMissing;
switch(gc_block->user_mcode) {
#if TRINAMIC_DEV
case Trinamic_StallGuardParams:
state = Status_OK;
break;
case Trinamic_WriteRegister:
if((*parameter_words).r && (*parameter_words).q) {
if(isnan(gc_block->values.r) || isnan(gc_block->values.q))
state = Status_BadNumberFormat;
else {
reg_ptr = TMC5160_GetRegPtr(&stepper[report.sg_status_motor], (tmc5160_regaddr_t)gc_block->values.r);
state = reg_ptr == NULL ? Status_GcodeValueOutOfRange : Status_OK;
(*parameter_words).r = (*parameter_words).q = Off;
}
}
break;
#endif
case Trinamic_DebugReport:
state = Status_OK;
if(gc_block->words.h && gc_block->values.h > 1)
state = Status_BadNumberFormat;
if(gc_block->words.q && isnan(gc_block->values.q))
state = Status_BadNumberFormat;
if(gc_block->words.s && isnan(gc_block->values.s))
state = Status_BadNumberFormat;
gc_block->words.h = gc_block->words.i = gc_block->words.q = gc_block->words.s =
gc_block->words.x = gc_block->words.y = gc_block->words.z = Off;
#ifdef A_AXIS
gc_block->words.a = Off;
#endif
#ifdef B_AXIS
gc_block->words.b = Off;
#endif
#ifdef C_AXIS
gc_block->words.c = Off;
#endif
// gc_block->user_mcode_sync = true;
break;
case Trinamic_StepperCurrent:
if(check_params(gc_block)) {
state = Status_OK;
gc_block->user_mcode_sync = true;
if(!gc_block->words.q)
gc_block->values.q = NAN;
else // TODO: add range check?
gc_block->words.q = Off;
}
break;
case Trinamic_ReportPrewarnFlags:
case Trinamic_ClearPrewarnFlags:
state = Status_OK;
break;
case Trinamic_HybridThreshold:
if(check_params(gc_block)) {
state = Status_OK;
gc_block->user_mcode_sync = true;
}
break;
case Trinamic_HomingSensitivity:
if(check_params(gc_block)) {
uint_fast8_t idx = N_AXIS;
state = gc_block->words.i && (isnan(gc_block->values.ijk[0]) || gc_block->values.ijk[0] != 1.0f) ? Status_BadNumberFormat : Status_OK;
gc_block->words.i = Off;
if(state == Status_OK) do {
idx--;
#if TMC_STALLGUARD == 4
if(!isnan(gc_block->values.xyz[idx]) && (gc_block->values.xyz[idx] < 0.0f || gc_block->values.xyz[idx] > 255.0f))
state = Status_BadNumberFormat;
#else
if(!isnan(gc_block->values.xyz[idx]) && (gc_block->values.xyz[idx] < -64.0f || gc_block->values.xyz[idx] > 63.0f))
state = Status_BadNumberFormat;
#endif
} while(idx && state == Status_OK);
}
break;
default:
state = Status_Unhandled;
break;
}
return state == Status_Unhandled && user_mcode.validate ? user_mcode.validate(gc_block, deprecated) : state;
}
// Execute driver specific M-code
static void trinamic_MCodeExecute (uint_fast16_t state, parser_block_t *gc_block)
{
bool handled = true;
uint_fast8_t motor = n_motors;
switch(gc_block->user_mcode) {
#if TRINAMIC_DEV
case Trinamic_StallGuardParams:
report_sg_params();
break;
case Trinamic_WriteRegister:
reg_ptr->payload.value = (uint32_t)gc_block->values.q;
TMC5160_WriteRegister(&stepper[report.sg_status_motor], reg_ptr);
break;
#endif
case Trinamic_DebugReport:
{
if(driver_enabled.mask != trinamic.driver_enable.mask) {
pos_failed(state_get());
return;
}
if(!trinamic.driver_enable.mask) {
hal.stream.write("TMC driver(s) not enabled, enable with $338 setting." ASCII_EOL);
return;
}
axes_signals_t axes = {0};
bool write_report = !(gc_block->words.i || gc_block->words.s || gc_block->words.h || gc_block->words.q);
if(!write_report) {
if(gc_block->words.i)
trinamic_drivers_init(driver_enabled);
if(gc_block->words.h)
report.sfilt = gc_block->values.h != 0.0f;
if(gc_block->words.q)
report.raw = gc_block->values.q != 0.0f;
if(gc_block->words.s)
report.sg_status_enable = gc_block->values.s != 0.0f;
}
axes.x = gc_block->words.x;
axes.y = gc_block->words.y;
axes.z = gc_block->words.z;
#ifdef A_AXIS
axes.a = gc_block->words.a;
#endif
#ifdef B_AXIS
axes.b = gc_block->words.b;
#endif
#ifdef C_AXIS
axes.c = gc_block->words.c;
#endif
axes.mask &= driver_enabled.mask;
if(!write_report) {
motor = report.sg_status_motor;
if(trinamic.driver[motor].mode == TMCMode_StealthChop)
stepper[motor]->stealthchop_enable(motor);
else if(trinamic.driver[motor].mode == TMCMode_CoolStep)
stepper[motor]->coolstep_enable(motor);
if(axes.mask) {
uint32_t axis = 0, mask = axes.mask;
while(mask) {
if(mask & 0x01) {
report.sg_status_motor = axis;
break;
}
axis++;
mask >>= 1;
}
}
motor = report.sg_status_motor;
if(report.sg_status_enable) {
report.sg_status_motormask.mask = 1 << report.sg_status_motor;
report.msteps = trinamic.driver[report.sg_status_motor].microsteps;
if(hal_stepper_pulse_start == NULL) {
hal_stepper_pulse_start = hal.stepper.pulse_start;
hal.stepper.pulse_start = stepper_pulse_start;
}
stepper[motor]->stallguard_enable(motor, settings.homing.feed_rate, settings.axis[motor].steps_per_mm, trinamic.driver[motor].homing_sensitivity);
stepper[motor]->sg_filter(motor, report.sfilt);
if(stepper[motor]->set_thigh_raw) // TODO: TMC2209 do not have this...
stepper[motor]->set_thigh_raw(motor, 0);
stepper[motor]->set_tcoolthrs_raw(motor, 0xFFFFF);
} else if(hal_stepper_pulse_start != NULL) {
hal.stepper.pulse_start = hal_stepper_pulse_start;
hal_stepper_pulse_start = NULL;
}
} else
write_debug_report(axes.mask ? axes.mask : driver_enabled.mask);
}
break;
case Trinamic_StepperCurrent:
do {
if(!isnan(gc_block->values.xyz[motor_map[--motor].axis]))
stepper[motor]->set_current(motor, (uint16_t)gc_block->values.xyz[motor_map[motor].axis],
isnan(gc_block->values.q) ? trinamic.driver[motor_map[motor].axis].hold_current_pct : (uint8_t)gc_block->values.q);
} while(motor);
break;
case Trinamic_ReportPrewarnFlags:
{
TMC_drv_status_t status;
strcpy(sbuf, "[TMCPREWARN:");
for(motor = 0; motor < n_motors; motor++) {
if(bit_istrue(driver_enabled.mask, bit(motor_map[motor].axis))) {
status = stepper[motor]->get_drv_status(motor);
strcat(sbuf, "|");
strcat(sbuf, get_axisname(motor_map[motor]));
strcat(sbuf, ":");
if(status.driver_error)
strcat(sbuf, "E");
else if(status.ot)
strcat(sbuf, "O");
else if(status.otpw)
strcat(sbuf, "W");
}
}
hal.stream.write(sbuf);
hal.stream.write("]" ASCII_EOL);
}
break;
case Trinamic_ClearPrewarnFlags:
otpw_triggered.mask = 0;
#if TRINAMIC_DEV
stallGuard_enable(report.sg_status_motor, true); // TODO: (re)move this...
#endif
break;
case Trinamic_HybridThreshold:
{
uint_fast8_t axis;
do {
axis = motor_map[--motor].axis;
if(!isnan(gc_block->values.xyz[axis])) // mm/min
stepper[motor]->set_tpwmthrs(motor, gc_block->values.xyz[axis] / 60.0f, settings.axis[axis].steps_per_mm);
} while(motor);