Exploit the fact that samples fit a single byte

This commit is contained in:
Jan Hamal Dvořák 2025-06-20 23:10:48 +02:00
parent 3c9ad982e6
commit 0d2c617d94

View file

@ -20,15 +20,12 @@
#define IRP_SM 0
#define IRN_SM 1
// Actually one less, but this is also for timing.
#define SAMPLE_DEPTH 128
#define NUM_SAMPLES_BITS 10
#define NUM_SAMPLES (1 << NUM_SAMPLES_BITS)
static uint16_t irp_buffer[NUM_SAMPLES] __aligned(2 * NUM_SAMPLES);
static uint8_t irp_buffer[NUM_SAMPLES] __aligned(NUM_SAMPLES);
static int dma_ch_irp_rx;
static uint16_t irn_buffer[NUM_SAMPLES] __aligned(2 * NUM_SAMPLES);
static uint8_t irn_buffer[NUM_SAMPLES] __aligned(NUM_SAMPLES);
static int dma_ch_irn_rx;
#define BIT(n) (1 << (n))
@ -37,7 +34,7 @@ static void init_pio_sm(pio_hw_t *pio, int sm, int rx_pin, int fb_pin)
{
const uint16_t insn[] = {
pio_encode_jmp_x_dec(4),
pio_encode_in(pio_y, 16) | pio_encode_sideset_opt(1, 1),
pio_encode_in(pio_y, 8) | pio_encode_sideset_opt(1, 1),
pio_encode_mov(pio_x, pio_osr),
pio_encode_set(pio_y, 0) | pio_encode_sideset_opt(1, 0),
pio_encode_jmp_pin(6),
@ -62,7 +59,7 @@ static void init_pio_sm(pio_hw_t *pio, int sm, int rx_pin, int fb_pin)
sm_config_set_set_pins(&pc, fb_pin, 1);
sm_config_set_sideset_pin_base(&pc, fb_pin);
sm_config_set_sideset(&pc, 2, true, false);
sm_config_set_in_shift(&pc, false, true, 16);
sm_config_set_in_shift(&pc, false, true, 32);
sm_config_set_out_shift(&pc, false, false, 32);
pio_sm_init(pio, sm, prog.origin, &pc);
@ -71,7 +68,7 @@ static void init_pio_sm(pio_hw_t *pio, int sm, int rx_pin, int fb_pin)
pio_sm_restart(pio, sm);
pio_sm_clear_fifos(pio, sm);
pio->txf[sm] = SAMPLE_DEPTH - 2;
pio->txf[sm] = 126;
pio_sm_exec_wait_blocking(pio, sm, pio_encode_pull(false, false));
pio_sm_exec_wait_blocking(pio, sm, pio_encode_mov(pio_x, pio_osr));
@ -92,7 +89,7 @@ static unsigned tail;
static bool read_sample(int *sample)
{
unsigned head = (dma_hw->ch[dma_ch_irn_rx].write_addr >> 1) % NUM_SAMPLES;
unsigned head = dma_hw->ch[dma_ch_irn_rx].write_addr % NUM_SAMPLES;
if (tail == head)
return false;
@ -138,19 +135,19 @@ int main()
dma_channel_config dma_conf;
dma_conf = dma_channel_get_default_config(dma_ch_irp_rx);
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_16);
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
channel_config_set_read_increment(&dma_conf, false);
channel_config_set_write_increment(&dma_conf, true);
channel_config_set_ring(&dma_conf, true, NUM_SAMPLES_BITS + 1);
channel_config_set_ring(&dma_conf, true, NUM_SAMPLES_BITS);
channel_config_set_dreq(&dma_conf, pio_get_dreq(IR_PIO, IRP_SM, false));
channel_config_set_chain_to(&dma_conf, dma_ch_irn_rx);
dma_channel_configure(dma_ch_irp_rx, &dma_conf, irp_buffer, &IR_PIO->rxf[IRP_SM], 1, false);
dma_conf = dma_channel_get_default_config(dma_ch_irn_rx);
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_16);
channel_config_set_transfer_data_size(&dma_conf, DMA_SIZE_32);
channel_config_set_read_increment(&dma_conf, false);
channel_config_set_write_increment(&dma_conf, true);
channel_config_set_ring(&dma_conf, true, NUM_SAMPLES_BITS + 1);
channel_config_set_ring(&dma_conf, true, NUM_SAMPLES_BITS);
channel_config_set_dreq(&dma_conf, pio_get_dreq(IR_PIO, IRN_SM, false));
channel_config_set_chain_to(&dma_conf, dma_ch_irp_rx);
dma_channel_configure(dma_ch_irn_rx, &dma_conf, irn_buffer, &IR_PIO->rxf[IRN_SM], 1, false);