AMPERAGE
Posts: 4
Joined: Sat Nov 16, 2013 12:25 am

How to query which programs are controlling/writing to GPIO?

Fri Apr 03, 2020 5:08 am

I'm trying to find out if there is a way to query which programs/code are accessing and writing to the GPIO on an RPi 4. For some background:

I have a couple of GPIO pins which are behaving unexpectedly - one is toggling between high and low states at approx. 1Hz. Another is constantly set low, if I set it high then it will revert back to a low after about 1 second. The logical explanation is that there is code running somewhere (I suspect written by myself at some point) which is causing these behaviours, but I can't figure out where it is. It is not in any of the main programs that I run.

I don't call any programs in rc.local that would cause the behaviour, and the only alterations I've made in config.txt are to set a number of GPIO to outputs and drive them high. I haven't made any edits to bashrc, init.d tab, systemd or crontab that might auto-run scrips. Task manager doesn't give me any clues to where the culprit is either.

Installing a fresh version of NOOBS would almost certainly solve the problem, but I'm trying to avoid having to start from scrach and reinstall / setup the RPi again.

Is there any way to find out which code is causing the GPIO behaviour that I can't account for?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to query which programs are controlling/writing to GPIO?

Fri Apr 03, 2020 6:08 am

Not easy. If the process is using gpiomem for access, then you might be able to catch it with sudo fuser /dev/gpiomem, or /dev/mem for other processes. That still won't necessarily catch everything.

Which are the pins that are causing a problem? If one is GPIO4 (pin 7), it could be that the 1-wire interface is enabled and causing regular pulses on that pin (use lsmod and look for w1_gpio).
Unreadable squiggle

AMPERAGE
Posts: 4
Joined: Sat Nov 16, 2013 12:25 am

Re: How to query which programs are controlling/writing to GPIO?

Fri Apr 03, 2020 7:12 am

rpdom wrote: Which are the pins that are causing a problem? If one is GPIO4 (pin 7), it could be that the 1-wire interface is enabled and causing regular pulses on that pin (use lsmod and look for w1_gpio)
GPIO4 (pin 7) is the pin that is generating the pulses as you've suggested. I've run lsmod and it doesn't show w1_gpio enabled for pin 7 (I assume that "Used By" for lsmod refer to the physical pin and not the GPIO number.) The only item returned for pin 7 is:

Module: snd
Memory: 73728
Used By: 7 snd_compress,snd_timer,snd_bcm2835,snd_soc_core, snd_pc

I currently have GPIOs set to outputs, driven high with a pull up resistor set (gpio=0-27=op,dh,pu) in the config.txt. This clearly doesn't disable comms on the pins (a poor assumption on my behalf) - how can I ensure that for all GPIOs that I am using purely as digital outputs that any comms protocols are disabled? This may in fact be my issue instead of rogue code like I'd initially thought.

FYI the other pin causing issue is GPIO27 (pin 13).

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to query which programs are controlling/writing to GPIO?

Fri Apr 03, 2020 7:16 am

AMPERAGE wrote:
Fri Apr 03, 2020 7:12 am
GPIO4 (pin 7) is the pin that is generating the pulses as you've suggested. I've run lsmod and it doesn't show w1_gpio enabled for pin 7 (I assume that "Used By" for lsmod refer to the physical pin and not the GPIO number.) The only item returned for pin 7 is:
The "used by" number on lsmod is nothing to do with GPIO pins. It is how many other modules are using the listed module and their names.

If w1_gpio shows up at all, it is probably using pin 7 (GPIO 4) and you'll need to disable it.

Pin 13 (GPIO 27) is an odd one. I don't know of anything that specifically uses that pin.
Unreadable squiggle

AMPERAGE
Posts: 4
Joined: Sat Nov 16, 2013 12:25 am

Re: How to query which programs are controlling/writing to GPIO?

Fri Apr 03, 2020 7:27 am

rpdom wrote:
Fri Apr 03, 2020 7:16 am
AMPERAGE wrote:
Fri Apr 03, 2020 7:12 am
GPIO4 (pin 7) is the pin that is generating the pulses as you've suggested. I've run lsmod and it doesn't show w1_gpio enabled for pin 7 (I assume that "Used By" for lsmod refer to the physical pin and not the GPIO number.) The only item returned for pin 7 is:
The "used by" number on lsmod is nothing to do with GPIO pins. It is how many other modules are using the listed module and their names.

If w1_gpio shows up at all, it is probably using pin 7 (GPIO 4) and you'll need to disable it.

Pin 13 (GPIO 27) is an odd one. I don't know of anything that specifically uses that pin.
Gotcha, thanks for the clarification.

w1_gpio does not show up at all when I run lsmod. Are there any other culprits that you could suggest I look for?

User avatar
rpdom
Posts: 17275
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: How to query which programs are controlling/writing to GPIO?

Fri Apr 03, 2020 7:36 am

AMPERAGE wrote:
Fri Apr 03, 2020 7:27 am
w1_gpio does not show up at all when I run lsmod. Are there any other culprits that you could suggest I look for?
Sorry, I can't think of any. :(
Unreadable squiggle

Return to “Troubleshooting”