-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathdriver.c
2524 lines (2125 loc) · 81.7 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 STM32F4xx ARM processors
Part of grblHAL
Copyright (c) 2019-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/>.
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "main.h"
#include "driver.h"
#include "serial.h"
#include "grbl/limits.h"
#include "grbl/protocol.h"
#include "grbl/motor_pins.h"
#include "grbl/pin_bits_masks.h"
#include "grbl/state_machine.h"
#if I2C_ENABLE
#include "i2c.h"
#endif
#if SDCARD_ENABLE
#include "sdcard/sdcard.h"
#include "ff.h"
#include "diskio.h"
#endif
#if USB_SERIAL_CDC
#include "usb_serial.h"
#endif
#if EEPROM_ENABLE
#include "eeprom/eeprom.h"
#endif
#if PPI_ENABLE
#include "laser/ppi.h"
#endif
#if KEYPAD_ENABLE == 2
#include "keypad/keypad.h"
#endif
#if FLASH_ENABLE
#include "flash.h"
#endif
#if ETHERNET_ENABLE
#include "enet.h"
#if TELNET_ENABLE
#include "networking/TCPStream.h"
#endif
#if WEBSOCKET_ENABLE
#include "networking/WsStream.h"
#endif
#endif
#if !I2C_STROBE_ENABLE
#define I2C_STROBE_BIT 0
#endif
#if !SPINDLE_SYNC_ENABLE
#define SPINDLE_INDEX_BIT 0
#endif
#if !SAFETY_DOOR_ENABLE
#define SAFETY_DOOR_BIT 0
#endif
#ifdef MPG_MODE_PIN
#define MPG_MODE_BIT (1<<MPG_MODE_PIN)
#else
#define MPG_MODE_BIT 0
#endif
#if CONTROL_MASK != (RESET_BIT+FEED_HOLD_BIT+CYCLE_START_BIT+SAFETY_DOOR_BIT)
#error Interrupt enabled input pins must have unique pin numbers!
#endif
#define DRIVER_IRQMASK (LIMIT_MASK|CONTROL_MASK|I2C_STROBE_BIT|SPINDLE_INDEX_BIT|MPG_MODE_BIT)
#ifdef Z_LIMIT_POLL
#if (DRIVER_IRQMASK-Z_LIMIT_BIT) != (LIMIT_MASK-Z_LIMIT_BIT+CONTROL_MASK+I2C_STROBE_BIT+SPINDLE_INDEX_BIT+MPG_MODE_BIT)
#error Interrupt enabled input pins must have unique pin numbers!
#endif
#else
#if DRIVER_IRQMASK != (LIMIT_MASK+CONTROL_MASK+I2C_STROBE_BIT+SPINDLE_INDEX_BIT+MPG_MODE_BIT)
#error Interrupt enabled input pins must have unique pin numbers!
#endif
#endif
typedef union {
uint8_t mask;
struct {
uint8_t limits :1,
door :1,
unused :6;
};
} debounce_t;
#if (!VFD_SPINDLE || N_SPINDLE > 1) && defined(SPINDLE_ENABLE_PIN)
#define PWM_SPINDLE
#if defined(SPINDLE_PWM_TIMER_N)
static bool pwmEnabled = false;
static spindle_pwm_t spindle_pwm;
static void spindle_set_speed (uint_fast16_t pwm_value);
#endif
#elif defined(SPINDLE_PWM_TIMER_N)
#undef SPINDLE_PWM_TIMER_N
#endif
static periph_signal_t *periph_pins = NULL;
static input_signal_t inputpin[] = {
{ .id = Input_Reset, .port = RESET_PORT, .pin = RESET_PIN, .group = PinGroup_Control },
{ .id = Input_FeedHold, .port = FEED_HOLD_PORT, .pin = FEED_HOLD_PIN, .group = PinGroup_Control },
{ .id = Input_CycleStart, .port = CYCLE_START_PORT, .pin = CYCLE_START_PIN, .group = PinGroup_Control },
#if SAFETY_DOOR_ENABLE
{ .id = Input_SafetyDoor, .port = SAFETY_DOOR_PORT, .pin = SAFETY_DOOR_PIN, .group = PinGroup_Control },
#endif
#ifdef PROBE_PIN
{ .id = Input_Probe, .port = PROBE_PORT, .pin = PROBE_PIN, .group = PinGroup_Probe },
#endif
#ifdef I2C_STROBE_PIN
{ .id = Input_KeypadStrobe, .port = I2C_STROBE_PORT, .pin = I2C_STROBE_PIN, .group = PinGroup_Keypad },
#endif
#ifdef MPG_MODE_PIN
{ .id = Input_MPGSelect, .port = MPG_MODE_PORT, .pin = MPG_MODE_PIN, .group = PinGroup_MPG },
#endif
// Limit input pins must be consecutive in this array
{ .id = Input_LimitX, .port = X_LIMIT_PORT, .pin = X_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef X2_LIMIT_PIN
{ .id = Input_LimitX_2, .port = X2_LIMIT_PORT, .pin = X2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
{ .id = Input_LimitY, .port = Y_LIMIT_PORT, .pin = Y_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef Y2_LIMIT_PIN
{ .id = Input_LimitY_2, .port = Y2_LIMIT_PORT, .pin = Y2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
{ .id = Input_LimitZ, .port = Z_LIMIT_PORT, .pin = Z_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef Z2_LIMIT_PIN
, { .id = Input_LimitZ_2, .port = Z2_LIMIT_PORT, .pin = Z2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef A_LIMIT_PIN
{ .id = Input_LimitA, .port = A_LIMIT_PORT, .pin = A_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef B_LIMIT_PIN
{ .id = Input_LimitB, .port = B_LIMIT_PORT, .pin = B_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef C_LIMIT_PIN
{ .id = Input_LimitC, .port = C_LIMIT_PORT, .pin = C_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#if SPINDLE_SYNC_ENABLE
{ .id = Input_SpindleIndex, .port = SPINDLE_INDEX_PORT, .pin = SPINDLE_INDEX_PIN, .group = PinGroup_SpindleIndex },
#endif
// Aux input pins must be consecutive in this array
#ifdef AUXINPUT0_PIN
{ .id = Input_Aux0, .port = AUXINPUT0_PORT, .pin = AUXINPUT0_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT1_PIN
{ .id = Input_Aux1, .port = AUXINPUT1_PORT, .pin = AUXINPUT1_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT2_PIN
{ .id = Input_Aux2, .port = AUXINPUT2_PORT, .pin = AUXINPUT2_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT3_PIN
{ .id = Input_Aux3, .port = AUXINPUT3_PORT, .pin = AUXINPUT3_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT4_PIN
{ .id = Input_Aux4, .port = AUXINPUT4_PORT, .pin = AUXINPUT4_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT5_PIN
{ .id = Input_Aux5, .port = AUXINPUT5_PORT, .pin = AUXINPUT5_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT6_PIN
{ .id = Input_Aux6, .port = AUXINPUT6_PORT, .pin = AUXINPUT6_PIN, .group = PinGroup_AuxInput }
#endif
};
static output_signal_t outputpin[] = {
{ .id = Output_StepX, .port = X_STEP_PORT, .pin = X_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
{ .id = Output_StepY, .port = Y_STEP_PORT, .pin = Y_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
{ .id = Output_StepZ, .port = Z_STEP_PORT, .pin = Z_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
#ifdef A_AXIS
{ .id = Output_StepA, .port = A_STEP_PORT, .pin = A_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
#endif
#ifdef B_AXIS
{ .id = Output_StepB, .port = B_STEP_PORT, .pin = B_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
#endif
#ifdef C_AXIS
{ .id = Output_StepC, .port = C_STEP_PORT, .pin = C_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
#endif
#ifdef X2_STEP_PIN
{ .id = Output_StepX_2, .port = X2_STEP_PORT, .pin = X2_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
#endif
#ifdef Y2_STEP_PIN
{ .id = Output_StepY_2, .port = Y2_STEP_PORT, .pin = Y2_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
#endif
#ifdef Z2_STEP_PIN
{ .id = Output_StepZ_2, .port = Z2_STEP_PORT, .pin = Z2_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE} },
#endif
{ .id = Output_DirX, .port = X_DIRECTION_PORT, .pin = X_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
{ .id = Output_DirY, .port = Y_DIRECTION_PORT, .pin = Y_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
{ .id = Output_DirZ, .port = Z_DIRECTION_PORT, .pin = Z_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
#ifdef A_AXIS
{ .id = Output_DirA, .port = A_DIRECTION_PORT, .pin = A_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
#endif
#ifdef B_AXIS
{ .id = Output_DirB, .port = B_DIRECTION_PORT, .pin = B_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
#endif
#ifdef C_AXIS
{ .id = Output_DirC, .port = C_DIRECTION_PORT, .pin = C_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
#endif
#ifdef X2_DIRECTION_PIN
{ .id = Output_DirX_2, .port = X2_DIRECTION_PORT, .pin = X2_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
#endif
#ifdef Y2_DIRECTION_PIN
{ .id = Output_DirY_2, .port = Y2_DIRECTION_PORT, .pin = Y2_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
#endif
#ifdef Z2_DIRECTION_PIN
{ .id = Output_DirZ_2, .port = Z2_DIRECTION_PORT, .pin = Z2_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE} },
#endif
#ifdef STEPPERS_POWER_PORT
{ .id = Output_StepperPower, .port = STEPPERS_POWER_PORT, .pin = STEPPERS_POWER_PIN, .group = PinGroup_StepperPower },
#endif
#if !TRINAMIC_MOTOR_ENABLE
#ifdef STEPPERS_ENABLE_PORT
{ .id = Output_StepperEnable, .port = STEPPERS_ENABLE_PORT, .pin = STEPPERS_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef X_ENABLE_PORT
{ .id = Output_StepperEnableX, .port = X_ENABLE_PORT, .pin = X_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef Y_ENABLE_PORT
{ .id = Output_StepperEnableY, .port = Y_ENABLE_PORT, .pin = Y_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef Z_ENABLE_PORT
{ .id = Output_StepperEnableZ, .port = Z_ENABLE_PORT, .pin = Z_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef A_ENABLE_PORT
{ .id = Output_StepperEnableA, .port = A_ENABLE_PORT, .pin = A_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef B_ENABLE_PORT
{ .id = Output_StepperEnableB, .port = B_ENABLE_PORT, .pin = B_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef C_ENABLE_PORT
{ .id = Output_StepperEnableC, .port = C_ENABLE_PORT, .pin = C_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef X2_ENABLE_PIN
{ .id = Output_StepperEnableX, .port = X2_ENABLE_PORT, .pin = X2_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef Y2_ENABLE_PIN
{ .id = Output_StepperEnableY, .port = Y2_ENABLE_PORT, .pin = Y2_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#ifdef Z2_ENABLE_PIN
{ .id = Output_StepperEnableZ, .port = Z2_ENABLE_PORT, .pin = Z2_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = {STEPPERS_ENABLE_PINMODE} },
#endif
#endif // TRINAMIC_MOTOR_ENABLE
#ifdef MOTOR_CS_PIN
{ .id = Output_MotorChipSelect, .port = MOTOR_CS_PORT, .pin = MOTOR_CS_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_CSX_PIN
{ .id = Output_MotorChipSelectX, .port = MOTOR_CSX_PORT, .pin = MOTOR_CSX_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_CSY_PIN
{ .id = Output_MotorChipSelectY, .port = MOTOR_CSY_PORT, .pin = MOTOR_CSY_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_CSZ_PIN
{ .id = Output_MotorChipSelectZ, .port = MOTOR_CSZ_PORT, .pin = MOTOR_CSZ_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_CSM3_PIN
{ .id = Output_MotorChipSelectM3, .port = MOTOR_CSM3_PORT, .pin = MOTOR_CSM3_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_CSM4_PIN
{ .id = Output_MotorChipSelectM4, .port = MOTOR_CSM4_PORT, .pin = MOTOR_CSM4_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_CSM5_PIN
{ .id = Output_MotorChipSelectM5, .port = MOTOR_CSM5_PORT, .pin = MOTOR_CSM5_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_CS_PIN
{ .id = Output_MotorChipSelect, .port = MOTOR_CS_PORT, .pin = MOTOR_CS_PIN, .group = PinGroup_MotorChipSelect },
#endif
#ifdef MOTOR_UARTX_PIN
{ .id = Bidirectional_MotorUARTX, .port = MOTOR_UARTX_PORT, .pin = MOTOR_UARTX_PIN, .group = PinGroup_MotorUART },
#endif
#ifdef MOTOR_UARTY_PIN
{ .id = Bidirectional_MotorUARTY, .port = MOTOR_UARTY_PORT, .pin = MOTOR_UARTY_PIN, .group = PinGroup_MotorUART },
#endif
#ifdef MOTOR_UARTZ_PIN
{ .id = Bidirectional_MotorUARTZ, .port = MOTOR_UARTZ_PORT, .pin = MOTOR_UARTZ_PIN, .group = PinGroup_MotorUART },
#endif
#ifdef MOTOR_UARTM3_PIN
{ .id = Bidirectional_MotorUARTM3, .port = MOTOR_UARTM3_PORT, .pin = MOTOR_UARTM3_PIN, .group = PinGroup_MotorUART },
#endif
#ifdef MOTOR_UARTM4_PIN
{ .id = Bidirectional_MotorUARTM4, .port = MOTOR_UARTM4_PORT, .pin = MOTOR_UARTM4_PIN, .group = PinGroup_MotorUART },
#endif
#ifdef MOTOR_UARTM5_PIN
{ .id = Bidirectional_MotorUARTM5, .port = MOTOR_UARTM5_PORT, .pin = MOTOR_UARTM5_PIN, .group = PinGroup_MotorUART },
#endif
#ifdef PWM_SPINDLE
#ifdef SPINDLE_ENABLE_PIN
{ .id = Output_SpindleOn, .port = SPINDLE_ENABLE_PORT, .pin = SPINDLE_ENABLE_PIN, .group = PinGroup_SpindleControl },
#endif
#ifdef SPINDLE_DIRECTION_PIN
{ .id = Output_SpindleDir, .port = SPINDLE_DIRECTION_PORT, .pin = SPINDLE_DIRECTION_PIN, .group = PinGroup_SpindleControl },
#endif
#endif // PWM_SPINDLE
{ .id = Output_CoolantFlood, .port = COOLANT_FLOOD_PORT, .pin = COOLANT_FLOOD_PIN, .group = PinGroup_Coolant },
#ifdef COOLANT_MIST_PIN
{ .id = Output_CoolantMist, .port = COOLANT_MIST_PORT, .pin = COOLANT_MIST_PIN, .group = PinGroup_Coolant },
#endif
#ifdef SD_CS_PORT
{ .id = Output_SdCardCS, .port = SD_CS_PORT, .pin = SD_CS_PIN, .group = PinGroup_SdCard },
#endif
#ifdef AUXOUTPUT0_PORT
{ .id = Output_Aux0, .port = AUXOUTPUT0_PORT, .pin = AUXOUTPUT0_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT1_PORT
{ .id = Output_Aux1, .port = AUXOUTPUT1_PORT, .pin = AUXOUTPUT1_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT2_PORT
{ .id = Output_Aux2, .port = AUXOUTPUT2_PORT, .pin = AUXOUTPUT2_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT3_PORT
{ .id = Output_Aux3, .port = AUXOUTPUT3_PORT, .pin = AUXOUTPUT3_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT4_PORT
{ .id = Output_Aux4, .port = AUXOUTPUT4_PORT, .pin = AUXOUTPUT4_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT5_PORT
{ .id = Output_Aux5, .port = AUXOUTPUT5_PORT, .pin = AUXOUTPUT5_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT6_PORT
{ .id = Output_Aux6, .port = AUXOUTPUT6_PORT, .pin = AUXOUTPUT6_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT7_PORT
{ .id = Output_Aux7, .port = AUXOUTPUT7_PORT, .pin = AUXOUTPUT7_PIN, .group = PinGroup_AuxOutput }
#endif
};
extern __IO uint32_t uwTick;
static uint32_t pulse_length, pulse_delay, aux_irq = 0;;
static bool IOInitDone = false;
static axes_signals_t next_step_outbits;
static delay_t delay = { .ms = 1, .callback = NULL }; // NOTE: initial ms set to 1 for "resetting" systick timer on startup
static debounce_t debounce;
#ifdef PROBE_PIN
static probe_state_t probe = {
.connected = On
};
#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
#include "grbl/stepdir_map.h"
#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);
#endif
static void driver_delay (uint32_t ms, delay_callback_ptr callback)
{
if((delay.ms = ms) > 0) {
if(!(delay.callback = callback)) {
while(delay.ms)
grbl.on_execute_delay(state_get());
}
} else {
delay.callback = NULL;
if(callback)
callback();
}
}
// Enable/disable stepper motors
static void stepperEnable (axes_signals_t enable)
{
enable.mask ^= settings.steppers.enable_invert.mask;
#if !TRINAMIC_MOTOR_ENABLE
#ifdef STEPPERS_ENABLE_PORT
DIGITAL_OUT(STEPPERS_ENABLE_PORT, STEPPERS_ENABLE_PIN, enable.x);
#else
DIGITAL_OUT(X_ENABLE_PORT, X_ENABLE_PIN, enable.x);
#ifdef X2_ENABLE_PIN
DIGITAL_OUT(X2_ENABLE_PORT, X2_ENABLE_PIN, enable.x);
#endif
DIGITAL_OUT(Y_ENABLE_PORT, Y_ENABLE_PIN, enable.y);
#ifdef Y2_ENABLE_PIN
DIGITAL_OUT(Y2_ENABLE_PORT, Y2_ENABLE_PIN, enable.y);
#endif
DIGITAL_OUT(Z_ENABLE_PORT, Z_ENABLE_PIN, enable.z);
#ifdef Z2_ENABLE_PIN
DIGITAL_OUT(Z2_ENABLE_PORT, Z2_ENABLE_PIN, enable.z);
#endif
#ifdef A_ENABLE_PORT
DIGITAL_OUT(A_ENABLE_PORT, A_ENABLE_PIN, enable.a);
#endif
#ifdef B_ENABLE_PORT
DIGITAL_OUT(B_ENABLE_PORT, B_ENABLE_PIN, enable.b);
#endif
#ifdef C_ENABLE_PORT
DIGITAL_OUT(C_ENABLE_PORT, C_ENABLE_PIN, enable.c);
#endif
#endif
#endif
}
// Starts stepper driver ISR timer and forces a stepper driver interrupt callback
static void stepperWakeUp (void)
{
stepperEnable((axes_signals_t){AXES_BITMASK});
STEPPER_TIMER->ARR = 5000; // delay to allow drivers time to wake up
STEPPER_TIMER->EGR = TIM_EGR_UG;
STEPPER_TIMER->CR1 |= TIM_CR1_CEN;
}
// Disables stepper driver interrupts
static void stepperGoIdle (bool clear_signals)
{
STEPPER_TIMER->CR1 &= ~TIM_CR1_CEN;
STEPPER_TIMER->CNT = 0;
}
// Sets up stepper driver interrupt timeout, "Normal" version
static void stepperCyclesPerTick (uint32_t cycles_per_tick)
{
STEPPER_TIMER->ARR = cycles_per_tick < (1UL << 20) ? cycles_per_tick : 0x000FFFFFUL;
}
// Set stepper pulse output pins
// NOTE: step_outbits are: bit0 -> X, bit1 -> Y, bit2 -> Z...
#ifdef SQUARING_ENABLED
inline static __attribute__((always_inline)) void stepperSetStepOutputs (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;
#if STEP_OUTMODE == GPIO_BITBAND
step_outbits_1.mask = (step_outbits_1.mask & motors_1.mask) ^ settings.steppers.step_invert.mask;
DIGITAL_OUT(X_STEP_PORT, X_STEP_PIN, step_outbits_1.x);
#ifdef X2_STEP_PIN
DIGITAL_OUT(X2_STEP_PORT, X2_STEP_PIN, step_outbits_2.x);
#endif
DIGITAL_OUT(Y_STEP_PORT, Y_STEP_PIN, step_outbits_1.y);
#ifdef Y2_STEP_PIN
DIGITAL_OUT(Y2_STEP_PORT, Y2_STEP_PIN, step_outbits_2.y);
#endif
DIGITAL_OUT(Z_STEP_PORT, Z_STEP_PIN, step_outbits_1.z);
#ifdef Z2_STEP_PIN
DIGITAL_OUT(Z2_STEP_PORT, Z2_STEP_PIN, step_outbits_2.z);
#endif
#ifdef A_AXIS
DIGITAL_OUT(A_STEP_PORT, A_STEP_PIN, step_outbits_1.a);
#endif
#ifdef B_AXIS
DIGITAL_OUT(B_STEP_PORT, B_STEP_PIN, step_outbits_1.b);
#endif
#ifdef C_AXIS
DIGITAL_OUT(C_STEP_PORT, C_STEP_PIN, step_outbits_1.c);
#endif
#elif STEP_OUTMODE == GPIO_MAP
STEP_PORT->ODR = (STEP_PORT->ODR & ~STEP_MASK) | step_outmap[step_outbits_1.value & motors_1.mask];
#ifdef X2_STEP_PIN
DIGITAL_OUT(X2_STEP_PORT, X2_STEP_PIN, step_outbits_2.x);
#endif
#ifdef Y2_STEP_PIN
DIGITAL_OUT(Y2_STEP_PORT, Y2_STEP_PIN, step_outbits_2.y);
#endif
#ifdef Z2_STEP_PIN
DIGITAL_OUT(Z2_STEP_PORT, Z2_STEP_PIN, step_outbits_2.z);
#endif
#else // STEP_OUTMODE == GPIO_SHIFTx
STEP_PORT->ODR = (STEP_PORT->ODR & ~STEP_MASK) | (((step_outbits_1.mask & motors_1.mask) ^ settings.steppers.step_invert.mask) << STEP_OUTMODE);
#ifdef X2_STEP_PIN
DIGITAL_OUT(X2_STEP_PORT, X2_STEP_PIN, step_outbits_2.x);
#endif
#ifdef Y2_STEP_PIN
DIGITAL_OUT(Y2_STEP_PORT, Y2_STEP_PIN, step_outbits_2.y);
#endif
#ifdef Z2_STEP_PIN
DIGITAL_OUT(Z2_STEP_PORT, Z2_STEP_PIN, step_outbits_2.z);
#endif
#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 // SQUARING DISABLED
inline static __attribute__((always_inline)) void stepperSetStepOutputs (axes_signals_t step_outbits)
{
#if STEP_OUTMODE == GPIO_BITBAND
step_outbits.mask ^= settings.steppers.step_invert.mask;
DIGITAL_OUT(X_STEP_PORT, X_STEP_PIN, step_outbits.x);
#ifdef X2_STEP_PIN
DIGITAL_OUT(X2_STEP_PORT, X2_STEP_PIN, step_outbits.x);
#endif
DIGITAL_OUT(Y_STEP_PORT, Y_STEP_PIN, step_outbits.y);
#ifdef Y2_STEP_PIN
DIGITAL_OUT(Y2_STEP_PORT, Y2_STEP_PIN, step_outbits.y);
#endif
DIGITAL_OUT(Z_STEP_PORT, Z_STEP_PIN, step_outbits.z);
#ifdef Z2_STEP_PIN
DIGITAL_OUT(Z2_STEP_PORT, Z2_STEP_PIN, step_outbits.z);
#endif
#ifdef A_AXIS
DIGITAL_OUT(A_STEP_PORT, A_STEP_PIN, step_outbits.a);
#endif
#ifdef B_AXIS
DIGITAL_OUT(B_STEP_PORT, B_STEP_PIN, step_outbits.b);
#endif
#ifdef C_AXIS
DIGITAL_OUT(C_STEP_PORT, C_STEP_PIN, step_outbits.c);
#endif
#elif STEP_OUTMODE == GPIO_MAP
STEP_PORT->ODR = (STEP_PORT->ODR & ~STEP_MASK) | step_outmap[step_outbits.value];
#ifdef X2_STEP_PIN
DIGITAL_OUT(X2_STEP_PORT, X2_STEP_PIN, step_outbits.x ^ settings.steppers.step_invert.x);
#endif
#ifdef Y2_STEP_PIN
DIGITAL_OUT(Y2_STEP_PORT, Y2_STEP_PIN, step_outbits.y ^ settings.steppers.step_invert.y);
#endif
#ifdef Z2_STEP_PIN
DIGITAL_OUT(Z2_STEP_PORT, Z2_STEP_PIN, step_outbits.z ^ settings.steppers.step_invert.z);
#endif
#else // STEP_OUTMODE == GPIO_SHIFTx
#if N_GANGED
step_outbits.mask ^= settings.steppers.step_invert.mask;
STEP_PORT->ODR = (STEP_PORT->ODR & ~STEP_MASK) | (step_outbits.mask << STEP_OUTMODE);
#ifdef X2_STEP_PIN
DIGITAL_OUT(X2_STEP_PORT, X2_STEP_PIN, step_outbits.x);
#endif
#ifdef Y2_PIN
DIGITAL_OUT(Y2_STEP_PORT, Y2_STEP_PIN, step_outbits.y);
#endif
#ifdef Z2_STEP_PIN
DIGITAL_OUT(Z2_STEP_PORT, Z2_STEP_PIN, step_outbits.z);
#endif
#else
STEP_PORT->ODR = (STEP_PORT->ODR & ~STEP_MASK) | ((step_outbits.value << STEP_OUTMODE) ^ settings.steppers.step_invert.value);
#endif
#endif
}
#endif
#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 output pins
// NOTE: see note for stepperSetStepOutputs()
inline static __attribute__((always_inline)) void stepperSetDirOutputs (axes_signals_t dir_outbits)
{
#if DIRECTION_OUTMODE == GPIO_BITBAND
dir_outbits.mask ^= settings.steppers.dir_invert.mask;
DIGITAL_OUT(X_DIRECTION_PORT, X_DIRECTION_PIN, dir_outbits.x);
DIGITAL_OUT(Y_DIRECTION_PORT, Y_DIRECTION_PIN, dir_outbits.y);
DIGITAL_OUT(Z_DIRECTION_PORT, Z_DIRECTION_PIN, dir_outbits.z);
#ifdef GANGING_ENABLED
dir_outbits.mask ^= settings.steppers.ganged_dir_invert.mask;
#ifdef X2_DIRECTION_PIN
DIGITAL_OUT(X2_DIRECTION_PORT, X2_DIRECTION_PIN, dir_outbits.x);
#endif
#ifdef Y2_DIRECTION_PIN
DIGITAL_OUT(Y2_DIRECTION_PORT, Y2_DIRECTION_PIN, dir_outbits.y);
#endif
#ifdef Z2_DIRECTION_PIN
DIGITAL_OUT(Z2_DIRECTION_PORT, Z2_DIRECTION_PIN, dir_outbits.z);
#endif
#endif
#ifdef A_AXIS
DIGITAL_OUT(A_DIRECTION_PORT, A_DIRECTION_PIN, dir_outbits.a);
#endif
#ifdef B_AXIS
DIGITAL_OUT(B_DIRECTION_PORT, B_DIRECTION_PIN, dir_outbits.b);
#endif
#ifdef C_AXIS
DIGITAL_OUT(C_DIRECTION_PORT, C_DIRECTION_PIN, dir_outbits.c);
#endif
#elif DIRECTION_OUTMODE == GPIO_MAP
DIRECTION_PORT->ODR = (DIRECTION_PORT->ODR & ~DIRECTION_MASK) | dir_outmap[dir_outbits.value];
#ifdef X2_DIRECTION_PIN
DIGITAL_OUT(X2_DIRECTION_PORT, X2_DIRECTION_PIN, (dir_outbits.x ^ settings.steppers.dir_invert.x) ^ settings.steppers.ganged_dir_invert.x;
#endif
#ifdef Y2_DIRECTION_PIN
DIGITAL_OUT(Y2_DIRECTION_PORT, Y2_DIRECTION_PIN, (dir_outbits.y ^ settings.steppers.dir_invert.y) ^ settings.steppers.ganged_dir_invert.y);
#endif
#ifdef Z2_DIRECTION_PIN
DIGITAL_OUT(Z2_DIRECTION_PORT, Z2_DIRECTION_PIN, (dir_outbits.z ^ settings.steppers.dir_invert.z) ^ settings.steppers.ganged_dir_invert.z;
#endif
#else // DIRECTION_OUTMODE = SHIFTx
#ifdef GANGING_ENABLED
dir_outbits.mask ^= settings.steppers.dir_invert.mask;
DIRECTION_PORT->ODR = (DIRECTION_PORT->ODR & ~DIRECTION_MASK) | (dir_outbits.mask << DIRECTION_OUTMODE);
dir_outbits.mask ^= settings.steppers.ganged_dir_invert.mask;
#ifdef X2_DIRECTION_PIN
DIGITAL_OUT(X2_DIRECTION_PORT, X2_DIRECTION_PIN, dir_outbits.x);
#endif
#ifdef Y2_DIRECTION_PIN
DIGITAL_OUT(Y2_DIRECTION_PORT, Y2_DIRECTION_PIN, dir_outbits.y);
#endif
#ifdef Z2_DIRECTION_PIN
DIGITAL_OUT(Z2_DIRECTION_PORT, Z2_DIRECTION_PIN, dir_outbits.z);
#endif
#else
DIRECTION_PORT->ODR = (DIRECTION_PORT->ODR & ~DIRECTION_MASK) | ((dir_outbits.mask ^ settings.steppers.dir_invert.mask) << DIRECTION_OUTMODE);
#endif
#endif
}
// Sets stepper direction and pulse pins and starts a step pulse.
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)
stepperSetDirOutputs(stepper->dir_outbits);
if(stepper->step_outbits.value) {
stepperSetStepOutputs(stepper->step_outbits);
PULSE_TIMER->EGR = TIM_EGR_UG;
PULSE_TIMER->CR1 |= TIM_CR1_CEN;
}
}
// Start a stepper pulse, delay version.
// Note: delay is only added when there is a direction change and a pulse to be output.
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) {
stepperSetDirOutputs(stepper->dir_outbits);
if(stepper->step_outbits.value) {
next_step_outbits = stepper->step_outbits; // Store out_bits
PULSE_TIMER->ARR = pulse_delay;
PULSE_TIMER->EGR = TIM_EGR_UG;
PULSE_TIMER->CR1 |= TIM_CR1_CEN;
}
return;
}
if(stepper->step_outbits.value) {
stepperSetStepOutputs(stepper->step_outbits);
PULSE_TIMER->EGR = TIM_EGR_UG;
PULSE_TIMER->CR1 |= TIM_CR1_CEN;
}
}
#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;
stepperSetDirOutputs(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) {
stepperSetStepOutputs(stepper->step_outbits);
PULSE_TIMER->EGR = TIM_EGR_UG;
PULSE_TIMER->CR1 |= TIM_CR1_CEN;
}
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;
// spindle_tracker.block_start += (actual_pos - spindle_tracker.block_start) - spindle_tracker.prev_pos;
// spindle_tracker.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 >> stepper->exec_segment->amass_level);
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
// Enable/disable limit pins interrupt
static void limitsEnable (bool on, bool homing)
{
if(on) {
EXTI->PR |= LIMIT_MASK; // Clear any pending limit interrupts
EXTI->IMR |= LIMIT_MASK; // and enable
} else
EXTI->IMR &= ~LIMIT_MASK;
}
// 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.
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
#ifdef MAX_LIMIT_SWITCHES
signals.max.mask = settings.limits.invert.mask;
#endif
#if LIMIT_INMODE == GPIO_BITBAND
signals.min.x = DIGITAL_IN(X_LIMIT_PORT, X_LIMIT_PIN);
signals.min.y = DIGITAL_IN(Y_LIMIT_PORT, Y_LIMIT_PIN);
signals.min.z = DIGITAL_IN(Z_LIMIT_PORT, Z_LIMIT_PIN);
#ifdef A_LIMIT_PIN
signals.min.a = DIGITAL_IN(A_LIMIT_PORT, A_LIMIT_PIN);
#endif
#ifdef B_LIMIT_PIN
signals.min.b = DIGITAL_IN(B_LIMIT_PORT, B_LIMIT_PIN);
#endif
#ifdef C_LIMIT_PIN
signals.min.c = DIGITAL_IN(C_LIMIT_PORT, C_LIMIT_PIN);
#endif
#elif LIMIT_INMODE == GPIO_MAP
uint32_t bits = LIMIT_PORT->IDR;
signals.min.x = !!(bits & X_LIMIT_BIT);
signals.min.y = !!(bits & Y_LIMIT_BIT);
signals.min.z = !!(bits & Z_LIMIT_BIT);
#ifdef A_LIMIT_PIN
signals.min.a = !!(bits & A_LIMIT_BIT);
#endif
#ifdef B_LIMIT_PIN
signals.min.b = !!(bits & B_LIMIT_BIT);
#endif
#ifdef C_LIMIT_PIN
signals.min.c = !!(bits & C_LIMIT_BIT);
#endif
#else
signals.min.value = (uint8_t)((LIMIT_PORT->IDR & LIMIT_MASK) >> LIMIT_INMODE);
#endif
#ifdef X2_LIMIT_PIN
signals.min2.x = DIGITAL_IN(X2_LIMIT_PORT, X2_LIMIT_PIN);
#endif
#ifdef Y2_LIMIT_PIN
signals.min2.y = DIGITAL_IN(Y2_LIMIT_PORT, Y2_LIMIT_PIN);
#endif
#ifdef Z2_LIMIT_PIN
signals.min2.z = DIGITAL_IN(Z2_LIMIT_PORT, Z2_LIMIT_PIN);
#endif
#ifdef X_LIMIT_PIN_MAX
signals.max.x = DIGITAL_IN(X_LIMIT_PORT_MAX, X_LIMIT_PIN_MAX);
#endif
#ifdef Y_LIMIT_PIN_MAX
signals.max.y = DIGITAL_IN(Y_LIMIT_PORT_MAX, Y_LIMIT_PIN_MAX);
#endif
#ifdef Z_LIMIT_PIN_MAX
signals.max.z = DIGITAL_IN(Z_LIMIT_PORT_MAX, Z_LIMIT_PIN_MAX);
#endif
if (settings.limits.invert.mask) {
signals.min.value ^= settings.limits.invert.mask;
#ifdef DUAL_LIMIT_SWITCHES
signals.min2.mask ^= settings.limits.invert.mask;
#endif
#ifdef MAX_LIMIT_SWITCHES
signals.max.value ^= settings.limits.invert.mask;
#endif
}
return signals;
}
// Returns system state as a control_signals_t variable.
// Each bitfield bit indicates a control signal, where triggered is 1 and not triggered is 0.
static control_signals_t systemGetState (void)
{
control_signals_t signals;
signals.mask = settings.control_invert.mask;
#if CONTROL_INMODE == GPIO_BITBAND
#if ESTOP_ENABLE
signals.e_stop = DIGITAL_IN(RESET_PORT, RESET_PIN);
#else
signals.reset = DIGITAL_IN(RESET_PORT, RESET_PIN);
#endif
signals.feed_hold = DIGITAL_IN(FEED_HOLD_PORT, FEED_HOLD_PIN);
signals.cycle_start = DIGITAL_IN(CYCLE_START_PORT, CYCLE_START_PIN);
#ifdef SAFETY_DOOR_PIN
signals.safety_door_ajar = DIGITAL_IN(SAFETY_DOOR_PORT, SAFETY_DOOR_PIN);
#endif
#elif CONTROL_INMODE == GPIO_MAP
uint32_t bits = CONTROL_PORT->IDR;
#if ESTOP_ENABLE
signals.e_stop = !!(bits & RESET_BIT);
#else
signals.reset = !!(bits & RESET_BIT);
#endif
signals.feed_hold = !!(bits & FEED_HOLD_BIT);
signals.cycle_start = !!(bits & CYCLE_START_BIT);
#ifdef SAFETY_DOOR_PIN
signals.safety_door_ajar = !!(bits & SAFETY_DOOR_BIT);
#endif
#else
signals.value = (uint8_t)((CONTROL_PORT->IDR & CONTROL_MASK) >> CONTROL_INMODE);
#ifndef SAFETY_DOOR_PIN
signals.safety_door_ajar = settings.control_invert.safety_door_ajar;
#endif
#if ESTOP_ENABLE
signals.e_stop = signals.reset;
signals.reset = settings.control_invert.mask.reset;
#endif
#endif
if(settings.control_invert.mask)
signals.value ^= settings.control_invert.mask;
return signals;
}
#ifdef PROBE_PIN
// Sets up the probe pin invert mask to
// appropriately set the pin logic according to setting for normal-high/normal-low operation
// and the probing cycle modes for toward-workpiece/away-from-workpiece.
static void probeConfigure (bool is_probe_away, bool probing)
{
probe.triggered = Off;
probe.is_probing = probing;
probe.inverted = is_probe_away ? !settings.probe.invert_probe_pin : settings.probe.invert_probe_pin;