slimhazard
Posts: 61
Joined: Sat Apr 03, 2021 8:47 pm

pio_sm_set_[in|out]_pins() vs pio_sm_set_consecutive_pindirs()

Tue Sep 28, 2021 8:56 pm

I'm confused about the differences between the function calls mentioned in the Subject line, or whether there is a difference, or circumstances under which one should be chosen rather than the other.

With pio_sm_set_consecutive_pindirs(), you set the last argument to true for out or false for in. From the wording of the SDK documentation, it sounds like that would do the same thing, in the same contexts, as pio_sm_set_out_pins() or pio_sm_set_in_pins(), respectively.

But in practice it doesn't seem that way. I'm currently working on a PIO program in which I'm using pio_sm_set_consecutive_pindirs() with true for out, and it is in fact generating output at the pin. But when I replace it with pio_sm_set_out_pins(), there's no output at all.

I guess at bit more investigation would work out when and why you use one but not the other, but right now I'm sort of cargo-cultishly going with the one that works. And hoping that someone on the forum knows the answer that will get me out of the cargo cult.

kilograham
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 962
Joined: Fri Apr 12, 2019 11:00 am
Location: austin tx

Re: pio_sm_set_[in|out]_pins() vs pio_sm_set_consecutive_pindirs()

Tue Sep 28, 2021 9:12 pm

pio_sm_set_out_pins configures which pins are targeted by the OUT (and MOV) instruction on that SM
pio_sm_set_in_pins configures which pins are targeted by the IN instruction on that SM
pio_sm_set_set_pins configures which pins are targeted by the SET instruction on that SM.
pio_sm_set_sideset_pins configures which pins are targeted by side set for that SM

pio_sm_set_consecutive_pindirs uses a given SM to reconfigure the pin directions (a per PIO setting). It does so by reconfiguring the SET pins (pio_sm_set_set_pins) above on that SM, and the executing SET PINDIRS instructions on that SM, before finally restoring the SM's pin config to what it was.

Note, that sm_config_set_out_pins and friends are probably more idiomatic (and produce smaller code) unless you are changing things on the fly after initial config

slimhazard
Posts: 61
Joined: Sat Apr 03, 2021 8:47 pm

Re: pio_sm_set_[in|out]_pins() vs pio_sm_set_consecutive_pindirs()

Tue Sep 28, 2021 10:52 pm

I read the SDK docs before posting the question -- I learned about RTFM long ago.

The problem is that they left me confused about configuring input and output for PIO, because the text seems to describe several different functions that do the same thing, but evidently they're not the same. In fact I had overlooked sm_config_set_out_pins(), which turns out to have been the one that got my code to work (that was my post over on the General forum, maybe I should have made it just one post).

The wording of the docs describe all of pio_sm_set_out_pins(), pio_sm_set_consecutive_pindirs() with true for the output argument, and sm_config_set_out_pins() as configuring pins for output. In my case, it turned out that I needed both "consecutive" and sm_config_set_out_pins(), in that order, but not pio_sm_set_out_pins(). Of course it's better to have code that's working than code that's buggy, but I still couldn't explain why that particular combination worked while the others didn't.

You mentioned "changing things on the fly after initial config", which goes toward explaining one of the differences. And it in fact that wasn't my use case, just configure once at initialization.

By the way, I'm not including the "set_set" and "set_sideset" functions, they're straightforward and do what you'd expect.

Return to “SDK”