summaryrefslogtreecommitdiffstats
path: root/src/modchipfw/pulsegen.pio
blob: 4068940e618a57a73d25ddd92a2a7dd6762bf8e1 (plain) (blame)
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
.program pulsegen

; we use 2 bits from the delay bits for side set
.side_set 2

entry:
    ; Could probably get rid of these blocking PULLs by enabling autopull and writing to the fifo before enabling the SM?
    ; Read number of edges
    PULL BLOCK side 0
    MOV X, OSR side 0 ; Might want to use OUT here instead of MOV
    
    ; Read pulse offset
    PULL BLOCK side 0
    MOV Y, OSR side 0

    ; Clear interrupt (might not be needed if we clear it on the M0 side of things?)
    IRQ CLEAR 0 side 0

nedges:
    ; Wait for rising-edges
    WAIT 0 PIN 0 side 0
    WAIT 1 PIN 0 side 0
    JMP X-- nedges side 0

    ; Read pulse width
    ; This will cause a fixed delay between the trigger event and the glitch insertion. Fine for our purposes.
    PULL BLOCK side 0
    MOV X, OSR side 0

; Loop for pulse offset cycles
poffset:
    JMP Y-- poffset side 2

; Loop for pulse width cycles
pwidth:
    JMP X-- pwidth side 3


    SET Y, 31 side 2 ; A fixed delay to ensure that the glitch has been inserted before the capacitors are enabled again.
delay:
    NOP side 2 [7]
    NOP side 2 [7]
    NOP side 2 [7]
    NOP side 2 [7]
    NOP side 2 [7]
    JMP Y-- delay side 2

    ; Signal that the pulse has been inserted, and disable the pulse using sideset
    IRQ WAIT 0 side 0


% c-sdk {
void pulsegen_program_init(PIO pio, uint sm, uint offset, uint trigger_pin, uint pulse_pin, uint caps_pin) {
    pio_sm_config c = pulsegen_program_get_default_config(offset);

    sm_config_set_sideset_pins(&c, pulse_pin);
    sm_config_set_in_pins(&c, trigger_pin);
    sm_config_set_in_shift(&c, false, false, 32);
    
    pio_gpio_init(pio, trigger_pin);
    pio_gpio_init(pio, pulse_pin);
    pio_gpio_init(pio, caps_pin);

    pio_sm_set_consecutive_pindirs(pio, sm, trigger_pin, 1, false);
    pio_sm_set_consecutive_pindirs(pio, sm, pulse_pin, 2, true);

    sm_config_set_clkdiv(&c, 1);
    pio_sm_init(pio, sm, offset, &c);
}
%}