-
Notifications
You must be signed in to change notification settings - Fork 39
/
driver.c
2705 lines (2227 loc) · 79.2 KB
/
driver.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
/*
driver.c - driver code for IMXRT1062 processor (on Teensy 4.0/4.1 board)
Part of grblHAL
Copyright (c) 2020-2022 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/>.
*/
// shut up compiler warning...
#pragma GCC diagnostic ignored "-Wunused-function"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "uart.h"
#include "driver.h"
#include "grbl/protocol.h"
#include "grbl/limits.h"
#include "grbl/state_machine.h"
#ifdef I2C_PORT
#include "i2c.h"
#endif
#if EEPROM_ENABLE
#include "eeprom/eeprom.h"
#else
#include "avr/eeprom.h"
#endif
#if IOPORTS_ENABLE
#include "ioports.h"
#endif
#if KEYPAD_ENABLE == 2
#include "keypad/keypad.h"
#endif
#if SDCARD_ENABLE
#include "uSDFS.h"
#include "sdcard/sdcard.h"
#endif
#if PPI_ENABLE
#include "laser/ppi.h"
static void ppi_timeout_isr (void);
#endif
#if ETHERNET_ENABLE
#include "enet.h"
#endif
#if USB_SERIAL_CDC == 1
#include "usb_serial_ard.h"
#elif USB_SERIAL_CDC == 2
#include "usb_serial_pjrc.h"
#endif
#define DEBOUNCE_QUEUE 8 // Must be a power of 2
#define F_BUS_MHZ (F_BUS_ACTUAL / 1000000)
#include "grbl/motor_pins.h"
typedef struct {
volatile uint_fast8_t head;
volatile uint_fast8_t tail;
input_signal_t *signal[DEBOUNCE_QUEUE];
} debounce_queue_t;
#if QEI_ENABLE
#define QEI_DEBOUNCE 3
#define QEI_VELOCITY_TIMEOUT 100
typedef union {
uint_fast8_t pins;
struct {
uint_fast8_t a :1,
b :1;
};
} qei_state_t;
typedef struct {
encoder_t encoder;
int32_t count;
int32_t vel_count;
uint_fast16_t state;
volatile uint32_t dbl_click_timeout;
volatile uint32_t vel_timeout;
uint32_t vel_timestamp;
} qei_t;
static qei_t qei = {0};
#endif
static debounce_queue_t debounce_queue = {0};
// Standard inputs
static gpio_t Reset, FeedHold, CycleStart, Probe, LimitX, LimitY, LimitZ;
// Standard outputs
static gpio_t Mist, Flood, stepX, stepY, stepZ, dirX, dirY, dirZ;
#if (!VFD_SPINDLE || N_SPINDLE > 1) && defined(SPINDLE_ENABLE_PIN)
#define PWM_SPINDLE
#if PLASMA_ENABLE && defined(SPINDLE_PWM_PIN)
#undef SPINDLE_PWM_PIN
#endif
static gpio_t spindleEnable, spindleDir;
static bool pwmEnabled = false;
static spindle_pwm_t spindle_pwm;
static void spindle_set_speed (uint_fast16_t pwm_value);
#endif
// Optional I/O
#ifdef SAFETY_DOOR_PIN
static gpio_t SafetyDoor;
#endif
#ifdef LIMITS_OVERRIDE_PIN
static gpio_t LimitsOverride;
#endif
#ifdef A_AXIS
static gpio_t stepA, dirA, LimitA;
#ifdef A_ENABLE_PIN
static gpio_t enableA;
#endif
#endif
#ifdef B_AXIS
static gpio_t stepB, dirB, LimitB;
#ifdef B_ENABLE_PIN
static gpio_t enableB;
#endif
#endif
#ifdef C_AXIS
static gpio_t stepC, dirC;
#ifdef C_ENABLE_PIN
static gpio_t enableC;
#endif
#ifdef C_LIMIT_PIN
static gpio_t LimitC;
#endif
#endif
#ifdef STEPPERS_ENABLE_PIN
static gpio_t steppersEnable;
#endif
#ifdef X_ENABLE_PIN
static gpio_t enableX;
#endif
#ifdef Y_ENABLE_PIN
static gpio_t enableY;
#endif
#ifdef Z_ENABLE_PIN
static gpio_t enableZ;
#endif
#if I2C_STROBE_ENABLE
static gpio_t KeypadStrobe;
#endif
#if MPG_MODE == 1
static gpio_t ModeSelect;
static input_signal_t *mpg_pin = NULL;
#endif
#if QEI_ENABLE
static bool qei_enable = false;
static gpio_t QEI_A, QEI_B;
#ifdef QEI_SELECT_PIN
#define QEI_SELECT_ENABLED 1
static gpio_t QEI_Select;
#endif
#ifdef QEI_INDEX_PIN
#define QEI_INDEX_ENABLED 1
static gpio_t QEI_Index;
#endif
#endif
#ifdef X2_STEP_PIN
static gpio_t stepX2;
#endif
#ifdef X2_DIRECTION_PIN
static gpio_t dirX2;
#endif
#ifdef X2_ENABLE_PIN
static gpio_t enableX2;
#endif
#ifdef X2_LIMIT_PIN
static gpio_t LimitX2;
#endif
#ifdef X_LIMIT_PIN_MAX
static gpio_t LimitXMax;
#endif
#ifdef Y2_STEP_PIN
static gpio_t stepY2;
#endif
#ifdef Y2_DIRECTION_PIN
static gpio_t dirY2;
#endif
#ifdef Y2_ENABLE_PIN
static gpio_t enableY2;
#endif
#ifdef Y2_LIMIT_PIN
static gpio_t LimitY2;
#endif
#ifdef Y_LIMIT_PIN_MAX
static gpio_t LimitYMax;
#endif
#ifdef Z2_STEP_PIN
static gpio_t stepZ2;
#endif
#ifdef Z2_DIRECTION_PIN
static gpio_t dirZ2;
#endif
#ifdef Z2_ENABLE_PIN
static gpio_t enableZ2;
#endif
#ifdef Z2_LIMIT_PIN
static gpio_t LimitZ2;
#endif
#ifdef Z_LIMIT_PIN_MAX
static gpio_t LimitZMax;
#endif
#ifdef SPINDLE_INDEX_PIN
static gpio_t SpindleIndex;
#endif
#ifdef AUXINPUT0_PIN
static gpio_t AuxIn0;
#endif
#ifdef AUXINPUT1_PIN
static gpio_t AuxIn1;
#endif
#ifdef AUXINPUT2_PIN
static gpio_t AuxIn2;
#endif
#ifdef AUXINPUT3_PIN
static gpio_t AuxIn3;
#endif
#ifdef AUXINPUT4_PIN
static gpio_t AuxIn4;
#endif
#ifdef AUXINPUT5_PIN
static gpio_t AuxIn5;
#endif
#ifdef AUXINPUT6_PIN
static gpio_t AuxIn6;
#endif
#ifdef AUXINPUT7_PIN
static gpio_t AuxIn7;
#endif
#ifdef AUXOUTPUT0_PIN
static gpio_t AuxOut0;
#endif
#ifdef AUXOUTPUT1_PIN
static gpio_t AuxOut1;
#endif
#ifdef AUXOUTPUT2_PIN
static gpio_t AuxOut2;
#endif
#ifdef AUXOUTPUT3_PIN
static gpio_t AuxOut3;
#endif
#ifdef AUXOUTPUT4_PIN
static gpio_t AuxOut4;
#endif
#ifdef AUXOUTPUT5_PIN
static gpio_t AuxOut5;
#endif
#ifdef AUXOUTPUT6_PIN
static gpio_t AuxOut6;
#endif
#ifdef AUXOUTPUT7_PIN
static gpio_t AuxOut7;
#endif
static periph_signal_t *periph_pins = NULL;
input_signal_t inputpin[] = {
#if ESTOP_ENABLE
{ .id = Input_EStop, .port = &Reset, .pin = RESET_PIN, .group = PinGroup_Control },
#else
{ .id = Input_Reset, .port = &Reset, .pin = RESET_PIN, .group = PinGroup_Control },
#endif
{ .id = Input_FeedHold, .port = &FeedHold, .pin = FEED_HOLD_PIN, .group = PinGroup_Control },
{ .id = Input_CycleStart, .port = &CycleStart, .pin = CYCLE_START_PIN, .group = PinGroup_Control },
#ifdef SAFETY_DOOR_PIN
{ .id = Input_SafetyDoor, .port = &SafetyDoor, .pin = SAFETY_DOOR_PIN, .group = PinGroup_Control },
#endif
#if defined(LIMITS_OVERRIDE_PIN)
{ .id = Input_LimitsOverride, .port = &LimitsOverride, .pin = LIMITS_OVERRIDE_PIN, .group = PinGroup_Control },
#endif
#ifdef MPG_MODE_PIN
{ .id = Input_MPGSelect, .port = &ModeSelect, .pin = MPG_MODE_PIN, .group = PinGroup_MPG },
#endif
{ .id = Input_Probe, .port = &Probe, .pin = PROBE_PIN, .group = PinGroup_Probe },
// Limit input pins must be consecutive
{ .id = Input_LimitX, .port = &LimitX, .pin = X_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef X2_LIMIT_PIN
{ .id = Input_LimitX_2, .port = &LimitX2, .pin = X2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef X_LIMIT_PIN_MAX
{ .id = Input_LimitX_Max, .port = &LimitXMax, .pin = X_LIMIT_PIN_MAX, .group = PinGroup_Limit },
#endif
{ .id = Input_LimitY, .port = &LimitY, .pin = Y_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef Y2_LIMIT_PIN
{ .id = Input_LimitY_2, .port = &LimitY2, .pin = Y2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef Y_LIMIT_PIN_MAX
{ .id = Input_LimitY_Max, .port = &LimitYMax, .pin = Y_LIMIT_PIN_MAX, .group = PinGroup_Limit },
#endif
{ .id = Input_LimitZ, .port = &LimitZ, .pin = Z_LIMIT_PIN, .group = PinGroup_Limit }
#ifdef Z2_LIMIT_PIN
, { .id = Input_LimitZ_2, .port = &LimitZ2, .pin = Z2_LIMIT_PIN, .group = PinGroup_Limit }
#endif
#ifdef Z_LIMIT_PIN_MAX
, { .id = Input_LimitZ_Max, .port = &LimitZMax, .pin = Z_LIMIT_PIN_MAX, .group = PinGroup_Limit }
#endif
#ifdef A_LIMIT_PIN
, { .id = Input_LimitA, .port = &LimitA, .pin = A_LIMIT_PIN, .group = PinGroup_Limit }
#endif
#ifdef B_LIMIT_PIN
, { .id = Input_LimitB, .port = &LimitB, .pin = B_LIMIT_PIN, .group = PinGroup_Limit }
#endif
#ifdef C_LIMIT_PIN
, { .id = Input_LimitC, .port = &LimitC, .pin = C_LIMIT_PIN, .group = PinGroup_Limit }
#endif
// End limit pin definitions
#if MPG_MODE_PIN
, { .id = Input_ModeSelect, .port = &ModeSelect, .pin = MPG_MODE_PIN, .group = PinGroup_MPG }
#endif
#if I2C_STROBE_ENABLE && defined(I2C_STROBE_PIN)
, { .id = Input_KeypadStrobe, .port = &KeypadStrobe, .pin = I2C_STROBE_PIN, .group = PinGroup_Keypad }
#endif
#ifdef SPINDLE_INDEX_PIN
, { .id = Input_SpindleIndex, .port = &SpindleIndex, .pin = SPINDLE_INDEX_PIN, .group = PinGroup_SpindleIndex }
#endif
#if QEI_ENABLE
, { .id = Input_QEI_A, .port = &QEI_A, .pin = QEI_A_PIN, .group = PinGroup_QEI }
, { .id = Input_QEI_B, .port = &QEI_B, .pin = QEI_B_PIN, .group = PinGroup_QEI }
#if QEI_SELECT_ENABLED
, { .id = Input_QEI_Select, .port = &QEI_Select, .pin = QEI_SELECT_PIN, .group = PinGroup_QEI_Select }
#endif
#if QEI_INDEX_ENABLED
, { .id = Input_QEI_Index, .port = &QEI_Index, .pin = QEI_INDEX_PIN, .group = PinGroup_QEI }
#endif
#endif
// Aux input pins must be consecutive
#ifdef AUXINPUT0_PIN
, { .id = Input_Aux0, .port = &AuxIn0, .pin = AUXINPUT0_PIN, .group = PinGroup_AuxInput }
#endif
#ifdef AUXINPUT1_PIN
, { .id = Input_Aux1, .port = &AuxIn1, .pin = AUXINPUT1_PIN, .group = PinGroup_AuxInput }
#endif
#ifdef AUXINPUT2_PIN
, { .id = Input_Aux2, .port = &AuxIn2, .pin = AUXINPUT2_PIN, .group = PinGroup_AuxInput }
#endif
#ifdef AUXINPUT3_PIN
, { .id = Input_Aux3, .port = &AuxIn3, .pin = AUXINPUT3_PIN, .group = PinGroup_AuxInput }
#endif
#ifdef AUXINPUT4_PIN
, { .id = Input_Aux4, .port = &AuxIn4, .pin = AUXINPUT4_PIN, .group = PinGroup_AuxInput }
#endif
#ifdef AUXINPUT5_PIN
, { .id = Input_Aux5, .port = &AuxIn5, .pin = AUXINPUT5_PIN, .group = PinGroup_AuxInput }
#endif
#ifdef AUXINPUT6_PIN
, { .id = Input_Aux6, .port = &AuxIn6, .pin = AUXINPUT6_PIN, .group = PinGroup_AuxInput }
#endif
#ifdef AUXINPUT7_PIN
, { .id = Input_Aux7, .port = &AuxIn7, .pin = AUXINPUT7_PIN, .group = PinGroup_AuxInput }
#endif
};
static output_signal_t outputpin[] = {
{ .id = Output_StepX, .port = &stepX, .pin = X_STEP_PIN, .group = PinGroup_StepperStep },
{ .id = Output_StepY, .port = &stepY, .pin = Y_STEP_PIN, .group = PinGroup_StepperStep },
{ .id = Output_StepZ, .port = &stepZ, .pin = Z_STEP_PIN, .group = PinGroup_StepperStep },
#ifdef A_AXIS
{ .id = Output_StepA, .port = &stepA, .pin = A_STEP_PIN, .group = PinGroup_StepperStep },
#endif
#ifdef B_AXIS
{ .id = Output_StepB, .port = &stepB, .pin = B_STEP_PIN, .group = PinGroup_StepperStep },
#endif
#ifdef C_AXIS
{ .id = Output_StepC, .port = &stepC, .pin = C_STEP_PIN, .group = PinGroup_StepperStep },
#endif
#ifdef X2_STEP_PIN
{ .id = Output_StepX_2, .port = &stepX2, .pin = X2_STEP_PIN, .group = PinGroup_StepperStep },
#endif
#ifdef Y2_STEP_PIN
{ .id = Output_StepY_2, .port = &stepY2, .pin = Y2_STEP_PIN, .group = PinGroup_StepperStep },
#endif
#ifdef Z2_STEP_PIN
{ .id = Output_StepZ_2, .port = &stepZ2, .pin = Z2_STEP_PIN, .group = PinGroup_StepperStep },
#endif
{ .id = Output_DirX, .port = &dirX, .pin = X_DIRECTION_PIN, .group = PinGroup_StepperDir },
{ .id = Output_DirY, .port = &dirY, .pin = Y_DIRECTION_PIN, .group = PinGroup_StepperDir },
{ .id = Output_DirZ, .port = &dirZ, .pin = Z_DIRECTION_PIN, .group = PinGroup_StepperDir },
#ifdef A_AXIS
{ .id = Output_DirA, .port = &dirA, .pin = A_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#ifdef B_AXIS
{ .id = Output_DirB, .port = &dirB, .pin = B_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#ifdef C_AXIS
{ .id = Output_DirC, .port = &dirC, .pin = C_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#ifdef X2_DIRECTION_PIN
{ .id = Output_DirX_2, .port = &dirX2, .pin = X2_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#ifdef Y2_DIRECTION_PIN
{ .id = Output_DirY_2, .port = &dirY2, .pin = Y2_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#ifdef Z2_DIRECTION_PIN
{ .id = Output_DirZ_2, .port = &dirZ2, .pin = Z2_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#if !TRINAMIC_ENABLE
#ifdef STEPPERS_ENABLE_PIN
{ .id = Output_StepperEnable, .port = &steppersEnable, .pin = STEPPERS_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef X_ENABLE_PIN
{ .id = Output_StepperEnableX, .port = &enableX, .pin = X_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef Y_ENABLE_PIN
{ .id = Output_StepperEnableY, .port = &enableY, .pin = Y_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef Z_ENABLE_PIN
{ .id = Output_StepperEnableZ, .port = &enableZ, .pin = Z_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef A_ENABLE_PIN
{ .id = Output_StepperEnableA, .port = &enableA, .pin = A_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef B_ENABLE_PIN
{ .id = Output_StepperEnableB, .port = &enableB, .pin = B_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef C_ENABLE_PIN
{ .id = Output_StepperEnableC, .port = &enableC, .pin = C_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef X2_ENABLE_PIN
{ .id = Output_StepperEnableX, .port = &enableX2, .pin = X2_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef Y2_ENABLE_PIN
{ .id = Output_StepperEnableY, .port = &enableY2, .pin = Y2_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef Z2_ENABLE_PIN
{ .id = Output_StepperEnableZ, .port = &enableZ2, .pin = Z2_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#endif
#ifdef PWM_SPINDLE
{ .id = Output_SpindleOn, .port = &spindleEnable, .pin = SPINDLE_ENABLE_PIN, .group = PinGroup_SpindleControl },
#ifdef SPINDLE_DIRECTION_PIN
{ .id = Output_SpindleDir, .port = &spindleDir, .pin = SPINDLE_DIRECTION_PIN, .group = PinGroup_SpindleControl },
#endif
#endif // PWM_SPINDLE
{ .id = Output_CoolantFlood, .port = &Mist, .pin = COOLANT_FLOOD_PIN, .group = PinGroup_Coolant },
#ifdef COOLANT_MIST_PIN
{ .id = Output_CoolantMist, .port = &Flood, .pin = COOLANT_MIST_PIN, .group = PinGroup_Coolant },
#endif
#ifdef AUXOUTPUT0_PIN
{ .id = Output_Aux0, .port = &AuxOut0, .pin = AUXOUTPUT0_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT1_PIN
{ .id = Output_Aux1, .port = &AuxOut1, .pin = AUXOUTPUT1_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT2_PIN
{ .id = Output_Aux2, .port = &AuxOut2, .pin = AUXOUTPUT2_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT3_PIN
{ .id = Output_Aux3, .port = &AuxOut3, .pin = AUXOUTPUT3_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT4_PIN
{ .id = Output_Aux4, .port = &AuxOut4, .pin = AUXOUTPUT4_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT5_PIN
{ .id = Output_Aux5, .port = &AuxOut5, .pin = AUXOUTPUT5_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT6_PIN
{ .id = Output_Aux6, .port = &AuxOut6, .pin = AUXOUTPUT6_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT7_PIN
{ .id = Output_Aux7, .port = &AuxOut7, .pin = AUXOUTPUT7_PIN, .group = PinGroup_AuxOutput }
#endif
};
static pin_group_pins_t limit_inputs = {0};
#if QEI_ENABLE
#define ADD_MSEVENT 1
static volatile bool ms_event = false;
#else
#define ADD_MSEVENT 0
#endif
static bool IOInitDone = false;
static uint16_t pulse_length, pulse_delay;
static axes_signals_t next_step_outbits;
static delay_t grbl_delay = { .ms = 0, .callback = NULL };
static probe_state_t probe = {
.connected = On
};
#ifdef SQUARING_ENABLED
static axes_signals_t motors_1 = {AXES_BITMASK}, motors_2 = {AXES_BITMASK};
#endif
#if SPINDLE_SYNC_ENABLE
#include "grbl/spindle_sync.h"
static spindle_data_t spindle_data;
static spindle_encoder_t spindle_encoder = {
.tics_per_irq = 4
};
static spindle_sync_t spindle_tracker;
static volatile bool spindleLock = false;
static void stepperPulseStartSynchronized (stepper_t *stepper);
static void spindleDataReset (void);
static spindle_data_t *spindleGetData (spindle_data_request_t request);
static void spindle_pulse_isr (void);
#endif
#if I2C_STROBE_ENABLE
static driver_irq_handler_t i2c_strobe = { .type = IRQ_I2C_Strobe };
static bool irq_claim (irq_type_t irq, uint_fast8_t id, irq_callback_ptr handler)
{
bool ok;
if((ok = irq == IRQ_I2C_Strobe && i2c_strobe.callback == NULL))
i2c_strobe.callback = handler;
return ok;
}
#endif
// Interrupt handler prototypes
// Interrupt handlers needs to be registered, possibly by modifying a system specific startup file.
// It is possible to relocate the interrupt dispatch table from flash to RAM and programatically attach handlers.
// See the driver for SAMD21 for an example, relocation is done in the driver_init() function.
// Also, if a MCU specific driver library is used this might have functions to programatically attach handlers.
static void stepper_driver_isr (void);
static void stepper_pulse_isr (void);
static void stepper_pulse_isr_delayed (void);
static void gpio_isr (void);
static void debounce_isr (void);
static void systick_isr (void);
static void (*systick_isr_org)(void) = NULL;
// Millisecond resolution delay function
// Will return immediately if a callback function is provided
static void driver_delay_ms (uint32_t ms, delay_callback_ptr callback)
{
if(ms) {
grbl_delay.ms = ms;
if(!(grbl_delay.callback = callback)) {
while(grbl_delay.ms)
grbl.on_execute_delay(state_get());
}
} else {
if(grbl_delay.ms) {
grbl_delay.callback = NULL;
grbl_delay.ms = 1;
}
if(callback)
callback();
}
}
// Set stepper pulse output pins.
// step_outbits.value (or step_outbits.mask) are: bit0 -> X, bit1 -> Y...
// Individual step bits can be accessed by step_outbits.x, step_outbits.y, ...
#ifdef SQUARING_ENABLED
inline static __attribute__((always_inline)) void set_step_outputs (axes_signals_t step_outbits_1)
{
axes_signals_t step_outbits_2;
step_outbits_2.mask = (step_outbits_1.mask & motors_2.mask) ^ settings.steppers.step_invert.mask;
step_outbits_1.mask = (step_outbits_1.mask & motors_1.mask) ^ settings.steppers.step_invert.mask;
DIGITAL_OUT(stepX, step_outbits_1.x);
#ifdef X2_STEP_PIN
DIGITAL_OUT(stepX2, step_outbits_2.x);
#endif
DIGITAL_OUT(stepY, step_outbits_1.y);
#ifdef Y2_STEP_PIN
DIGITAL_OUT(stepY2, step_outbits_2.y);
#endif
DIGITAL_OUT(stepZ, step_outbits_1.z);
#ifdef Z2_STEP_PIN
DIGITAL_OUT(stepZ2, step_outbits_2.z);
#endif
#ifdef A_AXIS
DIGITAL_OUT(stepA, step_outbits_1.a);
#endif
#ifdef B_AXIS
DIGITAL_OUT(stepB, step_outbits_1.b);
#endif
}
// Enable/disable motors for auto squaring of ganged axes
static void StepperDisableMotors (axes_signals_t axes, squaring_mode_t mode)
{
motors_1.mask = (mode == SquaringMode_A || mode == SquaringMode_Both ? axes.mask : 0) ^ AXES_BITMASK;
motors_2.mask = (mode == SquaringMode_B || mode == SquaringMode_Both ? axes.mask : 0) ^ AXES_BITMASK;
}
#else
inline static __attribute__((always_inline)) void set_step_outputs (axes_signals_t step_outbits)
{
step_outbits.value ^= settings.steppers.step_invert.mask;
DIGITAL_OUT(stepX, step_outbits.x);
#ifdef X2_STEP_PIN
DIGITAL_OUT(stepX2, step_outbits.x);
#endif
DIGITAL_OUT(stepY, step_outbits.y);
#ifdef Y2_STEP_PIN
DIGITAL_OUT(stepY2, step_outbits.y);
#endif
DIGITAL_OUT(stepZ, step_outbits.z);
#ifdef Z2_STEP_PIN
DIGITAL_OUT(stepZ2, step_outbits.z);
#endif
#ifdef A_AXIS
DIGITAL_OUT(stepA, step_outbits.a);
#endif
#ifdef B_AXIS
DIGITAL_OUT(stepB, step_outbits.b);
#endif
#ifdef C_AXIS
DIGITAL_OUT(stepC, step_outbits.c);
#endif
}
#endif // SQUARING_ENABLED
#ifdef GANGING_ENABLED
static axes_signals_t getGangedAxes (bool auto_squared)
{
axes_signals_t ganged = {0};
if(auto_squared) {
#if X_AUTO_SQUARE
ganged.x = On;
#endif
#if Y_AUTO_SQUARE
ganged.y = On;
#endif
#if Z_AUTO_SQUARE
ganged.z = On;
#endif
} else {
#if X_GANGED
ganged.x = On;
#endif
#if Y_GANGED
ganged.y = On;
#endif
#if Z_GANGED
ganged.z = On;
#endif
}
return ganged;
}
#endif
// Set stepper direction ouput pins.
// dir_outbits.value (or dir_outbits.mask) are: bit0 -> X, bit1 -> Y...
// Individual direction bits can be accessed by dir_outbits.x, dir_outbits.y, ...
inline static __attribute__((always_inline)) void set_dir_outputs (axes_signals_t dir_outbits)
{
dir_outbits.value ^= settings.steppers.dir_invert.mask;
DIGITAL_OUT(dirX, dir_outbits.x);
DIGITAL_OUT(dirY, dir_outbits.y);
DIGITAL_OUT(dirZ, dir_outbits.z);
#ifdef GANGING_ENABLED
dir_outbits.mask ^= settings.steppers.ganged_dir_invert.mask;
#ifdef X2_DIRECTION_PIN
DIGITAL_OUT(dirX2, dir_outbits.x);
#endif
#ifdef Y2_DIRECTION_PIN
DIGITAL_OUT(dirY2, dir_outbits.y);
#endif
#ifdef Z2_DIRECTION_PIN
DIGITAL_OUT(dirZ2, dir_outbits.z);
#endif
#endif
#ifdef A_AXIS
DIGITAL_OUT(dirA, dir_outbits.a);
#endif
#ifdef B_AXIS
DIGITAL_OUT(dirB, dir_outbits.b);
#endif
#ifdef C_AXIS
DIGITAL_OUT(dirC, dir_outbits.c);
#endif
}
// Enable steppers.
// enable.value (or enable.mask) are: bit0 -> X, bit1 -> Y...
// Individual enable bits can be accessed by enable.x, enable.y, ...
// NOTE: if a common signal is used to enable all drivers enable.x should be used to set the signal.
static void stepperEnable (axes_signals_t enable)
{
enable.value ^= settings.steppers.enable_invert.mask;
#ifdef STEPPERS_ENABLE_PIN
DIGITAL_OUT(steppersEnable, enable.x)
#endif
#ifdef X_ENABLE_PIN
DIGITAL_OUT(enableX, enable.x)
#endif
#ifdef X2_ENABLE_PIN
DIGITAL_OUT(enableX2, enable.x)
#endif
#ifdef Y_ENABLE_PIN
DIGITAL_OUT(enableY, enable.y)
#endif
#ifdef Y2_ENABLE_PIN
DIGITAL_OUT(enableY2, enable.y)
#endif
#ifdef Z_ENABLE_PIN
DIGITAL_OUT(enableZ, enable.z)
#endif
#ifdef Z2_ENABLE_PIN
DIGITAL_OUT(enableZ2, enable.z)
#endif
#ifdef A_ENABLE_PIN
DIGITAL_OUT(enableA, enable.a)
#endif
#ifdef B_ENABLE_PIN
DIGITAL_OUT(enableB, enable.b)
#endif
#ifdef C_ENABLE_PIN
DIGITAL_OUT(enableC, enable.c)
#endif
}
// Starts stepper driver timer and forces a stepper driver interrupt callback.
static void stepperWakeUp (void)
{
// Enable stepper drivers.
stepperEnable((axes_signals_t){AXES_BITMASK});
PIT_LDVAL0 = 5000;
PIT_TFLG0 |= PIT_TFLG_TIF;
PIT_TCTRL0 |= (PIT_TCTRL_TIE|PIT_TCTRL_TEN);
}
// Disables stepper driver interrupts and reset outputs.
static void stepperGoIdle (bool clear_signals)
{
PIT_TCTRL0 &= ~(PIT_TCTRL_TIE|PIT_TCTRL_TEN);
if(clear_signals) {
set_step_outputs((axes_signals_t){0});
set_dir_outputs((axes_signals_t){0});
}
}
// Sets up stepper driver interrupt timeout.
// Called at the start of each segment.
// NOTE: If a 32-bit timer is used it is advisable to limit max step time to about 2 seconds
// in order to avoid excessive delays on completion of motions
// NOTE: If a 16 bit timer is used it may be neccesary to adjust the timer clock frequency (prescaler)
// to cover the needed range. Refer to actual drivers for code examples.
static void stepperCyclesPerTick (uint32_t cycles_per_tick)
{
PIT_TCTRL0 &= ~PIT_TCTRL_TEN;
PIT_LDVAL0 = cycles_per_tick < (1UL << 20) ? cycles_per_tick : 0x000FFFFFUL;
PIT_TFLG0 |= PIT_TFLG_TIF;
PIT_TCTRL0 |= PIT_TCTRL_TEN;
}
// Start a stepper pulse, no delay version.
// stepper_t struct is defined in grbl/stepper.h
static void stepperPulseStart (stepper_t *stepper)
{
#if SPINDLE_SYNC_ENABLE
if(stepper->new_block && stepper->exec_segment->spindle_sync) {
spindle_tracker.stepper_pulse_start_normal = hal.stepper.pulse_start;
hal.stepper.pulse_start = stepperPulseStartSynchronized;
hal.stepper.pulse_start(stepper);
return;
}
#endif
if(stepper->dir_change)
set_dir_outputs(stepper->dir_outbits);
if(stepper->step_outbits.value) {
set_step_outputs(stepper->step_outbits);
TMR4_CTRL0 |= TMR_CTRL_CM(0b001);
}
}
// Start a stepper pulse, delay version.
// Note: delay is only added when there is a direction change and a pulse to be output.
// In the delayed step pulse interrupt handler the pulses are output and
// normal (no delay) operation is resumed.
// stepper_t struct is defined in grbl/stepper.h
static void stepperPulseStartDelayed (stepper_t *stepper)
{
#if SPINDLE_SYNC_ENABLE
if(stepper->new_block && stepper->exec_segment->spindle_sync) {
spindle_tracker.stepper_pulse_start_normal = hal.stepper.pulse_start;
hal.stepper.pulse_start = stepperPulseStartSynchronized;
hal.stepper.pulse_start(stepper);
return;
}
#endif
if(stepper->dir_change) {
set_dir_outputs(stepper->dir_outbits);
if(stepper->step_outbits.value) {
next_step_outbits = stepper->step_outbits; // Store out_bits
attachInterruptVector(IRQ_QTIMER4, stepper_pulse_isr_delayed);
TMR4_COMP10 = pulse_delay;
TMR4_CTRL0 |= TMR_CTRL_CM(0b001);
}
return;
}
if(stepper->step_outbits.value) {
set_step_outputs(stepper->step_outbits);
TMR4_CTRL0 |= TMR_CTRL_CM(0b001);
}
}
#if SPINDLE_SYNC_ENABLE
// Spindle sync version: sets stepper direction and pulse pins and starts a step pulse.
// Switches back to "normal" version if spindle synchronized motion is finished.
// TODO: add delayed pulse handling...
static void stepperPulseStartSynchronized (stepper_t *stepper)
{
static bool sync = false;
static float block_start;
if(stepper->new_block) {
if(!stepper->exec_segment->spindle_sync) {
hal.stepper.pulse_start = spindle_tracker.stepper_pulse_start_normal;
hal.stepper.pulse_start(stepper);
return;
}
sync = true;
set_dir_outputs(stepper->dir_outbits);
spindle_tracker.programmed_rate = stepper->exec_block->programmed_rate;
spindle_tracker.steps_per_mm = stepper->exec_block->steps_per_mm;
spindle_tracker.segment_id = 0;
spindle_tracker.prev_pos = 0.0f;
block_start = spindleGetData(SpindleData_AngularPosition)->angular_position * spindle_tracker.programmed_rate;
pidf_reset(&spindle_tracker.pid);
#ifdef PID_LOG
sys.pid_log.idx = 0;
sys.pid_log.setpoint = 100.0f;
#endif
}
if(stepper->step_outbits.value) {
set_step_outputs(stepper->step_outbits);
TMR4_CTRL0 |= TMR_CTRL_CM(0b001);
}
if(spindle_tracker.segment_id != stepper->exec_segment->id) {
spindle_tracker.segment_id = stepper->exec_segment->id;
if(!stepper->new_block) { // adjust this segments total time for any positional error since last segment
float actual_pos;
if(stepper->exec_segment->cruising) {
float dt = (float)hal.f_step_timer / (float)(stepper->exec_segment->cycles_per_tick * stepper->exec_segment->n_step);
actual_pos = spindleGetData(SpindleData_AngularPosition)->angular_position * spindle_tracker.programmed_rate;
if(sync) {
spindle_tracker.pid.sample_rate_prev = dt;
// block_start += (actual_pos - spindle_tracker.block_start) - spindle_tracker.prev_pos;
// block_start += spindle_tracker.prev_pos;
sync = false;
}
actual_pos -= block_start;
int32_t step_delta = (int32_t)(pidf(&spindle_tracker.pid, spindle_tracker.prev_pos, actual_pos, dt) * spindle_tracker.steps_per_mm);
int32_t ticks = (((int32_t)stepper->step_count + step_delta) * (int32_t)stepper->exec_segment->cycles_per_tick) / (int32_t)stepper->step_count;
stepper->exec_segment->cycles_per_tick = (uint32_t)max(ticks, spindle_tracker.min_cycles_per_tick);
stepperCyclesPerTick(stepper->exec_segment->cycles_per_tick);
} else
actual_pos = spindle_tracker.prev_pos;
#ifdef PID_LOG
if(sys.pid_log.idx < PID_LOG) {
sys.pid_log.target[sys.pid_log.idx] = spindle_tracker.prev_pos;
sys.pid_log.actual[sys.pid_log.idx] = actual_pos; // - spindle_tracker.prev_pos;
// spindle_tracker.log[sys.pid_log.idx] = STEPPER_TIMER->BGLOAD << stepper->amass_level;
// spindle_tracker.pos[sys.pid_log.idx] = stepper->exec_segment->cycles_per_tick stepper->amass_level;
// spindle_tracker.pos[sys.pid_log.idx] = stepper->exec_segment->cycles_per_tick * stepper->step_count;
// STEPPER_TIMER->BGLOAD = STEPPER_TIMER->LOAD;
// spindle_tracker.pos[sys.pid_log.idx] = spindle_tracker.prev_pos;
sys.pid_log.idx++;
}
#endif
}
spindle_tracker.prev_pos = stepper->exec_segment->target_position;
}
}
#endif
#if PLASMA_ENABLE
static void output_pulse_isr(void);
static axes_signals_t pulse_output = {0};
void stepperOutputStep (axes_signals_t step_outbits, axes_signals_t dir_outbits)
{
pulse_output = step_outbits;
dir_outbits.value ^= settings.steppers.dir_invert.mask;
DIGITAL_OUT(dirZ, dir_outbits.z);
TMR2_CTRL0 |= TMR_CTRL_CM(0b001);
}
#endif
// Returns limit state as an axes_signals_t variable.
// Each bitfield bit indicates an axis limit, where triggered is 1 and not triggered is 0.
// Dual limit switch inputs per axis version. Only one needs to be dual input!
inline static limit_signals_t limitsGetState()
{
limit_signals_t signals = {0};
signals.min.mask = settings.limits.invert.mask;
#ifdef DUAL_LIMIT_SWITCHES
signals.min2.mask = settings.limits.invert.mask;
#endif