comparison dac.pio @ 2:0d653f60dec8

Actually get data moving out. Previous shape info caused a hard fault.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 29 Mar 2021 17:39:00 +1030
parents a55e39064a71
children 2db42eaba3c8
comparison
equal deleted inserted replaced
1:b9ec45077bff 2:0d653f60dec8
1 ; 1 ;
2 ; Copyright (c) 2021 Raspberry Pi (Trading) Ltd. 2 ; Copyright (c) 2021 Daniel O'Connor
3 ;
4 ; SPDX-License-Identifier: BSD-3-Clause
5 ; 3 ;
6 4
7 .program dac 5 .program dac
8 6
9 ; Sample bits using an external clock, and push groups of bits into the RX FIFO. 7 .side_set 1
10 ; - IN pin 0 is the data pin 8 ; Clock DAC and write data from the FIFO
11 ; - IN pin 1 is the clock pin
12 ; - Autopush is enabled, threshold 8
13 ;
14 ; This program samples data with each rising clock edge (like mode 0 or mode 3
15 ; SPI). The data is actually sampled one system clock cycle after the rising
16 ; edge is observed, so a clock ratio of at least input_clk < clk_sys / 6 is
17 ; recommended for good sampling alignment.
18 9
19 wait 0 pin 1 10 .wrap_target
20 wait 1 pin 1 11 pull side 1
21 in pins, 1 12 out pins 8 side 0
13 nop side 1
14 out pins 8 side 0
15 nop side 1
16 out pins 8 side 0
17 nop side 1
18 out pins 8 side 0
19 .wrap
22 20
23 % c-sdk { 21 % c-sdk {
24 static inline void dac_program_init(PIO pio, uint sm, uint offset, uint pin) { 22 static inline void dac_program_init(PIO pio, uint sm, uint offset, uint pin) {
25 pio_sm_config c = dac_program_get_default_config(offset); 23 pio_sm_config c = dac_program_get_default_config(offset);
26 24
27 // Set the IN base pin to the provided `pin` parameter. This is the data 25 // Set the OUT base pin to the provided `pin` parameter.
28 // pin, and the next-numbered GPIO is used as the clock pin. 26 // First 8 pins are data, last is clock
29 sm_config_set_in_pins(&c, pin); 27 sm_config_set_out_pins(&c, pin, 9);
30 // Set the pin directions to input at the PIO 28 // Set the pin directions to output at the PIO
31 pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false); 29 pio_sm_set_consecutive_pindirs(pio, sm, pin, 9, true);
32 // Connect these GPIOs to this PIO block 30 // Connect these GPIOs to this PIO block
33 pio_gpio_init(pio, pin); 31 for (int i = 0; i < 9; i++)
34 pio_gpio_init(pio, pin + 1); 32 pio_gpio_init(pio, pin + i);
35 33
36 // Shifting to left matches the customary MSB-first ordering of SPI. 34 sm_config_set_out_shift(
37 sm_config_set_in_shift(
38 &c, 35 &c,
39 false, // Shift-to-right = false (i.e. shift to left) 36 true, // Shift-to-right
40 true, // Autopush enabled 37 false, // Autopull enabled
41 8 // Autopush threshold = 8 38 8 // Autopull threshold (bits!)
42 ); 39 );
43 40
44 // We only receive, so disable the TX FIFO to make the RX FIFO deeper. 41 // Configure clock as sideset pin
45 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX); 42 sm_config_set_sideset_pins(&c, pin + 7);
43
44 // We only send, so disable the RX FIFO to make the TX FIFO deeper.
45 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
46
47 // sm_config_set_clkdiv(&c, 100);
46 48
47 // Load our configuration, and start the program from the beginning 49 // Load our configuration, and start the program from the beginning
48 pio_sm_init(pio, sm, offset, &c); 50 pio_sm_init(pio, sm, offset, &c);
49 pio_sm_set_enabled(pio, sm, true); 51 pio_sm_set_enabled(pio, sm, true);
50 } 52 }