Projects‎ > ‎

Raspberry Pi ISR


my workbench with the pi
---------------------------------------WHAT THE IRQTEST CODE DOES--------------------------------------------
This project started with my friend Larry announcing that he was building an A/D convertor which would
produce 1.4 million, 16 bit samples per second and was there any way I could capture the data to a file
for later analysis?  I had just received my rPi and was wondering if I could hook the A/D up to 16 gpio bits
with a 17th as /WR strobe.  Ignoring for the time being that the rPi doesn't have 16 contiguous gpios coming
out to a header, I set out to test the concept and see how good I could do.  It was quite an experience.

The first experiment was some simple test code which wrote a big file to a USB HDD, I discovered that the 
rPi can write 18MB/second to a file pretty much continuously. Larry's requirement ws 2.8MB/s, so far so good.

The next experiment used the wiringPi library ISR code to measure interrupt latency.
Which turned out to be 25us at a minimum and 75us or more possible.  The oscilloscope screenshot 
below shows the wiringPi ISR latency
ISR Latency in user mode
.  It is well known that Linux user level ISRs are slow, this wasn't going to work for me.

Check out the gpio hardware hacking here: http://elinux.org/RPi_Low-level_peripherals.

The next experiment was to make a kernel level ISR.  I was going to have to compile the kernel.  Uggh.
My host machine is a Macintosh which seemed like it should work and it did, sort of. You will find
instructions for compiling the kernel here: http://elinux.org/RPi_Kernel_Compilation.  The problem I
had was that the instructions aren't quite right and, on top of that, there are bugs which only happen
when cross compiling from Macintosh which made it quite a nightmare.  I was eventually able to build a
working kernel but, be warned, its not easy.  To make matters worse, I found when writing the ISR, that
all the help available on the web assumes you are either compiling for the machine you are running on or
,at best, cross compiling from another Linux machine.  


I ended up abandoning the Mac cross compile for a virtual machine running in VMWare fusion with Mint
Linux. http://www.linuxmint.com/ Both VMWare fusion and Mint Linux are great products, I recommend them.
Compiling for rPi under a Linux host has been far easier than on Macintosh, in my opinion, it is
worth the trouble of installing the VM.  Try it, you'll like it.

Oh, I should mention, since we are talking about writing a kernel driver that rPi uses kernel version 3.2.27
but nearly all the documentation and help on the web assumes you are running version 2.6.xx ... some of the
APIs have changed just to add to the fun.

Back to my project.  The code in this project implements a kernel level ISR tied to a rising edge interrupt
on gpio 23.  For testing, I am driving gpio 23 from a function generator and using an oscilloscope on gpio 23
and gpio 22 (where there is an LED).  On each rising edge of gpio 23, the ISR toggles the LED and writes a
16 bit incrementing value to a buffer which is shared with the user code.  Check out the oscilloscope
screenshot below, the latency is reduced by about 10x over the user level version.  
The cursors are 2.7us apart. Wow!

I ran the code today with an 11us interrupt rate (thats 90,909 interrupts per second!), it writes to the file
perfectly.  I didn't want to push it faster because I have seen interrupts out to 7 or 8us so, 11 is fast enough.
It won't quite fit Larry's needs but I'm going to start working on a hardware fifo so we don't have to service
every single word.

Enjoy!
Jay.

Read the "Please Read Me" file!

A zip archive with the sources is below.
ċ
irqtest.zip
(17k)
Jay Hamlin,
Feb 18, 2013, 8:08 AM
ą
Jay Hamlin,
Feb 15, 2013, 3:04 PM
ą
Jay Hamlin,
Feb 15, 2013, 3:04 PM
ą
Jay Hamlin,
Feb 15, 2013, 3:00 PM
Comments