Fix PIO code origins handling

This commit is contained in:
Jan Hamal Dvořák 2024-08-03 00:01:22 +02:00
parent 3410740b4a
commit 5868a1ade9

View file

@ -91,6 +91,11 @@ static size_t iq_queue_pos = 0;
static uint32_t rnd = 0;
static int origin_lo = -1;
static int origin_rx = -1;
static int origin_fb = -1;
static int origin_ad = 0;
inline static __unused uint32_t rnd_next()
{
rnd = rnd * 0x41c64e6d + 12345;
@ -120,23 +125,23 @@ static void init_lo()
pio_program_t prog = {
.instructions = insn,
.length = sizeof(insn) / sizeof(*insn),
.origin = -1,
.origin = origin_lo,
};
pio_sm_restart(PIO, LO_SM);
pio_sm_clear_fifos(PIO, LO_SM);
if (pio_can_add_program(PIO, &prog))
prog.origin = pio_add_program(PIO, &prog);
origin_lo = pio_add_program(PIO, &prog);
pio_sm_config pc = pio_get_default_sm_config();
sm_config_set_out_pins(&pc, LO_PIN, 1);
sm_config_set_set_pins(&pc, LO_PIN, 1);
sm_config_set_wrap(&pc, prog.origin, prog.origin + prog.length - 1);
sm_config_set_wrap(&pc, origin_lo, origin_lo + prog.length - 1);
sm_config_set_clkdiv_int_frac(&pc, 1, 0);
sm_config_set_fifo_join(&pc, PIO_FIFO_JOIN_TX);
sm_config_set_out_shift(&pc, false, true, 32);
pio_sm_init(PIO, LO_SM, prog.origin, &pc);
pio_sm_init(PIO, LO_SM, origin_lo, &pc);
pio_sm_set_consecutive_pindirs(PIO, LO_SM, LO_PIN, 1, GPIO_IN);
pio_sm_exec_wait_blocking(PIO, LO_SM, pio_encode_set(pio_pins, 0));
@ -163,14 +168,14 @@ static void init_fb()
pio_program_t prog = {
.instructions = insn,
.length = sizeof(insn) / sizeof(*insn),
.origin = -1,
.origin = origin_fb,
};
pio_sm_restart(PIO, FB_SM);
pio_sm_clear_fifos(PIO, FB_SM);
if (pio_can_add_program(PIO, &prog))
prog.origin = pio_add_program(PIO, &prog);
origin_fb = pio_add_program(PIO, &prog);
pio_sm_config pc = pio_get_default_sm_config();
sm_config_set_sideset(&pc, 1, false, true);
@ -178,9 +183,9 @@ static void init_fb()
sm_config_set_out_pins(&pc, FB_PIN, 1);
sm_config_set_set_pins(&pc, FB_PIN, 1);
sm_config_set_sideset_pins(&pc, FB_PIN);
sm_config_set_wrap(&pc, prog.origin, prog.origin + prog.length - 1);
sm_config_set_wrap(&pc, origin_fb, origin_fb + prog.length - 1);
sm_config_set_clkdiv_int_frac(&pc, 1, 0);
pio_sm_init(PIO, FB_SM, prog.origin, &pc);
pio_sm_init(PIO, FB_SM, origin_fb, &pc);
pio_sm_set_consecutive_pindirs(PIO, FB_SM, FB_PIN, 1, GPIO_OUT);
}
@ -197,22 +202,22 @@ static void init_rx()
pio_program_t prog = {
.instructions = insn,
.length = sizeof(insn) / sizeof(*insn),
.origin = -1,
.origin = origin_rx,
};
pio_sm_restart(PIO, RX_SM);
pio_sm_clear_fifos(PIO, RX_SM);
if (pio_can_add_program(PIO, &prog))
prog.origin = pio_add_program(PIO, &prog);
origin_rx = pio_add_program(PIO, &prog);
pio_sm_config pc = pio_get_default_sm_config();
sm_config_set_in_pins(&pc, RX_PIN);
sm_config_set_wrap(&pc, prog.origin, prog.origin + prog.length - 1);
sm_config_set_wrap(&pc, origin_rx, origin_rx + prog.length - 1);
sm_config_set_clkdiv_int_frac(&pc, 1, 0);
sm_config_set_fifo_join(&pc, PIO_FIFO_JOIN_RX);
sm_config_set_in_shift(&pc, false, true, 32);
pio_sm_init(PIO, RX_SM, prog.origin, &pc);
pio_sm_init(PIO, RX_SM, origin_rx, &pc);
pio_sm_set_consecutive_pindirs(PIO, RX_SM, RX_PIN, 1, GPIO_IN);
}
@ -253,7 +258,7 @@ static void init_ad()
pio_program_t prog = {
.instructions = insn,
.length = sizeof(insn) / sizeof(*insn),
.origin = 0,
.origin = origin_ad,
};
pio_sm_restart(PIO, AD_SM);
@ -263,11 +268,11 @@ static void init_ad()
pio_add_program(PIO, &prog);
pio_sm_config pc = pio_get_default_sm_config();
sm_config_set_wrap(&pc, prog.origin, prog.origin + 15);
sm_config_set_wrap(&pc, origin_ad, origin_ad + 15);
sm_config_set_clkdiv_int_frac(&pc, 1, 0);
sm_config_set_in_shift(&pc, false, true, 32);
sm_config_set_out_shift(&pc, false, true, 32);
pio_sm_init(PIO, AD_SM, prog.origin, &pc);
pio_sm_init(PIO, AD_SM, origin_ad, &pc);
}
#define STEP_BASE ((UINT_MAX + 1.0) / CLK_SYS_HZ)