Memotech Bill
Posts: 85
Joined: Sun Nov 18, 2018 9:23 am

PINDIRS before or after PINS

Sun Apr 11, 2021 8:53 am

I don't know whether I have missed it, or whether the documentation is not sufficiently explicit.

In a PIO program, do you have to set a pin to output before you can write to it, or can you write the value first and then change the pin to output and have the value written written appear on the pin?

I am trying to write a PIO program to respond to a Z80 read request. Will this work?

Code: Select all

            pull                    ; Get the data to output
            out     pins 8          ; Send it to pins
            mov     osr !null       ; Fill OSR with ones
            out     pindirs 8       ; Set lower 8 pins (data) to output
Or do I need the following?

Code: Select all

            mov     osr !null       ; Fill OSR with ones
            out     pindirs 8       ; Set lower 8 pins (data) to output
            pull                    ; Get the data to output
            out     pins 8          ; Send it to pins
I realise that in this case it does not make much difference, but there could be cases where it is desirable to have a number of instructions set pins to a desired state, and then change the pin direction to output the pin state.

I am also curious as to a design decision. Why is PINDIRS not a destination for the MOV command?

There is a certain amount of non-uniformity in destinations for OUT, MOV and SET:

Code: Select all

Destinations            Sources
-------------------     -----------
out     mov     set     in      mov
---     ---     ---     --      ---
pins    pins    pins    pins    pins
x       x       x       x       x
y       y       y       y       y
null    -       -       null    null
pindirs exec    pindirs -       -
pc      pc      -       -       status
isr     isr     -       isr     -
exec    osr     -       osr     -
Whereas it could have been:

Code: Select all

Destinations            Sources
-------------------     -----------
out     mov     set     in      mov
---     ---     ---     --      ---
pins    pins    pins    pins    pins
x       x       x       x       x
y       y       y       y       y
exec    exec    -       null    null
pindirs pindirs pindirs pindirs pindirs
pc      pc      -       status  status
isr     isr     isr     isr     isr
null    osr     osr     osr     osr
I realise that there is no chance of the silicon being changed now.

hippy
Posts: 9703
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: PINDIRS before or after PINS

Sun Apr 11, 2021 1:41 pm

Memotech Bill wrote:
Sun Apr 11, 2021 8:53 am
I don't know whether I have missed it, or whether the documentation is not sufficiently explicit.

In a PIO program, do you have to set a pin to output before you can write to it, or can you write the value first and then change the pin to output and have the value written written appear on the pin?
I am sure that I read one can 'write then make output' in the RP2040 datasheet and I would expect that to apply to PIO as well as other direct GPIO register access.

Silicon will usually be such that one writes to an output latch whether a pin is set for output or not. Making it output simply connects the pin to that latch.

'Write then make output' has been the recommended practice for me on other chips when I have had to do that. It avoids the glitch if made output and not already set to the wanted level.

One of the problems with extensive documentation is actually finding the thing one wants to know. That gets easier the more one becomes familiar with it, and learns where one should be looking, and more unnecessary as one gets to understand a chip's workings.

Memotech Bill
Posts: 85
Joined: Sun Nov 18, 2018 9:23 am

Re: PINDIRS before or after PINS

Sun Apr 11, 2021 2:34 pm

hippy wrote:
Sun Apr 11, 2021 1:41 pm
Silicon will usually be such that one writes to an output latch whether a pin is set for output or not. Making it output simply connects the pin to that latch.
That is what I am hoping, but given what must be quite complex logic to avoid contention of all the possible controllers of a pin, I am not sure for the Pico.

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

Re: PINDIRS before or after PINS

Sun Apr 11, 2021 4:02 pm

values are only asserted on pins during the cycle you do the SET, OUT ... unless you set the sticky flag

Code: Select all

/*! \brief Set special 'out' operations in a state machine configuration
 *  \ingroup sm_config
 *
 * \param c Pointer to the configuration structure to modify
 * \param sticky to enable 'sticky' output (i.e. re-asserting most recent OUT/SET pin values on subsequent cycles)
 * \param has_enable_pin true to enable auxiliary OUT enable pin 
 * \param enable_pin_index pin index for auxiliary OUT enable
 */
static inline void sm_config_set_out_special(pio_sm_config *c, bool sticky, bool has_enable_pin, uint enable_pin_index) {

Return to “SDK”