Weird GPIO interaction
Alan Adams (2486) 1149 posts |
I’m running two of my programs (in BASIC, plus a little bit of assembler) on an rpi3B. One, entirely in BASIC, uses IIC_Control to send and receive data from an interface connecting a 1-wire device to the IIC bus. The other uses the GPIO module calls XGPIO_ReadEvent within a TickerV service routine to check the status of 2 of the GPIO pins, and sets a pollword bit to indicate which has changed state. If both these programs run together, then I get spurious pollword returns in the second program. It’s always bit 16 set, which means a transition detected on GPIO pin number 4. (I also check pin 5, which never gets these spurious returns.) Why? If there’s something IIC related to do with pin 4, I could modify the hardware to use a different pin, and alter the software to match. I don’t want to do that though if there’s any random element involved. It’s unlikely to be a major problem, because when the second program is in use, the first will not be running. It’s annoying though, as during debugging it’s useful to be running both. |
Sprow (202) 1158 posts |
Which port is this? ie. do you mean GPIO16 or GPIO48. That aside, why not just mask the event bits you’re not interested in. It’s a polled interface, so no harm collecting the event and just shrugging it off. |
Alan Adams (2486) 1149 posts |
Sorry, I don’t understand the question. If it helps, the code fragment that does the testing is below.
|
Stuart Swales (8827) 1357 posts |
You need to have a bitset of events that you are interested in – don’t go looking for, and then arbitrarily clearing down, bits that you have no interest in – others may depend on them. [Edit: Apologies – I saw constants like startpin, finishpin, and a nextpin label and naively assumed a loop was going to be involved without looking further] |
Alan Adams (2486) 1149 posts |
I’m only testing and resetting bits 4 and 5, for GPIO4 and GPIO5. I can’t remember where I found the documentation for GPIO_ReadEvent and GPIO_WriteEvent – the documentation on the ROOL website has placeholders for those functions. It’s working (except when another application conflicts). There’s a lot more discussion of this here: https://www.riscosopen.org/forum/forums/11/topics/14797?page=1 I picked those pins because by default they seem to be I/O pins, not special functions. This issue suggests that GPIO4 may have a different use. Looking now at one of the many online pin descriptions, I see one where GPIO4 is described as GPCLK0. All the other pin layout diagrams I have found show GPIO4 as a general purpose I/O pin. |
Alan Adams (2486) 1149 posts |
Also going through the code, I see then FNswi is unsafe. It does register and state preservation round the switch to SVC mode before entering the swi. However it was using the bottom 5 bits of the PCSR instead of the bottom 4. It’s a hangover of trying to learn assembler from old online code examples. Now altered. |
Julie Stamp (8365) 474 posts |
Did you try
beforehand? I would of thought that would stop any other ports (like GPCLK0) from affecting the pin. |
Alan Adams (2486) 1149 posts |
Here’s the initialisation section
My other program is using IIC calls – does that cause a similar initialisation, overwriting this one? It’s been suggested that GPIO4 is related to writes to the SD card. The other program may be writing to the SD card occasionally. I should be able to test that simply by copying a file on to it. Done. No problem caused. The IIC to 1-wire module I’m using has a socket that goes on pins 1-10 of the GPIO header. I don’t know whether pin 7 (GPIO4) is connected in the module, but I suspect it might be. However a close look at the PCB suggests not. All this however is leading me in the direction of changing the pin used from GPIO4 to GPIO6. Ten minutes with a soldering iron, and 30 minutes updating the software and making sure I’ve found ALL the references. |
Stuart Swales (8827) 1357 posts |
Probably not the cause of Alan’s problem(?), but I had a look at the Pi GPIO HAL: I think it might be missing a PHPSEI / PLP wrapper for the common routine called by EdgePollStatus – if we have one fg task polling the event status, it reads, furtles bits, then writes that GPIO register, but a bg task (could be unrelated, looking at different pins) might have changed that GPIO register value in the background, so a stale one could then be written by the fg task. Compare with the WriteMode function which does have the atomic GPIO r/w. |
Alan Adams (2486) 1149 posts |
That time was a bit optimistic. Ten minutes to move a wire from one pin to another, an hour to track down a faulty push-button switch that decided to fail JUST AS I OPENED THE BOX. Half an hour to swap the PI3 for another one, after I managed to apply 24 volts to a GPIO pin. (Anyone got a use for dead pi’s? That’s two now.) Another hour tidying up the wiring to make it easier to work on the thing out of the box, and another 20 minutes changing two capacitors to shorten the debounce times, now that I’ve got rid of the spurious second pulses. There was only one other reference to the pin number. Judging by this: https://www.raspberrypi.com/documentation/computers/os.html#gpio-and-the-40-pin-header GPIO4 is no longer a general purpose I/O pin by default. Most other online versions of this diagram differ. However, this: https://pinout.xyz/pinout/gpclk# suggests that GPIO5 and GPIO6 are also clock outputs. WTF? |
Chris Hall (132) 3554 posts |
GPIO 4 is used by the Witty Pi so is assumed to be general purpose. I don’t think it is implied that GPIO 4, 5 and 6 are not general purpose, so far as RISC OS is concerned. The Witty Pi 3 documentation suggests that the 1-wire interface (which uses GPIO 4 by default in Linux) is disabled or re-assigned by commenting out the driver in the Linux firmware.
RISC OS firmware does not enable the 1-wire interface. |
Alan Adams (2486) 1149 posts |
Nevertheless, when running a program that polls the IIC bus every 10 seconds, GPIO4 gets the occasional transition, at times that seem random. The program also writes to the SD card every ten minutes. Changing from GPIO4 to GPIO6 seems to have eliminated the problem. It’s been running for 15 hours so far, with no spurious events being recorded. |