annotate dac.pio @ 30:92fdf2ef995d default tip

Use 32 bit rather than 16 bit ints for loop vars. No risk of overflow and is actually faster.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 27 Feb 2025 15:24:17 +1030
parents 600a394629e6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 ;
9
3acdebd7eec7 Make it actually work
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
2 ; Copyright (c) 2025 Daniel O'Connor
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 ;
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 .program dac
27
e1d8fe3e418a Run PIOs at 1x with delays and sync.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
6 .define TRIGGER_IRQ 0
9
3acdebd7eec7 Make it actually work
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
7 ; Need 1 side set pin, the clock
3acdebd7eec7 Make it actually work
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
8 .side_set 1
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9
28
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
10 PUBLIC init:
9
3acdebd7eec7 Make it actually work
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
11 ; Clock in a 0 byte
10
98880b18bcc1 Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
12 mov pins, null side 0
98880b18bcc1 Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
13 nop side 1
98880b18bcc1 Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents: 9
diff changeset
14 ; Wait for start trigger and clear IRQ
27
e1d8fe3e418a Run PIOs at 1x with delays and sync.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
15 wait 1 irq TRIGGER_IRQ side 0
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
16 ; Clock DAC and write data from the FIFO
9
3acdebd7eec7 Make it actually work
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
17 ; DAC clocks data in on the rising clock edge
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
18 .wrap_target
27
e1d8fe3e418a Run PIOs at 1x with delays and sync.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
19 out pins 8 side 0 [1]
e1d8fe3e418a Run PIOs at 1x with delays and sync.
Daniel O'Connor <darius@dons.net.au>
parents: 25
diff changeset
20 nop side 1 [1]
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
21 .wrap
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 % c-sdk {
5
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
24 static inline void dac_program_init(PIO pio, uint sm, uint offset, uint pin, uint clkdiv) {
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 pio_sm_config c = dac_program_get_default_config(offset);
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
27 // Set the OUT base pin to the provided `pin` parameter.
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
28 // First 8 pins are data, last is clock
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
29 sm_config_set_out_pins(&c, pin, 9);
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
30 // Set the pin directions to output at the PIO
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
31 pio_sm_set_consecutive_pindirs(pio, sm, pin, 9, true);
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 // Connect these GPIOs to this PIO block
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
33 for (int i = 0; i < 9; i++)
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
34 pio_gpio_init(pio, pin + i);
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35
28
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
36 // Configure sideset pin to use for clock
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
37 sm_config_set_sideset_pins(&c, pin + 8);
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
38
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
39 sm_config_set_out_shift(
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 &c,
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
41 true, // Shift-to-right
9
3acdebd7eec7 Make it actually work
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
42 true, // Autopull enabled
28
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
43 8 // Autopull threshold (bits!)
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 );
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45
2
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
46 // We only send, so disable the RX FIFO to make the TX FIFO deeper.
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
47 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
0d653f60dec8 Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents: 0
diff changeset
48
5
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
49 sm_config_set_clkdiv(&c, clkdiv);
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50
28
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
51 // Load our configuration (but don't start)
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 pio_sm_init(pio, sm, offset, &c);
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 }
28
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
54
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
55 static inline uint dac_reset_instr (uint offset) {
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
56 // encode a "jmp init side 0" instruction for the state machine
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
57 return pio_encode_jmp (offset + dac_offset_init) | pio_encode_sideset (1, 0);
600a394629e6 Use 8 bit auto pull otherwise the PIOs jitter (due to DMA contention I guess?)
Daniel O'Connor <darius@dons.net.au>
parents: 27
diff changeset
58 }
0
a55e39064a71 First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 %}