Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 8082

SDK • Re: Handling (multiple) input pins in a PIO program and making sense of their data

$
0
0
If you drive something that's supposed to be open-collector with an ordinary output, then you risk a clash when you re driving it to '1' and some other device is driving it to '0' - giving an undefined logic level, and over-stressing the outputs (potentially in the long term causing hardware damage). So don't do that - if it's supposed to be open-collector, drive it that way.

RP2040 doesn't explicitly support open-drain (== open-collector) outputs. However, you can achieve exactly the same effect by setting an output to '0' and then switching the output enable on and off - when it's enabled, RP2040 will drive that '0' onto the wire, when disabled it will not drive the wire and the pull-up resistor will cause it to rise to '1'.

Again, there isn't a separate 'output enable' setting for the I/O pins, but there's a 'direction' setting which does the same thing - PINDIRS. When the corresponding bit in PINDIRS is set to '1' then the pin is an output and drives whatever is in its output register; when the PINDIRS bit is '0', the pin is an input and is not driven.

So for an ordinary output from a PIO program you set the PINDIRS bit to '1' at the start of the program (pio_sm_set_consecutive_pindirs() lets you do this from the ARM side without wasting valuable PIO instructions), and then twiddle the output bit with "OUT PINS" or "SET PINS" instructions. For an open collector output, you set the output to '0' at the beginning of the program (maybe a SET PINS instruction, but again you can force this from the ARM side to save instructions), and then twiddle the output during your program with "OUT PINDIRS" or "SET PINDIRS".

Note that this mechanism effectively inverts the data - writing a '1' to PINDIRS causes the output to go to zero; normally you just take care of that in your program logic, but if it is causing you particular inconvenience then there's a feature in the GPIO pad controls to invert the sense of PINDIRS (see 2.19.6.1 in the RP2040 datasheet, or gpio_set_oeover() in the SDK). The I2C example uses this feature to shorten the PIO code, as I2C is also an open-collector bus.

Statistics: Posted by arg001 — Mon Apr 14, 2025 5:06 pm



Viewing all articles
Browse latest Browse all 8082

Trending Articles