forked from grblHAL/RP2040
-
Notifications
You must be signed in to change notification settings - Fork 1
/
driverPIO.pio
88 lines (71 loc) · 2.68 KB
/
driverPIO.pio
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
; RP2040 PIO driver specific program
; stepper_timer program generate an interrupt at a settable period interval
.program stepper_timer
pull noblock ; Pull timer period from the TX FIFO, loads X into OSR if empty
mov x osr ; Reload X from OSR
mov y x
count:
jmp y-- count
irq 0
% c-sdk {
static inline void stepper_timer_program_init(PIO pio, uint sm, uint offset, float div ) {
pio_sm_config c = stepper_timer_program_get_default_config(offset);
// Load our configuration, and jump to the start of the program
pio_sm_init(pio, sm, offset, &c);
// Set the clock divider
pio_sm_set_clkdiv(pio, sm, div);
}
static inline void stepper_timer_set_period(PIO pio, uint sm, uint offset, uint32_t period) {
static uint32_t period_prev = 0;
if(period_prev != period) {
period_prev = period;
pio_sm_put(pio, sm, period);
pio_sm_exec(pio, sm, pio_encode_jmp(offset));
}
if(!(pio->ctrl & (1 << sm))) {
pio_sm_set_enabled(pio, sm, true);
pio->inte0 |= PIO_INTR_SM0_BITS;
}
}
static inline void stepper_timer_stop(PIO pio, uint sm) {
pio_sm_set_enabled(pio, sm, false);
pio->inte0 &= ~PIO_INTR_SM0_BITS;
}
static inline void stepper_timer_irq_clear(PIO pio) {
pio->irq = 1;
}
%}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; step pulse program generate a step pulse with a settable delay before the step is generated and a settable pulse length
.program step_pulse
pull block
out x, 8
delay:
jmp x-- delay
out x, 8
out pins, 6
pulse:
jmp x-- pulse
out pins, 6
% c-sdk {
static inline void step_pulse_program_init(PIO pio, uint sm, uint offset, uint32_t startPin, uint pinCount) {
pio_sm_config c = step_pulse_program_get_default_config(offset);
// Map the state machine's OUT pin group to the provided pin in pin count in parameters
sm_config_set_out_pins(&c, startPin, pinCount);
// Set these pins GPIO function (connect PIO to the pad)
for(uint i=0;i<pinCount;i++)
pio_gpio_init(pio, startPin+i);
// Set the pin direction to output at the PIO
pio_sm_set_consecutive_pindirs(pio, sm, startPin, pinCount, true);
// Set the set pins group to the same pins as the out pins group to reset the pins when the steps are finished
sm_config_set_set_pins(&c, startPin, pinCount);
// Load our configuration, and jump to the start of the program
pio_sm_init(pio, sm, offset, &c);
pio_sm_set_clkdiv(pio, sm, 12.5f);
// Set the state machine running
pio_sm_set_enabled(pio, sm, true);
}
static inline void step_pulse_generate(PIO pio, uint sm, uint32_t stepPulse) {
pio_sm_put(pio, sm, stepPulse);
}
%}