-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.c
1988 lines (1636 loc) · 66.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 - An embedded CNC Controller with rs274/ngc (g-code) support
Driver code for Texas Instruments Tiva C (TM4C123GH6PM) ARM processor
Part of grblHAL
Copyright (c) 2016-2024 Terje Io
Some parts
Copyright (c) 2011-2015 Sungeun K. Jeon
Copyright (c) 2009-2011 Simen Svale Skogsrud
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 <stdlib.h>
#include <string.h>
#include "driver.h"
#include "eeprom.h"
#include "serial.h"
#define AUX_DEVICES // until all drivers are converted?
#include "grbl/machine_limits.h"
#include "grbl/protocol.h"
#include "grbl/state_machine.h"
#include "grbl/pin_bits_masks.h"
#if TRINAMIC_ENABLE
static void trinamic_warn_isr (void);
#if !I2C_STROBE_ENABLE
static void trinamic_diag1_isr (void);
#endif
#endif
#if I2C_ENABLE
#include "i2c.h"
#endif
#if ATC_ENABLE
#include "atc.h"
#endif
// prescale step counter to 20Mhz (80 / (STEPPER_DRIVER_PRESCALER + 1))
#define STEPPER_DRIVER_PRESCALER 3
#if PWM_RAMPED
#define SPINDLE_RAMP_STEP_INCR 20 // timer compare register change per ramp step
#define SPINDLE_RAMP_STEP_TIME 2 // ms
typedef struct {
volatile uint32_t ms_cfg;
volatile uint32_t delay_ms;
int32_t pwm_current;
int32_t pwm_target;
int32_t pwm_step;
} pwm_ramp_t;
static pwm_ramp_t pwm_ramp;
#endif
#if PPI_ENABLE
#include "laser/ppi.h"
static void ppi_timeout_isr (void);
#endif
#include "grbl/motor_pins.h"
#define DEBOUNCE_QUEUE 8 // Must be a power of 2
typedef struct {
volatile uint_fast8_t head;
volatile uint_fast8_t tail;
input_signal_t *signal[DEBOUNCE_QUEUE];
} debounce_queue_t;
typedef struct {
uint32_t port;
void (*handler)(void);
uint32_t count;
input_signal_t pins[8];
} irq_handler_t;
static periph_signal_t *periph_pins = NULL;
#if AUX_CONTROLS_ENABLED
static uint8_t probe_port;
static pin_debounce_t debounce;
#if SAFETY_DOOR_ENABLE
static input_signal_t *door_pin;
#endif
static void aux_irq_handler (uint8_t port, bool state);
#endif
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_BIT
{ .id = Input_SafetyDoor, .port = SAFETY_DOOR_PORT, .pin = SAFETY_DOOR_PIN, .group = PinGroup_Control },
#endif
#ifdef AUX_DEVICES
{ .id = Input_Probe, .port = PROBE_PORT, .pin = PROBE_PIN, .group = PinGroup_Probe },
#endif
#ifdef KEYPAD_IRQ_PIN
{ .id = Input_KeypadStrobe, .port = KEYPAD_PORT, .pin = KEYPAD_IRQ_PIN, .group = PinGroup_Keypad },
#endif
#ifdef MPG_MODE_PIN
{ .id = Input_ModeSelect, .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 },
{ .id = Input_LimitY, .port = Y_LIMIT_PORT, .pin = Y_LIMIT_PIN, .group = PinGroup_Limit },
{ .id = Input_LimitZ, .port = Z_LIMIT_PORT, .pin = Z_LIMIT_PIN, .group = PinGroup_Limit }
#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 LIMITS_OVERRIDE_BIT
, { .id = Input_LimitsOverride, .port = LIMITS_OVERRIDE_PORT, .pin = LIMITS_OVERRIDE_PIN, .group = PinGroup_Limit }
#endif
// , { .id = Input_SpindleIndex, .port = RPM_INDEX_PORT, .pin = RPM_INDEX_PIN, .group = PinGroup_QEI_Index }
// 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
#ifdef AUXINPUT7_PIN
, { .id = Input_Aux7, .port = AUXINPUT7_PORT, .pin = AUXINPUT7_PIN, .group = PinGroup_AuxInput }
#endif
};
static output_signal_t outputpin[] = {
{ .id = Output_StepX, .port = X_STEP_PORT, .pin = X_STEP_PIN, .group = PinGroup_StepperStep },
{ .id = Output_StepY, .port = Y_STEP_PORT, .pin = Y_STEP_PIN, .group = PinGroup_StepperStep },
{ .id = Output_StepZ, .port = Z_STEP_PORT, .pin = Z_STEP_PIN, .group = PinGroup_StepperStep },
#ifdef A_AXIS
{ .id = Output_StepA, .port = A_STEP_PORT, .pin = A_STEP_PIN, .group = PinGroup_StepperStep },
#endif
#ifdef B_AXIS
{ .id = Output_StepB, .port = B_STEP_PORT, .pin = B_STEP_PIN, .group = PinGroup_StepperStep },
#endif
#ifdef C_AXIS
{ .id = Output_StepC, .port = B_STEP_PORT, .pin = C_STEP_PIN, .group = PinGroup_StepperStep },
#endif
{ .id = Output_DirX, .port = X_DIRECTION_PORT, .pin = X_DIRECTION_PIN, .group = PinGroup_StepperDir },
{ .id = Output_DirY, .port = Y_DIRECTION_PORT, .pin = Y_DIRECTION_PIN, .group = PinGroup_StepperDir },
{ .id = Output_DirZ, .port = Z_DIRECTION_PORT, .pin = Z_DIRECTION_PIN, .group = PinGroup_StepperDir },
#ifdef A_AXIS
{ .id = Output_DirA, .port = A_DIRECTION_PORT, .pin = A_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#ifdef B_AXIS
{ .id = Output_DirB, .port = B_DIRECTION_PORT, .pin = B_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#ifdef C_AXIS
{ .id = Output_DirC, .port = C_DIRECTION_PORT, .pin = C_DIRECTION_PIN, .group = PinGroup_StepperDir },
#endif
#if CNC_BOOSTERPACK_A4998
{ .id = Output_StepperPower, .port = STEPPERS_VDD_PORT, .pin = STEPPERS_VDD_PIN, .group = PinGroup_StepperPower },
#endif
#if !TRINAMIC_ENABLE
#ifdef STEPPERS_ENABLE_PORT
{ .id = Output_StepperEnable, .port = STEPPERS_ENABLE_PORT, .pin = STEPPERS_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef XY_ENABLE_PORT
{ .id = Output_StepperEnableXY, .port = XY_ENABLE_PORT, .pin = XY_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef Z_ENABLE_PORT
{ .id = Output_StepperEnableZ, .port = Z_ENABLE_PORT, .pin = Z_ENABLE_PIN, .group = PinGroup_StepperEnable },
#endif
#ifdef A_ENABLE_PORT
{ .id = Output_StepperEnableA, .port = A_ENABLE_PORT, .pin = A_ENABLE_PIN, .group = PinGroup_StepperEnable, },
#endif
#ifdef B_ENABLE_PORT
{ .id = Output_StepperEnableB, .port = B_ENABLE_PORT, .pin = B_ENABLE_PIN, .group = PinGroup_StepperEnable, },
#endif
#ifdef C_ENABLE_PORT
{ .id = Output_StepperEnableC, .port = C_ENABLE_PORT, .pin = C_ENABLE_PIN, .group = PinGroup_StepperEnable, },
#endif
#endif
{ .id = Output_SpindleOn, .port = SPINDLE_ENABLE_PORT, .pin = SPINDLE_ENABLE_PIN, .group = PinGroup_SpindleControl },
{ .id = Output_SpindleDir, .port = SPINDLE_DIRECTION_PORT, .pin = SPINDLE_DIRECTION_PIN, .group = PinGroup_SpindleControl },
{ .id = Output_CoolantFlood, .port = COOLANT_FLOOD_PORT, .pin = COOLANT_FLOOD_PIN, .group = PinGroup_Coolant },
{ .id = Output_CoolantMist, .port = COOLANT_MIST_PORT, .pin = COOLANT_MIST_PIN, .group = PinGroup_Coolant },
#if TRINAMIC_ENABLE == 2130
#if TRINAMIC_I2C
{ .id = Input_MotorWarning, .port = TRINAMIC_WARN_IRQ_PORT, .pin = TRINAMIC_WARN_IRQ_PIN, .group = PinGroup_Motor_Warning },
#endif
{ .id = Input_MotorFault, .port = TRINAMIC_DIAG_IRQ_PORT, .pin = TRINAMIC_DIAG_IRQ_PIN, .group = PinGroup_Motor_Fault },
#endif
#ifdef AUXOUTPUT0_PIN
{ .id = Output_Aux0, .port = AUXOUTPUT0_PORT, .pin = AUXOUTPUT0_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT1_PIN
{ .id = Output_Aux1, .port = AUXOUTPUT1_PORT, .pin = AUXOUTPUT1_PIN, .group = PinGroup_AuxOutput },
#endif
#ifdef AUXOUTPUT2_PIN
{ .id = Output_Aux2, .port = AUXOUTPUT2_PORT, .pin = AUXOUTPUT2_PIN, .group = PinGroup_AuxOutput }
#endif
};
static void port_a_isr (void);
static void port_b_isr (void);
static void port_c_isr (void);
static void port_d_isr (void);
static void port_e_isr (void);
static void port_f_isr (void);
static void port_g_isr (void);
static void port_h_isr (void);
static void port_k_isr (void);
static void port_l_isr (void);
static void port_m_isr (void);
static void port_n_isr (void);
static void port_p_isr (void);
static void port_q_isr (void);
static irq_handler_t irq_handler[] = {
{ .port = GPIO_PORTA_BASE, .handler = port_a_isr },
{ .port = GPIO_PORTB_BASE, .handler = port_b_isr },
{ .port = GPIO_PORTC_BASE, .handler = port_c_isr },
{ .port = GPIO_PORTD_BASE, .handler = port_d_isr },
{ .port = GPIO_PORTE_BASE, .handler = port_e_isr },
{ .port = GPIO_PORTF_BASE, .handler = port_f_isr },
{ .port = GPIO_PORTG_BASE, .handler = port_g_isr },
{ .port = GPIO_PORTH_BASE, .handler = port_h_isr },
{ .port = GPIO_PORTK_BASE, .handler = port_k_isr },
{ .port = GPIO_PORTL_BASE, .handler = port_l_isr },
{ .port = GPIO_PORTM_BASE, .handler = port_m_isr },
{ .port = GPIO_PORTN_BASE, .handler = port_n_isr },
{ .port = GPIO_PORTP_BASE, .handler = port_p_isr },
{ .port = GPIO_PORTQ_BASE, .handler = port_q_isr }
};
#include "grbl/stepdir_map.h"
static bool IOInitDone = false;
static uint32_t pulse_length, pulse_delay;
static volatile uint32_t elapsed_tics = 0;
static axes_signals_t next_step_outbits;
static pin_group_pins_t limit_inputs = {0};
static debounce_queue_t debounce_queue = {0};
static delay_t delay = { .ms = 1, .callback = NULL }; // NOTE: initial ms set to 1 for "resetting" systick timer on startup
static probe_state_t probe = {
.connected = On
};
#if DRIVER_SPINDLE_ENABLE
static spindle_id_t spindle_id = -1;
#if DRIVER_SPINDLE_PWM_ENABLE
static bool pwmEnabled = false;
static spindle_pwm_t spindle_pwm;
#endif // DRIVER_SPINDLE_PWM_ENABLE
#endif // DRIVER_SPINDLE_ENABLE
// Interrupt handler prototypes
static void stepper_driver_isr (void);
static void stepper_pulse_isr (void);
static void stepper_pulse_isr_delayed (void);
static void software_debounce_isr (void);
static void systick_isr (void);
#if I2C_STROBE_ENABLE
static void i2c_strobe_isr (void);
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
static void driver_delay_ms (uint32_t ms, void (*callback)(void))
{
if(delay.callback)
delay.callback();
if(ms) {
delay.ms = ms;
SysTickEnable();
if(!(delay.callback = callback)) {
while(delay.ms)
grbl.on_execute_delay(state_get());
}
} else {
if(delay.ms) {
delay.callback = NULL;
delay.ms = 1;
}
if(callback)
callback();
}
}
inline static bool enqueue_debounce (input_signal_t *signal)
{
bool ok;
uint_fast8_t bptr = (debounce_queue.head + 1) & (DEBOUNCE_QUEUE - 1);
if((ok = bptr != debounce_queue.tail)) {
debounce_queue.signal[debounce_queue.head] = signal;
debounce_queue.head = bptr;
}
return ok;
}
// Set stepper pulse output pins
// NOTE: step_outbits are: bit0 -> X, bit1 -> Y, bit2 -> Z...
// Mapping to registers can be done by
// 1. bitbanding. Pros: can assign pins to different ports, no RMW needed. Cons: overhead, pin changes not synchronous
// 2. bit shift. Pros: fast, Cons: bits must be consecutive
// 3. lookup table. Pros: signal inversions done at setup, Cons: slower than bit shift
inline static __attribute__((always_inline)) void set_step_outputs (axes_signals_t step_outbits)
{
#if STEP_OUTMODE == GPIO_MAP
GPIOPinWrite(STEP_PORT, STEP_MASK, step_outmap[step_outbits.value]);
#else
GPIOPinWrite(STEP_PORT, STEP_MASK, (step_outbits.value ^ settings.steppers.step_invert.mask) << STEP_OUTMODE);
#endif
}
// Set stepper direction output pins
// NOTE: see note for set_step_outputs()
inline static __attribute__((always_inline)) void set_dir_outputs (axes_signals_t dir_outbits)
{
#if DIRECTION_OUTMODE == GPIO_MAP
GPIOPinWrite(DIRECTION_PORT, DIRECTION_MASK, dir_outmap[dir_outbits.value]);
#else
GPIOPinWrite(DIRECTION_PORT, DIRECTION_MASK, (dir_outbits.value ^ settings.dir_invert.mask) << DIRECTION_OUTMODE);
#endif
}
// Disable steppers
static void stepperEnable (axes_signals_t enable, bool hold)
{
enable.mask ^= settings.steppers.enable_invert.mask;
#if TRINAMIC_MOTOR_ENABLE
axes_signals_t tmc_enable = trinamic_stepper_enable(enable);
#if !CNC_BOOSTERPACK // Trinamic BoosterPack does not support mixed drivers
if(!tmc_enable.z)
GPIOPinWrite(Z_ENABLE_PORT, Z_ENABLE_BIT, enable.z ? Z_ENABLE_BIT : 0);
if(!tmc_enable.x)
GPIOPinWrite(Z_ENABLE_PORT, Z_ENABLE_BIT, enable.z ? Z_ENABLE_BIT : 0);
#endif
#elif CNC_BOOSTERPACK
GPIOPinWrite(XY_ENABLE_PORT, XY_ENABLE_BIT, enable.x ? XY_ENABLE_BIT : 0);
GPIOPinWrite(Z_ENABLE_PORT, Z_ENABLE_BIT, enable.z ? Z_ENABLE_BIT : 0);
#else
GPIOPinWrite(STEPPERS_ENABLE_PORT, STEPPERS_ENABLE_BIT, enable.x ? STEPPERS_ENABLE_BIT : 0);
#endif
}
// Starts stepper driver ISR timer and forces a stepper driver interrupt callback
static void stepperWakeUp (void)
{
TimerLoadSet(PULSE_TIMER_BASE, TIMER_A, pulse_length);
// Enable stepper drivers.
hal.stepper.enable((axes_signals_t){AXES_BITMASK}, false);
TimerLoadSet(STEPPER_TIMER_BASE, TIMER_A, hal.f_step_timer / 500); // ~2ms delay to allow drivers time to wake up.
TimerEnable(STEPPER_TIMER_BASE, TIMER_A);
}
// Disables stepper driver interrupts and reset outputs
static void stepperGoIdle (bool clear_signals)
{
TimerDisable(STEPPER_TIMER_BASE, TIMER_A);
if(clear_signals) {
set_step_outputs((axes_signals_t){0});
set_dir_outputs((axes_signals_t){0});
}
}
// Sets up stepper driver interrupt timeout
static void stepperCyclesPerTick (uint32_t cycles_per_tick)
{
// Limit min steps/s to about 2 (hal.f_step_timer @ 20MHz)
#ifdef ADAPTIVE_MULTI_AXIS_STEP_SMOOTHING
TimerLoadSet(STEPPER_TIMER_BASE, TIMER_A, cycles_per_tick < (1UL << 18) ? cycles_per_tick : (1UL << 18) - 1UL);
#else
TimerLoadSet(STEPPER_TIMER_BASE, TIMER_A, cycles_per_tick < (1UL << 23) ? cycles_per_tick : (1UL << 23) - 1UL);
#endif
}
// "Normal" version: Sets stepper direction and pulse pins and starts a step pulse a few nanoseconds later.
// If spindle synchronized motion switch to PID version.
static void stepperPulseStart (stepper_t *stepper)
{
if(stepper->dir_change)
set_dir_outputs(stepper->dir_outbits);
if(stepper->step_outbits.value) {
set_step_outputs(stepper->step_outbits);
TimerEnable(PULSE_TIMER_BASE, TIMER_A);
}
}
// Delayed pulse version: sets stepper direction and pulse pins and starts a step pulse with an initial delay.
// If spindle synchronized motion switch to PID version.
// TODO: only delay after setting dir outputs?
static void stepperPulseStartDelayed (stepper_t *stepper)
{
if(stepper->dir_change) {
set_dir_outputs(stepper->dir_outbits);
if(stepper->step_outbits.value) {
next_step_outbits = stepper->step_outbits; // Store out_bits
IntRegister(PULSE_TIMER_INT, stepper_pulse_isr_delayed);
TimerLoadSet(PULSE_TIMER_BASE, TIMER_A, pulse_delay);
TimerEnable(PULSE_TIMER_BASE, TIMER_A);
}
return;
}
if(stepper->step_outbits.value) {
set_step_outputs(stepper->step_outbits);
TimerEnable(PULSE_TIMER_BASE, TIMER_A);
}
}
// Enable/disable limit pins interrupt
static void limitsEnable (bool on, axes_signals_t homing_cycle)
{
bool disable = !on;
axes_signals_t pin;
input_signal_t *limit;
uint_fast8_t idx = limit_inputs.n_pins;
limit_signals_t homing_source = xbar_get_homing_source_from_cycle(homing_cycle);
do {
limit = &limit_inputs.pins.inputs[--idx];
if(limit->group & (PinGroup_Limit|PinGroup_LimitMax)) {
if(on && homing_cycle.mask) {
pin = xbar_fn_to_axismask(limit->id);
disable = limit->group == PinGroup_Limit ? (pin.mask & homing_source.min.mask) : (pin.mask & homing_source.max.mask);
}
if(disable)
GPIOIntDisable(LIMIT_PORT, limit->bit); // Disable pin change interrupt.
else {
GPIOIntClear(limit->port, limit->bit); // Clear and
GPIOIntEnable(limit->port, limit->bit); // enable pin change interrupt.
}
}
} while(idx);
}
// 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};
uint32_t flags = GPIOPinRead(LIMIT_PORT, LIMIT_MASK);
signals.min.x = !!(flags & X_LIMIT_BIT);
signals.min.y = !!(flags & Y_LIMIT_BIT);
signals.min.z = !!(flags & Z_LIMIT_BIT);
if (settings.limits.invert.value)
signals.min.value ^= settings.limits.invert.value;
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.
inline static control_signals_t systemGetState (void)
{
control_signals_t signals;
uint32_t flags = GPIOPinRead(CONTROL_PORT, CONTROL_MASK);
signals.value = settings.control_invert.value;
signals.reset = !!(flags & RESET_BIT);
signals.feed_hold = !!(flags & FEED_HOLD_BIT);
signals.cycle_start = !!(flags & CYCLE_START_BIT);
#if SAFETY_DOOR_BIT
signals.safety_door_ajar = !!(flags & SAFETY_DOOR_BIT);
#endif
#if AUX_CONTROLS_ENABLED
#ifdef SAFETY_DOOR_PIN
if(debounce.safety_door)
signals.safety_door_ajar = !settings.control_invert.safety_door_ajar;
else
signals.safety_door_ajar = GPIOPinRead(SAFETY_DOOR_PORT, 1 << SAFETY_DOOR_PIN);
#endif
#ifdef MOTOR_FAULT_PIN
signals.motor_fault = GPIOPinRead(MOTOR_FAULT_PORT, 1 << MOTOR_FAULT_PIN);
#endif
#ifdef MOTOR_WARNING_PIN
signals.motor_warning = GPIOPinRead(MOTOR_WARNING_PORT, 1 << MOTOR_WARNING_PIN);
#endif
if(settings.control_invert.value)
signals.value ^= settings.control_invert.value;
#if AUX_CONTROLS_SCAN
signals = aux_ctrl_scan_status(signals);
#endif
#else
if(settings.control_invert.value)
signals.value ^= settings.control_invert.value;
#endif // AUX_CONTROLS_ENABLED
return signals;
}
// Toggle probe connected status. Used when no input pin is available.
static void probeConnectedToggle (void)
{
probe.connected = !probe.connected;
}
// 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.inverted = is_probe_away ? !settings.probe.invert_probe_pin : settings.probe.invert_probe_pin;
if(hal.signals_cap.probe_triggered) {
probe.is_probing = Off;
probe.triggered = hal.probe.get_state().triggered;
pin_irq_mode_t irq_mode = probing && !probe.triggered ? (probe.inverted ? IRQ_Mode_Falling : IRQ_Mode_Rising) : IRQ_Mode_None;
probe.irq_enabled = hal.port.register_interrupt_handler(probe_port, irq_mode, aux_irq_handler) && irq_mode != IRQ_Mode_None;
}
}
// Returns the probe connected and triggered pin states.
static probe_state_t probeGetState (void)
{
probe_state_t state = {0};
state.connected = probe.connected;
state.triggered = state.triggered = probe.is_probing && probe.irq_enabled ? probe.triggered : !!GPIOPinRead(PROBE_PORT, 1 << PROBE_PIN) ^ probe.inverted;
return state;
}
#if AUX_CONTROLS_ENABLED
static void aux_irq_handler (uint8_t port, bool state)
{
aux_ctrl_t *pin;
control_signals_t signals = {0};
if((pin = aux_ctrl_get_pin(port))) {
switch(pin->function) {
#ifdef SAFETY_DOOR_PIN
case Input_SafetyDoor:
if((debounce.safety_door = enqueue_debounce(door_pin))) {
TimerLoadSet(DEBOUNCE_TIMER_BASE, TIMER_A, 32000); // 32ms
TimerEnable(DEBOUNCE_TIMER_BASE, TIMER_A);
return;
}
break;
#endif
#ifdef PROBE_PIN
case Input_Probe:
if(probe.is_probing) {
probe.triggered = On;
return;
} else
signals.probe_triggered = On;
break;
#endif
#ifdef I2C_STROBE_PIN
case Input_I2CStrobe:
if(i2c_strobe.callback)
i2c_strobe.callback(0, DIGITAL_IN(I2C_STROBE_PORT, I2C_STROBE_PIN) == 0);
break;
#endif
#ifdef MPG_MODE_PIN
case Input_MPGSelect:
protocol_enqueue_foreground_task(mpg_select, NULL);
break;
#endif
default:
break;
}
signals.mask |= pin->cap.mask;
if(pin->irq_mode == IRQ_Mode_Change && pin->function != Input_Probe)
signals.deasserted = hal.port.wait_on_input(Port_Digital, pin->aux_port, WaitMode_Immediate, 0.0f) == 0;
}
if(signals.mask) {
if(!signals.deasserted)
signals.mask |= systemGetState().mask;
hal.control.interrupt_callback(signals);
}
}
static bool aux_claim_explicit (aux_ctrl_t *aux_ctrl)
{
if(ioport_claim(Port_Digital, Port_Input, &aux_ctrl->aux_port, NULL)) {
ioport_assign_function(aux_ctrl, &((input_signal_t *)aux_ctrl->input)->id);
#ifdef PROBE_PIN
if(aux_ctrl->function == Input_Probe) {
probe_port = aux_ctrl->aux_port;
hal.probe.get_state = probeGetState;
hal.probe.configure = probeConfigure;
hal.probe.connected_toggle = probeConnectedToggle;
hal.driver_cap.probe_pull_up = On;
hal.signals_cap.probe_triggered = hal.driver_cap.probe_latch = aux_ctrl->irq_mode != IRQ_Mode_None;
}
#endif
#ifdef SAFETY_DOOR_PIN
if(aux_ctrl->function == Input_SafetyDoor)
door_pin = (input_signal_t *)aux_ctrl->input;
#endif
} else
aux_ctrl->aux_port = 0xFF;
return aux_ctrl->aux_port != 0xFF;
}
#endif // AUX_CONTROLS_ENABLED
#if DRIVER_SPINDLE_ENABLE
// Static spindle (off, on cw & on ccw)
inline static void spindle_off (void)
{
GPIOPinWrite(SPINDLE_ENABLE_PORT, SPINDLE_ENABLE_BIT, settings.spindle.invert.on ? SPINDLE_ENABLE_BIT : 0);
}
inline static void spindle_on (void)
{
GPIOPinWrite(SPINDLE_ENABLE_PORT, SPINDLE_ENABLE_BIT, settings.spindle.invert.on ? 0 : SPINDLE_ENABLE_BIT);
}
inline static void spindle_dir (bool ccw)
{
GPIOPinWrite(SPINDLE_DIRECTION_PORT, SPINDLE_DIRECTION_BIT, (ccw ^ settings.spindle.invert.ccw) ? SPINDLE_DIRECTION_BIT : 0);
}
// Start or stop spindle
static void spindleSetState (spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
{
UNUSED(rpm);
UNUSED(spindle);
if(!state.on)
spindle_off();
else {
spindle_dir(state.ccw);
spindle_on();
}
}
#if DRIVER_SPINDLE_PWM_ENABLE
// Variable spindle control functions
// Sets spindle speed
#if PWM_RAMPED
static void spindleSetSpeed (spindle_ptrs_t *spindle, uint_fast16_t pwm_value)
{
if (pwm_value == spindle->context.pwm->off_value) {
pwm_ramp.pwm_target = 0;
pwm_ramp.pwm_step = -SPINDLE_RAMP_STEP_INCR;
pwm_ramp.delay_ms = 0;
pwm_ramp.ms_cfg = SPINDLE_RAMP_STEP_TIME;
SysTickEnable();
} else {
if(!pwmEnabled) {
spindle_on();
pwmEnabled = true;
pwm_ramp.pwm_current = spindle->context.pwm->min_value;
pwm_ramp.delay_ms = 0;
TimerMatchSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle->context.pwm->period - pwm_ramp.pwm_current + 15);
TimerLoadSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle->context.pwm->period);
TimerEnable(SPINDLE_PWM_TIMER_BASE, TIMER_A); // Ensure PWM output is enabled.
// TimerControlLevel(SPINDLE_PWM_TIMER_BASE, TIMER_A, false);
}
pwm_ramp.pwm_target = pwm_value;
pwm_ramp.pwm_step = pwm_ramp.pwm_target < pwm_ramp.pwm_current ? -SPINDLE_RAMP_STEP_INCR : SPINDLE_RAMP_STEP_INCR;
pwm_ramp.ms_cfg = SPINDLE_RAMP_STEP_TIME;
TimerControlLevel(SPINDLE_PWM_TIMER_BASE, TIMER_A, false);
SysTickEnable();
}
}
#else
static void spindleSetSpeed (spindle_ptrs_t *spindle, uint_fast16_t pwm_value)
{
if (pwm_value == spindle->context.pwm->off_value) {
pwmEnabled = false;
if(spindle->context.pwm->settings->flags.enable_rpm_controlled) {
if(spindle->context.pwm->cloned)
spindle_dir(false);
else
spindle_off();
}
if(spindle->context.pwm->always_on) {
TimerPrescaleMatchSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle->context.pwm->off_value >> 16);
TimerMatchSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle->context.pwm->off_value & 0xFFFF);
TimerControlLevel(SPINDLE_PWM_TIMER_BASE, TIMER_A, !spindle->context.pwm->settings->invert.pwm);
TimerEnable(SPINDLE_PWM_TIMER_BASE, TIMER_A); // Ensure PWM output is enabled.
} else {
uint_fast16_t pwm = spindle->context.pwm->period + 20000;
TimerPrescaleSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, pwm >> 16);
TimerLoadSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, pwm & 0xFFFF);
if(!pwmEnabled)
TimerEnable(SPINDLE_PWM_TIMER_BASE, TIMER_A); // Ensure PWM output is enabled to
TimerControlLevel(SPINDLE_PWM_TIMER_BASE, TIMER_A, !spindle->context.pwm->settings->invert.pwm); // ensure correct output level.
TimerDisable(SPINDLE_PWM_TIMER_BASE, TIMER_A); // Disable PWM.
}
} else {
TimerPrescaleMatchSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, pwm_value >> 16);
TimerMatchSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, pwm_value & 0xFFFF);
if(!pwmEnabled) {
if(spindle->context.pwm->cloned)
spindle_dir(true);
else
spindle_on();
pwmEnabled = true;
TimerPrescaleSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle->context.pwm->period >> 16);
TimerLoadSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle->context.pwm->period & 0xFFFF);
TimerControlLevel(SPINDLE_PWM_TIMER_BASE, TIMER_A, !spindle->context.pwm->settings->invert.pwm);
TimerEnable(SPINDLE_PWM_TIMER_BASE, TIMER_A); // Ensure PWM output is enabled.
}
}
}
#endif // !PWM_RAMPED
static uint_fast16_t spindleGetPWM (spindle_ptrs_t *spindle, float rpm)
{
return spindle->context.pwm->compute_value(spindle->context.pwm, rpm, false);
}
// Start or stop spindle
static void spindleSetStateVariable (spindle_ptrs_t *spindle, spindle_state_t state, float rpm)
{
#ifdef SPINDLE_DIRECTION_PIN
if (state.on || spindle->context.pwm->cloned)
spindle_dir(state.ccw);
#endif
if(!settings.spindle.flags.enable_rpm_controlled) {
if(state.on)
spindle_on();
else
spindle_off();
}
spindleSetSpeed(spindle, state.on || (state.ccw && spindle->context.pwm->cloned)
? spindle->context.pwm->compute_value(spindle->context.pwm, rpm, false)
: spindle->context.pwm->off_value);
}
bool spindleConfig (spindle_ptrs_t *spindle)
{
if(spindle == NULL)
return false;
spindle_pwm.offset = -1;
if(spindle_precompute_pwm_values(spindle, &spindle_pwm, &settings.spindle, SysCtlClockGet())) {
TimerPrescaleSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle_pwm.period >> 16);
TimerLoadSet(SPINDLE_PWM_TIMER_BASE, TIMER_A, spindle_pwm.period & 0xFFFF);
spindle->set_state = spindleSetStateVariable;
} else {
if(pwmEnabled)
spindle->set_state(spindle, (spindle_state_t){0}, 0.0f);
spindle->set_state = spindleSetState;
}
spindle_update_caps(spindle, spindle->cap.variable ? &spindle_pwm : NULL);
return true;
}
#if PPI_ENABLE
static void spindlePulseOn (uint_fast16_t pulse_length)
{
spindle_on();
TimerLoadSet(PPI_ENABLE_TIMER_BASE, TIMER_A, pulse_length);
TimerEnable(PPI_ENABLE_TIMER_BASE, TIMER_A);
}
#endif // PPI_ENABLE
#endif // DRIVER_SPINDLE_PWM_ENABLE
// Returns spindle state in a spindle_state_t variable
static spindle_state_t spindleGetState (spindle_ptrs_t *spindle)
{
spindle_state_t state = {0};
state.on = GPIOPinRead(SPINDLE_ENABLE_PORT, SPINDLE_ENABLE_BIT) != 0;
state.ccw = GPIOPinRead(SPINDLE_DIRECTION_PORT, SPINDLE_DIRECTION_BIT) != 0;
state.value ^= settings.spindle.invert.mask;
if(pwmEnabled)
state.on |= pwmEnabled;
#if PWM_RAMPED
state.at_speed = pwm_ramp.pwm_current == pwm_ramp.pwm_target;
#endif
return state;
}
#endif // DRIVER_SPINDLE_ENABLE
// Start/stop coolant (and mist if enabled)
static void coolantSetState (coolant_state_t mode)
{
mode.value ^= settings.coolant_invert.mask;
GPIOPinWrite(COOLANT_FLOOD_PORT, COOLANT_FLOOD_BIT, mode.flood ? COOLANT_FLOOD_BIT : 0);
GPIOPinWrite(COOLANT_MIST_PORT, COOLANT_MIST_BIT, mode.mist ? COOLANT_MIST_BIT : 0);
}
// Returns coolant state in a coolant_state_t variable
static coolant_state_t coolantGetState (void)
{
coolant_state_t state = {0};
state.flood = GPIOPinRead(COOLANT_FLOOD_PORT, COOLANT_FLOOD_BIT) != 0;
state.mist = GPIOPinRead(COOLANT_MIST_PORT, COOLANT_MIST_BIT) != 0;
state.value ^= settings.coolant_invert.mask;
return state;
}
// Helper functions for setting/clearing/inverting individual bits atomically (uninterruptable)
static void bitsSetAtomic (volatile uint_fast16_t *ptr, uint_fast16_t bits)
{
IntMasterDisable();
*ptr |= bits;
IntMasterEnable();
}
static uint_fast16_t bitsClearAtomic (volatile uint_fast16_t *ptr, uint_fast16_t bits)
{
IntMasterDisable();
uint_fast16_t prev = *ptr;
*ptr &= ~bits;
IntMasterEnable();
return prev;
}
static uint_fast16_t valueSetAtomic (volatile uint_fast16_t *ptr, uint_fast16_t value)
{
IntMasterDisable();
uint_fast16_t prev = *ptr;
*ptr = value;
IntMasterEnable();
return prev;
}
static void enable_irq (void)
{
IntMasterEnable();
}
static void disable_irq (void)
{
IntMasterDisable();
}
#if MPG_MODE == 1
static void mpg_select (void *data)
{
stream_mpg_enable(GPIOPinRead(MPG_MODE_PORT, MPG_MODE_BIT) == 0);
GPIOIntEnable(MPG_MODE_PORT, MPG_MODE_BIT);
}
static void mpg_enable (void *data)
{
if(sys.mpg_mode == (GPIOPinRead(MPG_MODE_PORT, MPG_MODE_BIT) == 0))
mpg_select(data);
#if I2C_STROBE_ENABLE
// BITBAND_PERI(I2C_STROBE_PORT->IE, I2C_STROBE_PIN) = 1;
#endif
}
#endif
uint32_t getElapsedTicks (void)
{
return elapsed_tics;
}
static irq_handler_t *get_handler (uint32_t port)
{
uint32_t i = sizeof(irq_handler) / sizeof(irq_handler_t);
do {
if(irq_handler[--i].port == port)
return &irq_handler[i];
} while(i);
return NULL;
}
// Configures perhipherals when settings are initialized or changed
static void settings_changed (settings_t *settings, settings_changed_flags_t changed)
{
#if USE_STEPDIR_MAP
stepdirmap_init(settings);
#endif
if(IOInitDone) {
#if DRIVER_SPINDLE_PWM_ENABLE
if(changed.spindle) {
spindleConfig(spindle_get_hal(spindle_id, SpindleHAL_Configured));
if(spindle_id == spindle_get_default())
spindle_select(spindle_id);
}
#endif
pulse_length = (uint32_t)(10.0f * (settings->steppers.pulse_microseconds - STEP_PULSE_LATENCY)) - 1;
if(hal.driver_cap.step_pulse_delay && settings->steppers.pulse_delay_microseconds > 0.0f) {
int32_t delay = (uint32_t)(10.0f * (settings->steppers.pulse_delay_microseconds - 1.2f)) - 1;
pulse_delay = delay < 2 ? 2 : delay;
hal.stepper.pulse_start = stepperPulseStartDelayed;
} else
hal.stepper.pulse_start = stepperPulseStart;
TimerIntRegister(PULSE_TIMER_BASE, TIMER_A, stepper_pulse_isr);
TimerIntEnable(PULSE_TIMER_BASE, TIMER_TIMA_TIMEOUT);
/****************************************
* Control, limit & probe pins config *
****************************************/
bool pullup;
uint32_t i = sizeof(inputpin) / sizeof(input_signal_t);
input_signal_t *input;
irq_handler_t *handler;
control_signals_t control_fei;
control_fei.mask = settings->control_disable_pullup.mask ^ settings->control_invert.mask;