Page 1 of 2

GPIO Speed Problem.

Posted: Sun Nov 23, 2014 12:34 am
by darksoul
Hi guys,
I compile an app which just toggles GPIO "0" ON and OFF.

Code: Select all
system("echo out  > /sys/class/gpio/gpio0/dirrection");
while (1)
{
  system("echo 1  > /sys/class/gpio/gpio0/value");
  system("echo 0  > /sys/class/gpio/gpio0/value");
}


After that i connected my oscilloscope to the output and recorded the state change.
So from this experiment i can see the max speed that gpio can toggle.

I was expecting very high and fast state change results, but unfortunately I was surprised how slow the gpio is,
For me, after 8-bit AVR processors, this brakes everything. :((

So my question is: is there a better way to work with GPIO to get better results?

Readings from my oscilloscope.
Image

Thanx
-D

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 12:59 am
by Peter H
You're creating a new process for every state change - that's an extremely expensive approach. It would be far more efficient to have your application open the device and then write to it directly within the loop. If that still isn't fast enough you may find that you get a higher frequency using a PWM driver. If you want really high frequency you might even bypass the OS and hit the hardware directly.

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 2:20 am
by darksoul
really high frequency you might even bypass the OS and hit the hardware directly.


Thx for your answer, any help with getting direct access to the hardware?

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 3:26 am
by Peter H
It's pretty unlikely that you need to access the hardware directly and given your initial solution I suspect you would have a huge learning curve. What are you trying to achieve?

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 4:52 am
by darksoul
for example, I'm trying to hook on the 74HC595 serial shift out ic,
I can stick with the 10ms per pulse, but this will make huge delay in my app.
I'm reading now what they call "access thru sysFS", but no luck there.

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 5:48 am
by darksoul
Well after surfing the internet i solved my problem.
Now in average the state change time is 300us. much much more better then 10ms :D

Image

Code Example that i used:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main(void)
{
        system ("echo out > /sys/class/gpio/gpio0/direction");

   FILE *p=NULL;
   for(int i=0;i<10000;i++)
   {
      p = fopen("/sys/class/gpio/gpio0/value","w");
      fprintf(p,"%d",1);
      fclose(p);
      p = fopen("/sys/class/gpio/gpio0/value","w");
      fprintf(p,"%d",0);
      fclose(p);
   }
}

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 2:45 pm
by Peter H
It isn't necessary to close and re-open the device each time you write to it. You can also save a little time by avoiding formatting the string every time you write to the device, since you're always writing "0" or "1".

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 3:45 pm
by darksoul
Alright, i will check that out, thx for your replies.

and sorry about the stupid questions, i'm 8-bit AVR guy, when you directly write the port)

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 8:17 pm
by Pyrofer
You can also memory map the gpio for more speed.

Re: GPIO Speed Problem.

Posted: Sun Nov 23, 2014 9:57 pm
by Peter H
I see there's a gpio_ API although I haven't used it - that might prove faster than using the device file system mapping.