hsdaoh-rp2350/apps/external_adc/adc_12bit_input.pio
2025-11-15 21:21:37 +01:00

83 lines
2.3 KiB
Text

;
; Copyright (c) 2024-2025 Steve Markgraf <steve@steve-m.de>
;
; SPDX-License-Identifier: BSD-3-Clause
;
; Sample 12 bit parallel ADC (AD9226) every 2 PIO cycles on rising clock edge,
; pack four 12 bit samples in three 16 bit words
; ADC clock output as side-set
;
; Data being pushed to the FIFO, four 12 bit samples A-D
; First word: A03 A02 A01 A00 B11 B10 B09 B08 B07 B06 B05 B04 B03 B02 B01 B00
; Second word: A07 A06 A05 A04 C11 C10 C09 C08 C07 C06 C05 C04 C03 C02 C01 C00
; Third word: A11 A10 A09 A08 D11 D10 D09 D08 D07 D06 D05 D04 D03 D02 D01 D00
.pio_version 1
.program adc_12bit_input
.side_set 2
public entry_point:
.wrap_target
mov osr, pins side 3 ; sample A
out isr, 4 side 0
in pins, 12 side 3 ; sample B, autopush
out isr, 4 side 0
in pins, 12 side 3 ; sample C, autopush
out isr, 4 side 0
in pins, 12 side 3 ; sample D, autopush
nop side 0
.wrap
% c-sdk {
static inline void adc_12bit_input_program_init(PIO pio, uint sm, uint offset, uint pin, uint clk_pin)
{
pio_sm_config c = adc_12bit_input_program_get_default_config(offset);
// Set the IN base pin to the provided `pin` parameter.
sm_config_set_in_pins(&c, pin);
// configure CLK pin for side-set
sm_config_set_sideset_pins(&c, clk_pin);
sm_config_set_sideset(&c, 2, false, false);
pio_sm_set_consecutive_pindirs(pio, sm, clk_pin, 2, true);
pio_gpio_init(pio, clk_pin);
pio_gpio_init(pio, clk_pin+1);
// Set the pin directions to input at the PIO
// Set D0-D11 of the ADC as input
pio_sm_set_consecutive_pindirs(pio, sm, pin, 12, false);
// Connect these GPIOs to this PIO block
for (int i = pin; i < (pin+12); i++)
pio_gpio_init(pio, i);
sm_config_set_in_shift(
&c,
false, // Shift-to-right = false (i.e. shift to left)
true, // Autopush enabled
16 // Autopush threshold = 16
);
// required in order to set shift-to-right to true (for the out ops)
sm_config_set_out_shift(
&c,
true, // Shift-to-right = true
false, // Autopush disabled
1 // Autopush threshold: ignored
);
// We only receive, so disable the TX FIFO to make the RX FIFO deeper.
sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX);
sm_config_set_clkdiv(&c, 2.f);
// Load our configuration, and start the program from the beginning
pio_sm_init(pio, sm, offset, &c);
// pio_sm_set_enabled(pio, sm, true);
}
%}