forked from grblHAL/Plugins_motor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trinamic.c
2404 lines (2019 loc) · 84.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-2023 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/>.
TMC2660 modifications Copyright (C) Sienci Labs Inc.
This file is part of the SuperLongBoard family of products.
This source describes Open Hardware and is licensed under the "CERN-OHL-S v2"
You may redistribute and modify this source and make products using
it under the terms of the CERN-OHL-S v2 (https://ohwr.org/cern_ohl_s_v2.t).
This source is distributed WITHOUT ANY EXPRESS OR IMPLIED WARRANTY,
INCLUDING OF MERCHANTABILITY, SATISFACTORY QUALITY AND FITNESS FOR A
PARTICULAR PURPOSE. Please see the CERN-OHL-S v2 for applicable conditions.
As per CERN-OHL-S v2 section 4, should You produce hardware based on this
source, You must maintain the Source Location clearly visible on the external
case of the CNC Controller or other product you make using this source.
You should have received a copy of the CERN-OHL-S v2 license with this source.
If not, see <https://ohwr.org/project/cernohl/wikis/Documents/CERN-OHL-version-2>.
Contact for information regarding this program and its license
can be sent through [email protected] or mailed to the main office
of Sienci Labs Inc. in Waterloo, Ontario, Canada.
*/
#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};
#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, stst_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 on_execute_realtime_ptr on_execute_realtime, on_execute_delay = NULL;
#if BOARD_LONGBOARD32
#ifndef TRINAMIC_STATUS_DELAY
#define TRINAMIC_STATUS_DELAY 254
#define STST_REDUCTION 1
static on_state_change_ptr on_state_change;
static uint32_t last_ms = 0;
static stepper_t * st; //storage for stepper data
static segment_t * sg; //storage for segment data
#endif
static TMC_drv_status_t status[5];
static stepper_enable_ptr stepper_enable = NULL;
static stepper_wake_up_ptr stepper_wakeup = NULL;
static driver_reset_ptr driver_reset = NULL;
static settings_t *driver_settings;
static sys_state_t current_state; // For storing the current state from sys.state via state_get()
#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 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_stepper_reset(void);
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
#if (0)
static status_code_t set_tmc2660_setting (setting_id_t setting, uint_fast16_t value);
static uint32_t get_tmc2660_setting (setting_id_t setting);
#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", NULL, NULL, 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
#if (BOARD_LONGBOARD32)
{ Setting_SLB32_TMC2660_toff, Group_MotorDriver, "Chopper toff", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.toff, NULL, NULL },
{ Setting_SLB32_TMC2660_tbl, Group_MotorDriver, "Chopper tbl", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.tbl, NULL, NULL },
{ Setting_SLB32_TMC2660_chm, Group_MotorDriver, "Chopper chm", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.chm, NULL, NULL },
{ Setting_SLB32_TMC2660_hstr, Group_MotorDriver, "Chopper hstr", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.hstr, NULL, NULL },
{ Setting_SLB32_TMC2660_hend, Group_MotorDriver, "Chopper hend", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.hend, NULL, NULL },
{ Setting_SLB32_TMC2660_hdec, Group_MotorDriver, "Chopper hdec", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.hdec, NULL, NULL },
{ Setting_SLB32_TMC2660_rndtf, Group_MotorDriver, "Chopper rndtf", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.rndtf, NULL, NULL },
{ Setting_SLB32_TMC2660_THRESH, Group_MotorDriver, "THRESH", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.thresh, NULL, NULL },
{ Setting_SLB32_TMC2660_semin, Group_MotorDriver, "CoolStep semin", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.semin, NULL, NULL },
{ Setting_SLB32_TMC2660_seup, Group_MotorDriver, "CoolStep seup", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.seup, NULL, NULL },
{ Setting_SLB32_TMC2660_semax, Group_MotorDriver, "CoolStep semax", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.semax, NULL, NULL },
{ Setting_SLB32_TMC2660_sedn, Group_MotorDriver, "CoolStep sedn", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.sedn, NULL, NULL },
{ Setting_SLB32_TMC2660_seimin, Group_MotorDriver, "CoolStep seimin", NULL, Format_Int8, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.seimin, NULL, NULL },
{ Setting_SLB32_TMC2660_drvconf, Group_MotorDriver, "drvconf_reg", NULL, Format_Integer, "###0", NULL, NULL, Setting_NonCore, &trinamic.tmc2660_settings.drvconf, NULL, 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 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."
},
#if (BOARD_LONGBOARD32)
{ Setting_SLB32_TMC2660_toff, "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_SLB32_TMC2660_tbl, "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_SLB32_TMC2660_chm, "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_SLB32_TMC2660_hstr, "CHM=0: Hysteresis start, offset from HEND (0-7 = 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_SLB32_TMC2660_hend, "Can be either negative, zero, or positive, 0-15 = -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_SLB32_TMC2660_hdec, "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_SLB32_TMC2660_rndtf, "Change from fixed to randomized TOFF times, by dNCLK= -24 to +6 clocks." },
{ Setting_SLB32_TMC2660_THRESH, "StallGuard threshold." },
{ Setting_SLB32_TMC2660_semin, "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_SLB32_TMC2660_seup, "Number of increments of the coil current each time SG is sampled below the lower threshold (0-3 = 1,2,4,8)." },
{ Setting_SLB32_TMC2660_semax, "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_SLB32_TMC2660_sedn, "Number of times SG must be sampled above the upper threshold before the coil current is decremented (0-3 = 32,8,2,1)." },
{ Setting_SLB32_TMC2660_seimin, "Minimum CoolStep current as a factor of the set motor current\\n"
"0: 1/2, 1: 1/4" },
{ Setting_SLB32_TMC2660_drvconf, "DRVCONF register defaults 0xA31F. All protections enabled." },
#endif
};
#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 settings_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 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]->get_config){
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]->get_config){
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]->get_config){
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;
}
// 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;
#if (BOARD_LONGBOARD32)
trinamic.tmc2660_settings.toff = TMC2660_CONSTANT_OFF_TIME;
trinamic.tmc2660_settings.tbl = TMC2660_BLANK_TIME;
trinamic.tmc2660_settings.chm = TMC2660_CHOPPER_MODE;
trinamic.tmc2660_settings.hstr = TMC2660_HSTR;
trinamic.tmc2660_settings.hend = TMC2660_HEND;
trinamic.tmc2660_settings.hdec = TMC2660_HDEC;
trinamic.tmc2660_settings.rndtf = TMC2660_RNDTF;
trinamic.tmc2660_settings.thresh = TMC2660_SG_THRESH;
trinamic.tmc2660_settings.semin = TMC2660_SEMIN;
trinamic.tmc2660_settings.seup = TMC2660_SEUP;
trinamic.tmc2660_settings.semax = TMC2660_SEMAX;
trinamic.tmc2660_settings.sedn = TMC2660_SEDN;
trinamic.tmc2660_settings.seimin = TMC2660_SEIMIN;
trinamic.tmc2660_settings.drvconf = TMC2660_DRVCONF;
//idx = 3;
#endif
do {
switch(--idx) {
case X_AXIS:
#if TMC_X_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
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_seek_sensitivity = TMC_X_HOMING_SEEK_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_X_HOMING_FEED_SGT;
break;
case Y_AXIS:
#if TMC_Y_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
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_seek_sensitivity = TMC_Y_HOMING_SEEK_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_Y_HOMING_FEED_SGT;
break;
case Z_AXIS:
#if TMC_Z_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
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_seek_sensitivity = TMC_Z_HOMING_SEEK_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_Z_HOMING_FEED_SGT;
break;
#ifdef A_AXIS
case A_AXIS:
#if TMC_A_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
trinamic.driver_enable.a = 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_seek_sensitivity = TMC_A_HOMING_SEEK_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_A_HOMING_FEED_SGT;
break;
#endif
#ifdef B_AXIS
case B_AXIS:
#if TMC_B_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
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_seek_sensitivity = TMC_B_HOMING_SEEK_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_B_HOMING_FEED_SGT;
break;
#endif
#ifdef C_AXIS
case C_AXIS:
#if TMC_C_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
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_seek_sensitivity = TMC_C_HOMING_SEEK_SGT;
trinamic.driver[idx].homing_feed_sensitivity = TMC_C_HOMING_FEED_SGT;
break;
#endif
}
trinamic.driver[idx].homing_seek_rate = DEFAULT_HOMING_SEEK_RATE;
trinamic.driver[idx].homing_feed_rate = DEFAULT_HOMING_FEED_RATE;
} 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_seek_sensitivity < 0)
trinamic.driver[idx].homing_seek_sensitivity = 0;
if(trinamic.driver[idx].homing_feed_sensitivity < 0)
trinamic.driver[idx].homing_feed_sensitivity = 0;
#else
if(trinamic.driver[--idx].homing_seek_sensitivity > 64)
trinamic.driver[idx].homing_seek_sensitivity = 0;
if(trinamic.driver[idx].homing_feed_sensitivity > 64)
trinamic.driver[idx].homing_feed_sensitivity = 0;
#endif
// Until $-setting is added set from mode from defines
switch(idx) {
case X_AXIS:
#if TMC_X_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
break;
case Y_AXIS:
#if TMC_Y_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
break;
case Z_AXIS:
#if TMC_Z_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
break;
#ifdef A_AXIS
case A_AXIS:
#if TMC_A_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
break;
#endif
#ifdef B_AXIS
case B_AXIS:
#if TMC_B_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
break;
#endif
#ifdef C_AXIS
case C_AXIS:
#if TMC_C_STEALTHCHOP
trinamic.driver[idx].mode = TMCMode_StealthChop;
#else
trinamic.driver[idx].mode = TMCMode_CoolStep;
#endif
break;
#endif
}
//
} while(idx);
}
#if !TRINAMIC_MIXED_DRIVERS
trinamic.driver_enable.mask = AXES_BITMASK;
#endif
settings_loaded = true;
}
#endif // End region settings
static void pos_failed (sys_state_t state)
{
report_message("Could not communicate with stepper driver!", Message_Warning);
}
#if BOARD_LONGBOARD32
static void TMC2660onStateChanged (sys_state_t state)
{
static sys_state_t previous_state;
uint_fast8_t motor = 0;
if(previous_state == STATE_ALARM){ //if coming out of alarm state ensure drivers are programmed.
trinamic_stepper_reset();
while(motor<n_motors) {
if(stepper[motor]->update_settings)
stepper[motor]->update_settings(motor, &trinamic.tmc2660_settings);
motor++;
}
}
previous_state = state;
if (on_state_change) // Call previous function in the chain.
on_state_change(state);
}
static void poll_report (sys_state_t state)
{
uint_fast8_t motor = 0;
uint_fast16_t axis;
while(motor<n_motors) {
if(status[motor].stst){
if(stepper[motor]){
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);
hal.stream.write("[STCR:");
hal.stream.write(uitoa((trinamic.driver[axis].current * trinamic.driver[axis].hold_current_pct)/100));
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);
}
motor++;
}
}
static void trinamic_poll (void)
{
static uint_fast16_t error_count = 0;
uint_fast8_t motor = 0;
uint_fast16_t axis;
uint32_t ms = hal.get_elapsed_ticks();
uint8_t stall_fault, otpw_fault;
static bool error_active = false;
control_signals_t ctrl_pin_state = hal.control.get_state();
if(ms < last_ms + TRINAMIC_STATUS_DELAY) // check once every update period
return;
current_state = state_get();
if(current_state == STATE_IDLE){
while(motor<n_motors) {
if(stepper[motor]->get_drv_status){
status[motor] = stepper[motor]->get_drv_status(motor);
}
motor++;
}
}
//check overtemp
motor = 0;
otpw_fault = 0;
while(motor<n_motors) {
if(status[motor].otpw){
otpw_fault |= (1 << motor);
}
motor++;
}
//check stallguard
stall_fault = 0;
/*motor = 0;
while(motor<n_motors) {
if(status[motor].stallguard){
//stall_fault |= (1 << motor);
}
motor++;
}*/
if (stall_fault || otpw_fault){
error_count++;
if(error_count > 2){
error_active = true;
grbl.enqueue_realtime_command(CMD_STOP);
grbl.enqueue_realtime_command(CMD_FEED_HOLD);
//settings.steppers.deenergize.value = 0;
st_go_idle();
protocol_enqueue_rt_command(poll_report);
}
} else{
error_count = 0;
}
//check STST
#if (DYNAMIC_STST)
motor = 0;
if((current_state == STATE_IDLE) || (current_state == STATE_ALARM)){
//if(((current_state == STATE_IDLE) || (current_state == STATE_ALARM)) && !(ctrl_pin_state.e_stop || ctrl_pin_state.reset)){//do not try to write to the motor if reset or estop asserted.
while(motor<n_motors) {
if(stepper[motor]){
axis = motor_map[motor].axis;
if(trinamic.driver[axis].hold_current_pct != 100){
if( stepper[motor]->set_current){
stepper[motor]->set_current(motor, (trinamic.driver[axis].current * trinamic.driver[axis].hold_current_pct)/100, trinamic.driver[axis].hold_current_pct);
}
}
}
motor++;
}
}
#endif
//error has been recovered
if (error_active){
if((stall_fault==0) && (otpw_fault==0)){
error_active = false;
error_count = 0;
//need to recover engerize state.
}
}
//protocol_enqueue_rt_command(poll_report);
last_ms = ms;
}
static void trinamic_poll_realtime (sys_state_t grbl_state)
{
on_execute_realtime(grbl_state);
trinamic_poll();
}
static void trinamic_poll_delay (sys_state_t grbl_state)
{
on_execute_delay(grbl_state);
trinamic_poll();
}
static void set_stst_for_block(sys_state_t grbl_state){
uint_fast8_t motor = 0;
uint_fast16_t axis = 0;
while(motor<n_motors) {
axis = motor_map[motor].axis;
#if 0
if(st->steps[axis]){
if(stepper[motor]->set_current){
stepper[motor]->set_current(motor, trinamic.driver[axis].current, trinamic.driver[axis].hold_current_pct);
}
} else{
if(trinamic.driver[axis].hold_current_pct != 100){
if(stepper[motor]->set_current){
stepper[motor]->set_current(motor, (trinamic.driver[axis].current * trinamic.driver[axis].hold_current_pct)/100, trinamic.driver[axis].hold_current_pct);
}
}
}
#else
if(sg->next->exec_block->steps[axis]){//if motors are used in an upcoming segment, need to power them up
if(stepper[motor]->set_current){
stepper[motor]->set_current(motor, trinamic.driver[axis].current, trinamic.driver[axis].hold_current_pct);
}
}else if (st->steps[axis]){//if motors are used in current segment, need to ensure they are powered up.
if(stepper[motor]->set_current){
stepper[motor]->set_current(motor, trinamic.driver[axis].current, trinamic.driver[axis].hold_current_pct);
}
} else { //else they can be put on standby.
if(stepper[motor]->set_current){
stepper[motor]->set_current(motor, (trinamic.driver[axis].current * trinamic.driver[axis].hold_current_pct)/100, trinamic.driver[axis].hold_current_pct);
}
}
#endif
motor++;
}
motor = 0;
while(motor<n_motors) {
if(stepper[motor]->get_drv_status){
status[motor] = stepper[motor]->get_drv_status(motor);
}
motor++;
}
//last_ms = hal.get_elapsed_ticks();
}
static void set_stst_for_homing(){
uint_fast8_t motor = 0;
uint_fast16_t axis = 0;
while(motor<n_motors) {
axis = motor_map[motor].axis;
if(stepper[motor]->set_current){
stepper[motor]->set_current(motor, trinamic.driver[axis].current, trinamic.driver[axis].hold_current_pct);
}
motor++;
}
last_ms = hal.get_elapsed_ticks() + 10000;
}
static void stst_pulse_start (stepper_t *motors)
{
#if(DYNAMIC_STST)
if (motors->new_block){
st = motors; //reference the current block
sg = motors->exec_segment;
protocol_enqueue_rt_command(set_stst_for_block); //enqueue the command to set currents.
}
#endif
//check stallguard pins
stst_stepper_pulse_start(motors);
}
#endif //longboard specific code
static void on_settings_changed (settings_t *settings, settings_changed_flags_t changed)
{
static bool init_ok = false;
static float steps_per_mm[N_AXIS];
uint_fast8_t idx = N_AXIS;
settings_changed(settings, changed);
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);
}
#if BOARD_LONGBOARD32
//pulse start pointer gets reset on settings change, need to restore.
#if (STST_REDUCTION)
if(hal.stepper.pulse_start == stst_stepper_pulse_start) {
stst_stepper_pulse_start = hal.stepper.pulse_start;
hal.stepper.pulse_start = stst_pulse_start;
}
#endif
uint_fast8_t motor = 0;
while(motor<n_motors) {
if(stepper[motor]->update_settings)
stepper[motor]->update_settings(motor, &trinamic.tmc2660_settings);
motor++;
}
#endif
}
#if (BOARD_LONGBOARD32)
static bool non_trinamic_driver_config (motor_map_t motor){
bool ok = false;
ok = (stepper[motor.id] = TMC2660_AddNULLMotor(motor)) != NULL;
return ok;
}
#endif
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]
};