question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support setting the GPIO Pin mode to ALT(n) on the Raspi

See original GitHub issue

The 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:closed
  • Created 4 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
krwqcommented, Nov 21, 2019

@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.

0reactions
Ellerbachcommented, Jul 1, 2021

[Triage] This is now implemented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change GPIO pin mode to ALT with Python RPi.GPIO
No. As far as I am aware RPi.GPIO only supports modes INPUT and OUTPUT. Modes ALT0, ALT1, ALT2, ALT3, ALT4, and ALT5 are...
Read more >
Raspberry Pi GPIO Pin Alternate Functions
ALT 1: The pins are used as a secondary memory bus. Due to the design of the Raspberry Pi, this is of no...
Read more >
GPIO: switching between ALT functions - Raspberry Pi Forums
Dear Group, I'm looking for a command-line utility to switch GPIO pins between their alternative functions. Does such a thing exist?
Read more >
Raspberry gPIo - SparkFun Learn
This tutorial was written originally for the Raspberry Pi Model B but applies for any Raspberry Pi Models ... To set a pin...
Read more >
Beginner GPIO Guide
A Beginner GPIO Guide that shows how to control GPIO Pins in Bash, Python and C. The examples require few hardware and are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found