Support setting the GPIO Pin mode to ALT(n) on the Raspi
See original GitHub issueThe mode of each GPIO Pin can be set to IN, OUT or up to 5 alternate modes (ALT0-ALT5). For instance, BCM Pin 12 can be set to IN, OUT, PWM0 (ALT0), SMI (ALT1), DPI (ALT2), AVEOUT (ALT3), AVEIN (ALT4) or JTAG TMS (ALT5). See the BCM Chip reference documentation or https://pinout.xyz/ for the alternate functions of each pin. Note that this page was not yet updated for the PI4, which has even more alternate functions (one of them being 4 different I2C channels spread over different pins, see prelimary datasheet, page 10: https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2711/rpi_DATA_2711_1p0_preliminary.pdf).
By default, all pins are set as input after a reboot, except those for which a driver module is loaded (i.e. if the i2c overlay is loaded, Pins 2 and 3 are set to ALT0 after startup). The current mode of each pin can for instance be queried using the gpio
tool by entering gpio readall
at the command line. Nothing prevents changing the pin mode to anything else after bootup though. So even if the i2c module is loaded, setting pin 2 to output is still possible, but of course i2c commands won’t work after that.
The library is currently only supporting setting the modes to input or output (together with the pullups, but that setting is independent, even though it only makes sense for an input pin). If once set to output, there’s no way of setting it back to ALT0 mode which is required for I2C, except a reboot or a manual command using an alternate library (i.e. gpio -g mode 2 ALT0
will do).
While it usually will not make much sense to change the mode of the I2C pins (if an I2C device is in use) a programming error may still lead to such a wrong setting during development or after a hardware reconfiguration by the user. Some modes may even be used interchangeably by the same program, for instance OUT and PWM for driving a LED.
Suggestion Extend PinMode to add members for ALT0-ALT5. Implement the setting of these modes in RaspberryPiDriver (should be straight-forward, is only a different combination of mode bits in the same registers as the Input/Output mode is set). For drivers or hardware where these modes are not supported, NotSupportedException is thrown (same as happens now if setting mode to InputPullUp if that setting is not supported).
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (7 by maintainers)
Top GitHub Comments
@pgrawehr I actually think of GPIO as pure input/output with or without pull up/down.
The alternative functions should not be exposed directly IMO. Rather than that I think GpioController should have something like:
PwmChannel CreatePwmChannel(int pin)
/IEnumerable<int> PwmPinsWithHardwareSupport()
and similar for other functions which will set the mode to the correct alternative form under hood.Implementation can use Alt0-5 wherever they want but for users I think the API should be much simpler and they should not need to know what Alt means.
[Triage] This is now implemented.