GPIO Speed Problem.

darksoul
 
Posts: 45
Joined: Fri Nov 14, 2014 2:29 am

GPIO Speed Problem.

Sun Nov 23, 2014 12:34 am

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

Peter H
 
Posts: 58
Joined: Wed Nov 12, 2014 10:24 pm

Re: GPIO Speed Problem.

Sun Nov 23, 2014 12:59 am

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.

darksoul
 
Posts: 45
Joined: Fri Nov 14, 2014 2:29 am

Re: GPIO Speed Problem.

Sun Nov 23, 2014 2:20 am

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?

Peter H
 
Posts: 58
Joined: Wed Nov 12, 2014 10:24 pm

Re: GPIO Speed Problem.

Sun Nov 23, 2014 3:26 am

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?

darksoul
 
Posts: 45
Joined: Fri Nov 14, 2014 2:29 am

Re: GPIO Speed Problem.

Sun Nov 23, 2014 4:52 am

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.

darksoul
 
Posts: 45
Joined: Fri Nov 14, 2014 2:29 am

Re: GPIO Speed Problem.

Sun Nov 23, 2014 5:48 am

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);
   }
}

Peter H
 
Posts: 58
Joined: Wed Nov 12, 2014 10:24 pm

Re: GPIO Speed Problem.

Sun Nov 23, 2014 2:45 pm

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".

darksoul
 
Posts: 45
Joined: Fri Nov 14, 2014 2:29 am

Re: GPIO Speed Problem.

Sun Nov 23, 2014 3:45 pm

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)

Pyrofer
 
Posts: 61
Joined: Tue Oct 21, 2014 11:35 am

Re: GPIO Speed Problem.

Sun Nov 23, 2014 8:17 pm

You can also memory map the gpio for more speed.

Peter H
 
Posts: 58
Joined: Wed Nov 12, 2014 10:24 pm

Re: GPIO Speed Problem.

Sun Nov 23, 2014 9:57 pm

I see there's a gpio_ API although I haven't used it - that might prove faster than using the device file system mapping.

Next
Return to VoCore & VoCore+Dock

Who is online

Users browsing this forum: No registered users and 3 guests