golemparts / rppal

A Rust library that provides access to the Raspberry Pi's GPIO, I2C, PWM, SPI and UART peripherals.
MIT License
1.21k stars 96 forks source link

Set multiple pins at once? #122

Open superlou opened 1 year ago

superlou commented 1 year ago

Is it possible to set multiple pins at once, through something like GPIO register masking or a multiple set/multiple clear register? It sounds like the Python interface can set or clear multiple simultaneously. I am trying to write my own HUB75 driver, and it would be nice to be able to set and clear all 6 bits (R0, R1, G0, G1, B1, and B2) in a single write.

golemparts commented 1 year ago

Thanks for the suggestion! While RPPAL doesn't currently provide that functionality, It would technically be possible to combine register writes for multiple pins. I can imagine a batch_write function (and possibly a batch_read) that takes a list of OutputPins and states, and combines those into fewer writes.

The BCM2711 uses 4 registers to set/clear pin states (GPSET0, GPSET1, GPCLR0, GPCLR1). In the worst case scenario you'd go from writing 6 32-bit values to 4 (possibly as a single 128-bit write), plus the overhead (either in the library or the end-user code) of sorting through the list of pins. I think it's worth implementing as a convenience function for parallel interfaces, with any performance gains as a bonus.

I'll put this on the list of requested changes and see if we can get it into a future update.

superlou commented 1 year ago

Thanks for considering it. I'm actually getting pretty acceptable performance from what I can tell (though I don't have a logic analyzer at home for my hobby projects) setting them one at a time. If it's just a convenience function, I can just write a wrapper that takes an array of pins. In hindsight, trying to set them via the set/clear registers potentially adds a lot of support work since it might be unique to each Raspberry PI SOC.