Repository navigation
Description
If gpio interrupts on different gpios occur on almost the same time, sometime some of them seem to get lost.
This is what I do to test this issue.
Create 100 Interrupts with pigpio and this code (wave.c):
#include <stdio.h>
#include <pigpio.h>
#define PULSE 200
#define GPIO 12
int main()
{
int i, wave_id, len;
gpioPulse_t pulse[PULSE];
uint32_t g_mask;
g_mask |= (1<<GPIO);
if (gpioInitialise()<0) return 1;
for (i=0; i<PULSE; i += 2) {
pulse[i].gpioOn = g_mask;
pulse[i].gpioOff = 0;
pulse[i].usDelay = 1000;
pulse[i+1].gpioOn = 0;
pulse[i+1].gpioOff = g_mask;
pulse[i+1].usDelay = 1000;
}
gpioSetMode(GPIO, PI_OUTPUT);
gpioWaveClear();
gpioWaveAddGeneric(PULSE, pulse);
wave_id = gpioWaveCreate();
gpioWaveTxSend(wave_id, PI_WAVE_MODE_ONE_SHOT);
len = gpioWaveGetMicros();
gpioDelay(PULSE*1000);
gpioTerminate();
return 0;
}Connect the gpio to two other gpios (in my case connect 12 to 4 and 22.
Export gpios in sysfs and enable interrupt:
echo 4 > /sys/class/gpio/export
echo rising > /sys/class/gpio/gpio4/edge
echo 22 > /sys/class/gpio/export
echo rising > /sys/class/gpio/gpio22/edge
count interrupts with this code (cnt_irqs.c):
#include <stdio.h>
#include <poll.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#define GPIO_FN_MAXLEN 32
#define POLL_TIMEOUT 5000
#define RDBUF_LEN 5
int main(int argc, char **argv) {
char fn[GPIO_FN_MAXLEN];
int fd,ret,cnt;
struct pollfd pfd;
char rdbuf[RDBUF_LEN];
struct timespec old_t, new_t;
memset(rdbuf, 0x00, RDBUF_LEN);
if(argc!=2) {
printf("Usage: %s <GPIO>\nGPIO must be exported to sysfs and have enabled edge detection\n", argv[0]);
return 1;
}
snprintf(fn, GPIO_FN_MAXLEN-1, "/sys/class/gpio/gpio%s/value", argv[1]);
fd=open(fn, O_RDONLY);
if(fd<0) {
perror(fn);
return 2;
}
pfd.fd=fd;
pfd.events=POLLPRI;
ret=read(fd, rdbuf, RDBUF_LEN-1);
if(ret<0) {
perror("read()");
return 4;
}
printf("value is: %s\n", rdbuf);
clock_gettime(CLOCK_REALTIME, &old_t);
cnt = 0;
while(1) {
memset(rdbuf, 0x00, RDBUF_LEN);
lseek(fd, 0, SEEK_SET);
ret=poll(&pfd, 1, POLL_TIMEOUT);
if(ret<0) {
perror("poll()");
close(fd);
return 3;
}
if(ret==0) {
printf("timeout\n");
break;
}
ret=read(fd, rdbuf, RDBUF_LEN-1);
if(ret<0) {
perror("read()");
return 4;
}
cnt++;
}
close(fd);
printf("%d interupts\n", cnt);
return 0;
}What I do now is open 3 shells on the pi. In short order I start ./cnt_irqs 4, ./cnt_irqs 22 and sudo ./wave
I would now expect 100 interupts as a result on both cnt_irqs, but I usually get something between 95 and 99.
If I start only one cnt_irqs then I get the expected 100 interrupts.
when I look at bcm2708_gpio_interrupt in bcm2708_gpio.c,
static irqreturn_t bcm2708_gpio_interrupt(int irq, void *dev_id)
{
unsigned long edsr;
unsigned bank;
int i;
unsigned gpio;
for (bank = 0; bank <= 1; bank++) {
edsr = readl(__io_address(GPIO_BASE) + GPIOEDS(bank));
for_each_set_bit(i, &edsr, 32) {
gpio = i + bank * 32;
generic_handle_irq(gpio_to_irq(gpio));
}
writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank));
}
return IRQ_HANDLED;
}The writel(0xffffffff, __io_address(GPIO_BASE) + GPIOEDS(bank)); seems to be the problem, as it resets all bit. So if an interrupt occurs during the for_each_bit loop, it will not get handled.
If I add writel(1<<i,__io_address(GPIO_BASE) + GPIOEDS(bank)); in the loop instead, it seems to work for me.
I got the idea from here where similar issues were discussed.
Activity
popcornmix commentedon Jan 7, 2015
I agree clearing the explicit bit is correct.
I'll make the change.
gpio: Only clear the currently occurring interrupt. Avoids losing int…
notro commentedon Jan 7, 2015
Just for reference, the upstream code is sligtly different when it comes to when that bit is cleared.
Probably it increases the edge-interrupt detection speed. At least when it's just two very close interrupts. Like detection of both edges in a narrow pulse. I have no idea of fast it needs to go to loose an edge though, with only bit clearing on the end. Depends of course on how long the interrupt handler takes to run. Just speculation here, thought I'd mention it.
Ref: http://lxr.free-electrons.com/ident?i=bcm2835_gpio_irq_handler
popcornmix commentedon Jan 7, 2015
Interesting. I can see the logic that if handling the interrupt is slow, you can catch a subsequent interrupt a bit quicker.
I don't have a good way of testing this, so I'll go for the simple change for now, and if someone has a test setup (like a pulse generator connected to gpio) we can try implementing the upstream way.
popcornmix commentedon Jan 7, 2015
@chaosjug
Can you (from a latest 3.12 rpi-update kernel) update to this:
https://dl.dropboxusercontent.com/u/3669512/temp/kernel_gpio_clear.img
(rename to kernel.img) and confirm it works. If it does I'll push change to 3.12 kernel.
chaosjug commentedon Jan 10, 2015
As my first testing method was not very robust, I tried another approach.
I'm now generating two interrupts 10us appart with this code:
I also added new debug output to the isr:
connecting ths two output GPIOs (now 5 and 6) to two other GPIOs and enabling the interrupts leads to this output:
So the first time, the register is read, only the first interrupt is there. The second time the register is read, only the second interupt is there. This suggests to me, that we actually do not need to clear the bit at all (at least for edge interrupts)!
This might be different for level interrupts but I can not activate them from sysfs, so I could not test this.
I also tested the latest rpi-update kernel and the one @popcornmix provided.
With the original kernel I loose the second interrupt most of the time, with the other kernel I get both. So this is definitively an improvement.
popcornmix commentedon Jan 10, 2015
Looks like bcm2708_gpio_irq_unmask clears the interrupt and I believe that is called every interrupt, so clearing in bcm2708_gpio_interrupt is redundant.
@chaosjug can try try removing the writel to GPIOEDS in bcm2708_gpio_interrupt and check it works okay?
pelwell commentedon Jan 10, 2015
I'm wondering if the unmask call is necessary in the interrupt handler. The lirc-rpi module seems to work without it.
popcornmix commentedon Jan 10, 2015
Are you suggesting not assigning:
pelwell commentedon Jan 10, 2015
No, that wasn't what I meant. The lirc-rpi interrupt handler used to explicitly call the irq_unmask method, and that seems to not be necessary. But if the interrupt framework is also calling unmask then it is probably there for a reason.
gpio: Only clear the currently occurring interrupt. Avoids losing int…
notro commentedon Jan 10, 2015
I tried to look at the interrupt code in the kernel, but couldn't determine if irq_mask/unmask is called during interrupt processing.
@chaosjug Can you add printk's to those functions to determine whether they are called or not?
The reason I ask, is that pinctrl-bcm2835 protects irq_enable/disable with spinlocks, whereas bcm2708_gpio don't. Just to rule out a possible race condition.
Spinlock on uniprocessors with preemption results in:
spin_lock -> preempt_disable
spin_unlock -> preempt_enable
Ref: Linux Kernel Architecture ch 5.2.2 Spinlocks
chaosjug commentedon Jan 10, 2015
I tried removing the writel and it still works, but I only tested with a rising edge interrupt.
I guess this will be different for high/low level interrupts. In that case the handler has to make sure that the level is back to the not active state before clearing the bit, otherwise we would just get the next interrupt. So the distinction in the upstream version mentioned by notro makes sense to me.
chaosjug commentedon Jan 10, 2015
I added printk to irq_mask/unmask and kept the printk in gpio_interrupt from above and removed the writel in gpio_interrupt. Then I generated two interrupts 10us apart and I get this result:
notro commentedon Jan 11, 2015
@chaosjug thanks. So racing is possible, but I guess not very likely for normal use cases.
71 remaining items
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
Add bcm2708_gpio driver
kernel: Bump to 3.12.36
Merge pull request raspberrypi#760 from ojeda/kallsyms