-
Notifications
You must be signed in to change notification settings - Fork 49
/
driver.c
2995 lines (2500 loc) · 88.5 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 RP2040 ARM processors
Part of grblHAL
Copyright (c) 2021 Volksolive
Copyright (c) 2021-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h>
#include "pico/time.h"
#include "hardware/timer.h"
#include "hardware/irq.h"
#include "hardware/pio.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"
#include "hardware/clocks.h"
#include "hardware/spi.h"
#include "hardware/structs/systick.h"
#include "hardware/structs/iobank0.h"
#include "hardware/structs/sio.h"
#if RP_MCU == 2040
#include "hardware/rtc.h"
#endif
#include "driver.h"
#include "serial.h"
#include "driverPIO.pio.h"
#include "ws2812.pio.h"
#define AUX_DEVICES // until all drivers are converted?
#include "grbl/crossbar.h"
#include "grbl/machine_limits.h"
#include "grbl/state_machine.h"
#include "grbl/motor_pins.h"
#include "grbl/pin_bits_masks.h"
#include "grbl/protocol.h"
#if NVSDATA_BUFFER_ENABLE
#include "grbl/nvs_buffer.h"
#endif
#ifdef I2C_PORT
#include "i2c.h"
#endif
#if SDCARD_ENABLE
#include "sdcard/sdcard.h"
#include "ff.h"
#include "diskio.h"
#endif
#if LITTLEFS_ENABLE
#include "littlefs_hal.h"
#include "sdcard/fs_littlefs.h"
#endif
#if USB_SERIAL_CDC
#include "usb_serial.h"
#endif
#if EEPROM_ENABLE
#include "eeprom/eeprom.h"
#endif
#if ODOMETER_ENABLE
#include "odometer/odometer.h"
#endif
#if PPI_ENABLE
#include "laser/ppi.h"
#endif
#if FLASH_ENABLE
#include "flash.h"
#endif
#if IOEXPAND_ENABLE
#include "ioexpand.h"
#endif
#if WIFI_ENABLE
#include "wifi.h"
#endif
#if BLUETOOTH_ENABLE == 1
#include "bt_native.h"
#endif
#if WIFI_ENABLE || BLUETOOTH_ENABLE == 1
#include "pico/cyw43_arch.h"
#endif
#if ETHERNET_ENABLE
#include "networking/wiznet/enet.h"
#endif
#ifdef GPIO_PIO_1
static uint x_step_sm;
static uint y_step_sm;
static uint z_step_sm;
static PIO z_step_pio;
#ifdef X2_STEP_PIN
static PIO x2_step_pio;
static uint x2_step_sm;
#endif
#ifdef Y2_STEP_PIN
static PIO y2_step_pio;
static uint y2_step_sm;
#endif
#ifdef Z2_STEP_PIN
static PIO z2_step_pio;
static uint z2_step_sm;
#endif
#ifdef A_STEP_PIN
static PIO a_step_pio;
static uint a_step_sm;
#endif
#ifdef B_STEP_PIN
static PIO b_step_pio;
static uint b_step_sm;
#endif
#ifdef C_STEP_PIN
static PIO c_step_pio;
static uint c_step_sm;
#endif
#endif
#ifdef NEOPIXELS_PIN
#ifndef NEOPIXELS_NUM
#define NEOPIXELS_NUM 0
#endif
static PIO neop_pio;
static uint neop_sm;
#endif
typedef union
{
uint32_t value;
struct
{
uint32_t delay :8,
length :8,
set :6,
reset :6;
};
} pio_steps_t;
#if ETHERNET_ENABLE || WIFI_ENABLE || BLUETOOTH_ENABLE == 1
typedef union
{
uint8_t value;
struct
{
uint8_t wifi :1,
bluetooth :1,
ethernet :1,
unused :5;
};
} net_types_t;
static net_types_t net = {0};
#endif
#if DRIVER_SPINDLE_ENABLE
static spindle_id_t spindle_id = -1;
#endif
#if DRIVER_SPINDLE_PWM_ENABLE
static bool pwmEnabled = false;
static spindle_pwm_t spindle_pwm;
#endif
static pio_steps_t pio_steps = {.delay = 20, .length = 100};
static uint stepper_timer_sm, stepper_timer_sm_offset;
static uint16_t pulse_length, pulse_delay;
static bool IOInitDone = false;
static status_code_t (*on_unknown_sys_command)(uint_fast16_t state, char *line, char *lcline);
static volatile uint32_t elapsed_ticks = 0;
static probe_state_t probe = { .connected = On };
static pin_group_pins_t limit_inputs;
#if SAFETY_DOOR_BIT
static input_signal_t *safety_door;
#endif
#if IOEXPAND_ENABLE
static ioexpand_t io_expander = {0};
#endif
#ifdef NEOPIXELS_PIN
neopixel_cfg_t neopixel = { .intensity = 255 };
#endif
#include "grbl/stepdir_map.h"
static input_signal_t *irq_pins[32] = {0};
static periph_signal_t *periph_pins = NULL;
static input_signal_t inputpin[] = {
#ifdef RESET_PIN
#if ESTOP_ENABLE
{ .id = Input_EStop, .port = GPIO_INPUT, .pin = RESET_PIN, .group = PinGroup_Control },
#else
{ .id = Input_Reset, .port = GPIO_INPUT, .pin = RESET_PIN, .group = PinGroup_Control },
#endif
#endif
#ifdef FEED_HOLD_PIN
{ .id = Input_FeedHold, .port = GPIO_INPUT, .pin = FEED_HOLD_PIN, .group = PinGroup_Control },
#endif
#ifdef CYCLE_START_PIN
{ .id = Input_CycleStart, .port = GPIO_INPUT, .pin = CYCLE_START_PIN, .group = PinGroup_Control },
#endif
#if SAFETY_DOOR_BIT
{ .id = Input_SafetyDoor, .port = GPIO_INPUT, .pin = SAFETY_DOOR_PIN, .group = PinGroup_Control },
#endif
#ifdef LIMITS_OVERRIDE_PIN
{ .id = Input_LimitsOverride, .port = GPIO_INPUT, .pin = LIMITS_OVERRIDE_PIN, .group = PinGroup_Control },
#endif
{ .id = Input_LimitX, .port = GPIO_INPUT, .pin = X_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef X2_LIMIT_PIN
{ .id = Input_LimitX_2, .port = GPIO_INPUT, .pin = X2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
{ .id = Input_LimitY, .port = GPIO_INPUT, .pin = Y_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef Y2_LIMIT_PIN
{ .id = Input_LimitY_Max, .port = GPIO_INPUT, .pin = Y2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
{ .id = Input_LimitZ, .port = GPIO_INPUT, .pin = Z_LIMIT_PIN, .group = PinGroup_Limit },
#ifdef Z2_LIMIT_PIN
{ .id = Input_LimitZ_Max, .port = GPIO_INPUT, .pin = Z2_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef A_LIMIT_PIN
{ .id = Input_LimitA, .port = GPIO_INPUT, .pin = A_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef B_LIMIT_PIN
{ .id = Input_LimitB, .port = GPIO_INPUT, .pin = B_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifdef C_LIMIT_PIN
{ .id = Input_LimitC, .port = GPIO_INPUT, .pin = C_LIMIT_PIN, .group = PinGroup_Limit },
#endif
#ifndef AUX_DEVICES
#ifdef PROBE_PIN
{ .id = Input_Probe, .port = GPIO_INPUT, .pin = PROBE_PIN, .group = PinGroup_Probe },
#endif
#if MPG_MODE_PIN
{ .id = Input_MPGSelect, .port = GPIO_INPUT, .pin = MPG_MODE_PIN, .group = PinGroup_MPG },
#endif
#if I2C_STROBE_ENABLE && defined(I2C_STROBE_PIN)
{ .id = Input_I2CStrobe, .port = GPIO_INPUT, .pin = I2C_STROBE_PIN, .group = PinGroup_I2C },
#endif
#endif // AUX_DEVICES
#ifdef SPI_IRQ_PIN
{ .id = Input_SPIIRQ, .port = GPIO_INPUT, .pin = SPI_IRQ_PIN, .group = PinGroup_SPI },
#endif
#ifdef AUXINPUT0_PIN
{ .id = Input_Aux0, .port = GPIO_INPUT, .pin = AUXINPUT0_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT1_PIN
{ .id = Input_Aux1, .port = GPIO_INPUT, .pin = AUXINPUT1_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT2_PIN
{ .id = Input_Aux2, .port = GPIO_INPUT, .pin = AUXINPUT2_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT3_PIN
{ .id = Input_Aux3, .port = GPIO_INPUT, .pin = AUXINPUT3_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT4_PIN
{ .id = Input_Aux4, .port = GPIO_INPUT, .pin = AUXINPUT4_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT5_PIN
{ .id = Input_Aux5, .port = GPIO_INPUT, .pin = AUXINPUT5_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT6_PIN
{ .id = Input_Aux6, .port = GPIO_INPUT, .pin = AUXINPUT6_PIN, .group = PinGroup_AuxInput },
#endif
#ifdef AUXINPUT7_PIN
{ .id = Input_Aux7, .port = GPIO_INPUT, .pin = AUXINPUT7_PIN, .group = PinGroup_AuxInput }
#endif
};
#if STEP_PORT == GPIO_SR8
#if N_ABC_MOTORS > 1
#error "Axis configuration is not supported!"
#endif
#define X_STEP_PIN 0
#define Y_STEP_PIN 1
#define Z_STEP_PIN 2
#ifdef X2_STEP_PORT
#undef X2_STEP_PIN
#define X2_STEP_PIN 3
#endif
#ifdef Y2_STEP_PORT
#undef Y2_STEP_PIN
#define Y2_STEP_PIN 3
#endif
#ifdef Z2_STEP_PORT
#undef Z2_STEP_PIN
#define Z2_STEP_PIN 3
#endif
#ifdef A_STEP_PORT
#undef A_STEP_PIN
#define A_STEP_PIN 3
#endif
#endif
#if DIRECTION_PORT == GPIO_SR8
#if N_ABC_MOTORS > 1
#error "Axis configuration is not supported!"
#endif
#define X_DIRECTION_PIN 4
#define Y_DIRECTION_PIN 5
#define Z_DIRECTION_PIN 6
#ifdef X2_DIRECTION_PORT
#undef X2_DIRECTION_PIN
#define X2_DIRECTION_PIN 7
#endif
#ifdef Y2_DIRECTION_PORT
#undef Y2_DIRECTION_PIN
#define Y2_DIRECTION_PIN 7
#endif
#ifdef Z2_DIRECTION_PORT
#undef Z2_DIRECTION_PIN
#define Z2_DIRECTION_PIN 7
#endif
#ifdef A_DIRECTION_PORT
#undef A_DIRECTION_PIN
#define A_DIRECTION_PIN 7
#endif
#endif
#if ENABLE_PORT == GPIO_SR16
#if N_ABC_MOTORS > 1
#error "Axis configuration is not supported!"
#endif
#define X_ENABLE_PIN 0
#define Y_ENABLE_PIN 1
#define Z_ENABLE_PIN 2
#ifdef X2_ENABLE_PORT
#undef X2_ENABLE_PIN
#define X2_ENABLE_PIN 3
#endif
#ifdef Y2_ENABLE_PORT
#undef Y2_ENABLE_PIN
#define Y2_ENABLE_PIN 3
#endif
#ifdef Z2_ENABLE_PORT
#undef Z2_ENABLE_PIN
#define Z2_ENABLE_PIN 3
#endif
#ifdef A_ENABLE_PORT
#undef A_ENABLE_PIN
#define A_ENABLE_PIN 3
#endif
#endif
#ifndef SPI_RST_PORT
#define SPI_RST_PORT GPIO_OUTPUT
#endif
static output_signal_t outputpin[] = {
{ .id = Output_StepX, .port = STEP_PORT, .pin = X_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE } },
{ .id = Output_StepY, .port = STEP_PORT, .pin = Y_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE} },
{ .id = Output_StepZ, .port = STEP_PORT, .pin = Z_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE } },
#ifdef A_STEP_PIN
{ .id = Output_StepA, .port = STEP_PORT, .pin = A_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE } },
#endif
#ifdef B_STEP_PIN
{ .id = Output_StepB, .port = STEP_PORT, .pin = B_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE } },
#endif
#ifdef C_STEP_PIN
{ .id = Output_StepC, .port = STEP_PORT, .pin = C_STEP_PIN, .group = PinGroup_StepperStep, .mode = {STEP_PINMODE } },
#endif
#ifdef X2_STEP_PIN
{ .id = Output_StepX_2, .port = STEP_PORT, .pin = X2_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE } },
#endif
#ifdef Y2_STEP_PIN
{ .id = Output_StepY_2, .port = STEP_PORT, .pin = Y2_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE } },
#endif
#ifdef Z2_STEP_PIN
{ .id = Output_StepZ_2, .port = STEP_PORT, .pin = Z2_STEP_PIN, .group = PinGroup_StepperStep, .mode = { STEP_PINMODE } },
#endif
{ .id = Output_DirX, .port = DIRECTION_PORT, .pin = X_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
{ .id = Output_DirY, .port = DIRECTION_PORT, .pin = Y_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
{ .id = Output_DirZ, .port = DIRECTION_PORT, .pin = Z_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
#ifdef A_DIRECTION_PIN
{ .id = Output_DirA, .port = DIRECTION_PORT, .pin = A_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
#endif
#ifdef B_DIRECTION_PIN
{ .id = Output_DirB, .port = DIRECTION_PORT, .pin = B_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
#endif
#ifdef C_DIRECTION_PIN
{ .id = Output_DirC, .port = DIRECTION_PORT, .pin = C_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
#endif
#ifdef X2_DIRECTION_PIN
{ .id = Output_DirX_2, .port = DIRECTION_PORT, .pin = X2_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = {DIRECTION_PINMODE } },
#endif
#ifdef Y2_DIRECTION_PIN
{ .id = Output_DirY_2, .port = DIRECTION_PORT, .pin = Y2_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
#endif
#ifdef Z2_DIRECTION_PIN
{ .id = Output_DirZ_2, .port = DIRECTION_PORT, .pin = Z2_DIRECTION_PIN, .group = PinGroup_StepperDir, .mode = { DIRECTION_PINMODE } },
#endif
#if !(TRINAMIC_ENABLE && TRINAMIC_I2C)
#ifndef STEPPERS_ENABLE_PIN
#ifdef X_ENABLE_PIN
{ .id = Output_StepperEnableX, .port = ENABLE_PORT, .pin = X_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef Y_ENABLE_PIN
{ .id = Output_StepperEnableY, .port = ENABLE_PORT, .pin = Y_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef Z_ENABLE_PIN
{ .id = Output_StepperEnableZ, .port = ENABLE_PORT, .pin = Z_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef X2_ENABLE_PIN
{ .id = Output_StepperEnableX, .port = ENABLE_PORT, .pin = X2_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef Y2_ENABLE_PIN
{ .id = Output_StepperEnableY, .port = ENABLE_PORT, .pin = Y2_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef Z2_ENABLE_PIN
{ .id = Output_StepperEnableZ, .port = ENABLE_PORT, .pin = Z2_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef A_ENABLE_PIN
{ .id = Output_StepperEnableA, .port = ENABLE_PORT, .pin = A_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef B_ENABLE_PIN
{ .id = Output_StepperEnableB, .port = ENABLE_PORT, .pin = B_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#ifdef C_ENABLE_PIN
{ .id = Output_StepperEnableC, .port = ENABLE_PORT, .pin = C_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#else // STEPPERS_ENABLE_PIN
{ .id = Output_StepperEnable, .port = ENABLE_PORT, .pin = STEPPERS_ENABLE_PIN, .group = PinGroup_StepperEnable, .mode = { STEPPERS_ENABLE_PINMODE } },
#endif
#endif // !(TRINAMIC_ENABLE && TRINAMIC_I2C)
#if defined(SPINDLE_PWM_PORT) && !defined(AUX_CONTROLS_OUT)
{ .id = Output_SpindlePWM, .port = SPINDLE_PWM_PORT, .pin = SPINDLE_PWM_PIN, .group = PinGroup_SpindlePWM },
#endif
#ifdef RTS_PIN
{ .id = Output_RTS, .port = GPIO_OUTPUT, .pin = RTS_PIN, .group = PinGroup_UART },
#endif
#ifdef SD_CS_PIN
{ .id = Output_SdCardCS, .port = GPIO_OUTPUT, .pin = SD_CS_PIN, .group = PinGroup_SdCard },
#endif
#ifdef SPI_CS_PIN
{ .id = Output_SPICS, .port = GPIO_OUTPUT, .pin = SPI_CS_PIN, .group = PinGroup_SPI },
#endif
#ifdef SPI_RST_PIN
{ .id = Output_SPIRST, .port = SPI_RST_PORT, .pin = SPI_RST_PIN, .group = PinGroup_SPI },
#endif
#ifdef SD_SHIFT_REGISTER // SD_SHIFT_REGISTER pin definitions - for $pins command only
{ .id = Output_SpindleOn, .port = GPIO_SR16, .pin = 4, .group = PinGroup_SpindleControl },
{ .id = Output_SpindleDir, .port = GPIO_SR16, .pin = 5, .group = PinGroup_SpindleControl },
{ .id = Output_CoolantFlood, .port = GPIO_SR16, .pin = 6, .group = PinGroup_Coolant },
{ .id = Output_CoolantMist, .port = GPIO_SR16, .pin = 7, .group = PinGroup_Coolant },
{ .id = Output_Aux0, .port = GPIO_SR16, .pin = 8, .group = PinGroup_AuxOutput },
{ .id = Output_Aux1, .port = GPIO_SR16, .pin = 9, .group = PinGroup_AuxOutput },
{ .id = Output_Aux2, .port = GPIO_SR16, .pin = 10, .group = PinGroup_AuxOutput },
{ .id = Output_Aux3, .port = GPIO_SR16, .pin = 11, .group = PinGroup_AuxOutput },
{ .id = Output_Aux4, .port = GPIO_SR16, .pin = 12, .group = PinGroup_AuxOutput },
{ .id = Output_Aux5, .port = GPIO_SR16, .pin = 13, .group = PinGroup_AuxOutput },
{ .id = Output_Aux6, .port = GPIO_SR16, .pin = 14, .group = PinGroup_AuxOutput },
#ifdef AUXOUTPUT7_PORT
{ .id = Output_Aux7, .port = AUXOUTPUT7_PORT, .pin = AUXOUTPUT7_PIN, .group = PinGroup_AuxOutput},
#endif
{ .id = Output_SPIRST, .port = GPIO_SR16, .pin = 15, .group = PinGroup_SPI },
#else // !SD_SHIFT_REGISTER
#ifndef AUX_CONTROLS_OUT
#ifdef SPINDLE_ENABLE_PIN
{ .id = Output_SpindleOn, .port = SPINDLE_PORT, .pin = SPINDLE_ENABLE_PIN, .group = PinGroup_SpindleControl},
#endif
#ifdef SPINDLE_DIRECTION_PIN
{ .id = Output_SpindleDir, .port = SPINDLE_PORT, .pin = SPINDLE_DIRECTION_PIN, .group = PinGroup_SpindleControl},
#endif
#endif // AUX_CONTROLS_OUT
#ifdef COOLANT_FLOOD_PIN
{ .id = Output_CoolantFlood, .port = COOLANT_PORT, .pin = COOLANT_FLOOD_PIN, .group = PinGroup_Coolant},
#endif
#ifdef COOLANT_MIST_PIN
{ .id = Output_CoolantMist, .port = COOLANT_PORT, .pin = COOLANT_MIST_PIN, .group = PinGroup_Coolant},
#endif
#ifdef LED_PIN
{ .id = Output_LED, .port = GPIO_OUTPUT, .pin = LED_PIN, .group = PinGroup_LED },
#endif
#ifdef LED_R_PIN
{ .id = Output_LED_R, .port = GPIO_OUTPUT, .pin = LED_R_PIN, .group = PinGroup_LED },
#endif
#ifdef LED_G_PIN
{ .id = Output_LED_G, .port = GPIO_OUTPUT, .pin = LED_G_PIN, .group = PinGroup_LED },
#endif
#ifdef LED_B_PIN
{ .id = Output_LED_B, .port = GPIO_OUTPUT, .pin = LED_B_PIN, .group = PinGroup_LED },
#endif
#ifdef LED_W_PIN
{ .id = Output_LED_W, .port = GPIO_OUTPUT, .pin = LED_W_PIN, .group = PinGroup_LED },
#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
#endif
#ifdef AUXOUTPUT0_PWM_PIN
{ .id = Output_Analog_Aux0, .port = GPIO_OUTPUT, .pin = AUXOUTPUT0_PWM_PIN, .group = PinGroup_AuxOutputAnalog, .mode = { PINMODE_PWM } },
#endif
#ifdef AUXOUTPUT1_PWM_PIN
{ .id = Output_Analog_Aux1, .port = GPIO_OUTPUT, .pin = AUXOUTPUT1_PWM_PIN, .group = PinGroup_AuxOutputAnalog, .mode = { PINMODE_PWM } },
#endif
#ifdef AUXOUTPUT2_PWM_PIN
{ .id = Output_Analog_Aux2, .port = GPIO_OUTPUT, .pin = AUXOUTPUT2_PWM_PIN, .group = PinGroup_AuxOutputAnalog, .mode = { PINMODE_PWM } },
#endif
#ifdef AUXOUTPUT3_PWM_PIN
{ .id = Output_Analog_Aux3, .port = GPIO_OUTPUT, .pin = AUXOUTPUT3_PWM_PIN, .group = PinGroup_AuxOutputAnalog, .mode = { PINMODE_PWM } },
#endif
};
#ifndef I2C_STROBE_BIT
#define I2C_STROBE_BIT 0
#endif
#ifndef SPI_IRQ_BIT
#define SPI_IRQ_BIT 0
#endif
#define NVIC_HIGH_LEVEL_PRIORITY 0xC0
#define NVIC_MEDIUM_LEVEL_PRIORITY 0x80
#define NVIC_LOW_LEVEL_PRIORITY 0x40
#ifndef DEBOUNCE_DELAY
#define DEBOUNCE_DELAY 40 // ms
#endif
#if AUX_CONTROLS_ENABLED
static uint8_t probe_port;
static pin_debounce_t debounce = {0};
static void aux_irq_handler (uint8_t port, bool state);
#endif
#if SD_SHIFT_REGISTER
static step_dir_sr_t sd_sr;
#endif
#if OUT_SHIFT_REGISTER
static uint32_t out_sr_sm;
static output_sr_t out_sr;
#endif
static void systick_handler(void);
static void stepper_int_handler(void);
static void gpio_int_handler(uint gpio, uint32_t events);
#if I2C_STROBE_ENABLE || SPI_IRQ_BIT
#if I2C_STROBE_ENABLE
static driver_irq_handler_t i2c_strobe = { .type = IRQ_I2C_Strobe };
#endif
#if SPI_IRQ_BIT
static driver_irq_handler_t spi_irq = { .type = IRQ_SPI };
#endif
static bool irq_claim (irq_type_t irq, uint_fast8_t id, irq_callback_ptr handler)
{
bool ok = false;
switch(irq) {
#if I2C_STROBE_ENABLE
case IRQ_I2C_Strobe:
if((ok = i2c_strobe.callback == NULL))
i2c_strobe.callback = handler;
break;
#endif
#if SPI_IRQ_BIT
case IRQ_SPI:
if((ok = spi_irq.callback == NULL))
spi_irq.callback = handler;
break;
#endif
default:
break;
}
return ok;
}
#endif // I2C_STROBE_ENABLE || SPI_IRQ_BIT
static int64_t delay_callback (alarm_id_t id, void *callback)
{
((delay_callback_ptr)callback)();
return 0;
}
static void driver_delay (uint32_t ms, delay_callback_ptr callback)
{
if (ms > 0) {
if (callback)
add_alarm_in_ms(ms, delay_callback, callback, false);
else {
uint32_t delay = ms * 1000, start = timer_hw->timerawl;
while (timer_hw->timerawl - start < delay)
grbl.on_execute_delay(state_get());
}
}
else if (callback)
callback();
tight_loop_contents();
}
//************************* STEPPER *************************//
// Enable/disable stepper motors
static void stepperEnable (axes_signals_t enable, bool hold)
{
enable.mask ^= settings.steppers.enable_invert.mask;
#if TRINAMIC_ENABLE && TRINAMIC_I2C
axes_signals_t tmc_enable = trinamic_stepper_enable(enable);
#elif ENABLE_PORT == GPIO_OUTPUT
#ifndef STEPPERS_ENABLE_PIN
gpio_put(X_ENABLE_PIN, enable.x);
#ifdef Y_ENABLE_PIN
gpio_put(Y_ENABLE_PIN, enable.y);
#endif
gpio_put(Z_ENABLE_PIN, enable.z);
#ifdef X2_ENABLE_PIN
gpio_put(X2_ENABLE_PIN, enable.x);
#endif
#ifdef Y2_ENABLE_PIN
gpio_put(Y2_ENABLE_PIN, enable.y);
#endif
#ifdef Z2_ENABLE_PIN
gpio_put(Z2_ENABLE_PIN, enable.z);
#endif
#ifdef A_ENABLE_PIN
gpio_put(A_ENABLE_PIN, enable.a);
#endif
#ifdef B_ENABLE_PIN
gpio_put(B_ENABLE_PIN, enable.b);
#endif
#ifdef C_ENABLE_PIN
gpio_put(C_ENABLE_PIN, enable.c);
#endif
#else // STEPPERS_ENABLE_PIN
gpio_put(STEPPERS_ENABLE_PIN, enable.x);
#endif
#elif ENABLE_PORT == GPIO_SR16
out_sr.x_ena = enable.x;
#ifdef X2_ENABLE_PIN
out_sr.m3_ena = enable.x;
#endif
out_sr.y_ena = enable.y;
#ifdef Y2_ENABLE_PIN
out_sr.m3_ena = enable.y;
#endif
out_sr.z_ena = enable.z;
#ifdef Z2_ENABLE_PIN
out_sr.m3_ena = enable.z;
#endif
#ifdef A_ENABLE_PIN
out_sr.m3_ena = enable.a;
#endif
out_sr16_write(pio1, out_sr_sm, out_sr.value);
#elif ENABLE_PORT == GPIO_IOEXPAND
#ifdef STEPPERS_DISABLEX_PIN
ioex_out(STEPPERS_DISABLEX_PIN) = enable.x;
#endif
#ifdef STEPPERS_DISABLEZ_PIN
ioex_out(STEPPERS_DISABLEZ_PIN) = enable.z;
#endif
ioexpand_out(io_expander);
#endif
}
// Starts stepper driver ISR timer and forces a stepper driver interrupt callback
static void stepperWakeUp (void)
{
hal.stepper.enable((axes_signals_t){AXES_BITMASK}, false);
stepper_timer_set_period(pio1, stepper_timer_sm, stepper_timer_sm_offset, hal.f_step_timer / 500); // ~2ms delay to allow drivers time to wake up.
irq_set_enabled(PIO1_IRQ_0, true);
}
// Disables stepper driver interrupts
static void stepperGoIdle (bool clear_signals)
{
irq_set_enabled(PIO1_IRQ_0, false);
stepper_timer_stop(pio1, stepper_timer_sm);
}
// Sets up stepper driver interrupt timeout, "Normal" version
static void __not_in_flash_func(stepperCyclesPerTick)(uint32_t cycles_per_tick)
{
stepper_timer_set_period(pio1, stepper_timer_sm, stepper_timer_sm_offset, cycles_per_tick < 1000000 ? cycles_per_tick : 1000000);
}
#ifdef SQUARING_ENABLED
static axes_signals_t motors_1 = {AXES_BITMASK}, motors_2 = {AXES_BITMASK};
// Set stepper pulse output pins
// NOTE: step_outbits are: bit0 -> X, bit1 -> Y, bit2 -> Z...
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;
step_outbits_1.mask = (step_outbits_1.mask & motors_1.mask) ^ settings.steppers.step_invert.mask;
#if STEP_PORT == GPIO_PIO_1
pio_steps.set = step_outbits_1.x;
pio_steps.reset = settings.steppers.step_invert.x;
step_pulse_generate(pio1, x_step_sm, pio_steps.value);
#ifdef X2_STEP_PIN
pio_steps.set = step_outbits_2.x;
step_pulse_generate(x2_step_pio, x2_step_sm, pio_steps.value);
#endif
pio_steps.set = step_outbits_1.y;
pio_steps.reset = settings.steppers.step_invert.y;
step_pulse_generate(pio1, y_step_sm, pio_steps.value);
#ifdef Y2_STEP_PIN
pio_steps.set = step_outbits_2.y;
step_pulse_generate(y2_step_pio, y2_step_sm, pio_steps.value);
#endif
pio_steps.set = step_outbits_1.z;
pio_steps.reset = settings.steppers.step_invert.z;
step_pulse_generate(z_step_pio, z_step_sm, pio_steps.value);
#ifdef Z2_STEP_PIN
pio_steps.set = step_outbits_2.z;
step_pulse_generate(z2_step_pio, z2_step_sm, pio_steps.value);
#endif
#ifdef A_STEP_PIN
pio_steps.set = step_outbits_1.a;
pio_steps.reset = settings.steppers.step_invert.a;
step_pulse_generate(a_step_pio, a_step_sm, pio_steps.value);
#endif
#ifdef B_STEP_PIN
pio_steps.set = step_outbits_1.b;
pio_steps.reset = settings.steppers.step_invert.b;
step_pulse_generate(b_step_pio, b_step_sm, pio_steps.value);
#endif
#ifdef C_STEP_PIN
pio_steps.set = step_outbits_1.c;
pio_steps.reset = settings.steppers.step_invert.c;
step_pulse_generate(c_step_pio, c_step_sm, pio_steps.value);
#endif
#elif STEP_PORT == GPIO_PIO
pio_steps.set = step_outbits_1.mask & 0x07;
#ifdef X2_STEP_PIN
if (step_outbits_2.x)
pio_steps.set |= (1 << (X2_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef Y2_STEP_PIN
if (step_outbits_2.y)
pio_steps.set |= (1 << (Y2_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef Z2_STEP_PIN
if (step_outbits_2.z)
pio_steps.set |= (1 << (Z2_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef A_STEP_PIN
if (step_outbits_1.a)
pio_steps.set |= (1 << (A_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef B_STEP_PIN
if (step_outbits_1.b)
pio_steps.set |= (1 << (B_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef C_STEP_PIN
if (step_outbits_1.c)
pio_steps.set |= (1 << (C_STEP_PIN - STEP_PINS_BASE));
#endif
step_pulse_generate(pio0, 0, pio_steps.value);
#elif STEP_PORT == GPIO_SR8
sd_sr.set.x_step = step_outbits_1.x;
#ifdef X2_STEP_PIN
sd_sr.set.m3_step = step_outbits_2.x;
#endif
sd_sr.set.y_step = step_outbits_1.y;
#ifdef Y2_STEP_PIN
sd_sr.set.m3_step = step_outbits_2.y;
#endif
sd_sr.set.z_step = step_outbits_1.z;
#ifdef Z2_STEP_PIN
sd_sr.set.m3_step = step_outbits_2.z;
#endif
#ifdef A_STEP_PIN
sd_sr.set.m3_step = step_outbits_1.a;
#endif
step_dir_sr4_write(pio0, 0, sd_sr.value);
#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_ENABLED
// Set stepper pulse output pins
// NOTE: step_outbits are: bit0 -> X, bit1 -> Y, bit2 -> Z...
inline static __attribute__((always_inline)) void stepperSetStepOutputs (axes_signals_t step_outbits)
{
step_outbits.mask ^= settings.steppers.step_invert.mask;
#if STEP_PORT == GPIO_PIO_1
pio_steps.set = step_outbits.x;
pio_steps.reset = settings.steppers.step_invert.x;
step_pulse_generate(pio1, x_step_sm, pio_steps.value);
#ifdef X2_STEP_PIN
step_pulse_generate(x2_step_pio, x2_step_sm, pio_steps.value);
#endif
pio_steps.set = step_outbits.y;
pio_steps.reset = settings.steppers.step_invert.y;
step_pulse_generate(pio1, y_step_sm, pio_steps.value);
#ifdef Y2_STEP_PIN
step_pulse_generate(y2_step_pio, y2_step_sm, pio_steps.value);
#endif
pio_steps.set = step_outbits.z;
pio_steps.reset = settings.steppers.step_invert.z;
step_pulse_generate(z_step_pio, z_step_sm, pio_steps.value);
#ifdef Z2_STEP_PIN
step_pulse_generate(z2_step_pio, z2_step_sm, pio_steps.value);
#endif
#ifdef A_STEP_PIN
pio_steps.set = step_outbits.a;
pio_steps.reset = settings.steppers.step_invert.a;
step_pulse_generate(a_step_pio, a_step_sm, pio_steps.value);
#endif
#ifdef B_STEP_PIN
pio_steps.set = step_outbits.b;
pio_steps.reset = settings.steppers.step_invert.b;
step_pulse_generate(b_step_pio, b_step_sm, pio_steps.value);
#endif
#ifdef C_STEP_PIN
pio_steps.set = step_outbits.c;
pio_steps.reset = settings.steppers.step_invert.c;
step_pulse_generate(c_step_pio, c_step_sm, pio_steps.value);
#endif
#elif STEP_PORT == GPIO_PIO
pio_steps.set = step_outbits.mask & 0x07;
#ifdef X2_STEP_PIN
if (step_outbits.x)
pio_steps.set |= (1 << (X2_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef Y2_STEP_PIN
if (step_outbits.y)
pio_steps.set |= (1 << (Y2_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef Z2_STEP_PIN
if (step_outbits.z)
pio_steps.set |= (1 << (Z2_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef A_STEP_PIN
if (step_outbits.a)
pio_steps.set |= (1 << (A_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef B_STEP_PIN
if (step_outbits.b)
pio_steps.set |= (1 << (B_STEP_PIN - STEP_PINS_BASE));
#endif
#ifdef C_STEP_PIN
if (step_outbits.c)
pio_steps.set |= (1 << (C_STEP_PIN - STEP_PINS_BASE));
#endif
step_pulse_generate(pio0, 0, pio_steps.value);
#elif STEP_PORT == GPIO_SR8
sd_sr.set.x_step = step_outbits.x;
#ifdef X2_STEP_PIN
sd_sr.set.m3_step = step_outbits.x;
#endif
sd_sr.set.y_step = step_outbits.y;
#ifdef Y2_STEP_PIN
sd_sr.set.m3_step = step_outbits.y;
#endif
sd_sr.set.z_step = step_outbits.z;
#ifdef Z2_STEP_PIN
sd_sr.set.m3_step = step_outbits.z;
#endif
#ifdef A_STEP_PIN
sd_sr.set.m3_step = step_outbits.a;
#endif
step_dir_sr4_write(pio0, 0, sd_sr.value);
#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 output pins
// NOTE: see note for stepperSetStepOutputs()
//inline static __attribute__((always_inline)) void stepperSetDirOutputs (axes_signals_t dir_outbits)
static void stepperSetDirOutputs (axes_signals_t dir_outbits)
{
#if DIRECTION_PORT == GPIO_OUTPUT
#if DIRECTION_OUTMODE == GPIO_MAP
gpio_put_masked(DIRECTION_MASK, dir_outmap[dir_outbits.mask]);
#ifdef X2_DIRECTION_PIN
DIGITAL_OUT(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_PIN, (dir_outbits.y ^ settings.steppers.dir_invert.y) ^ settings.steppers.ganged_dir_invert.y);
#endif
#ifdef Z2_DIRECTION_PIN