filon
Posts: 4
Joined: Tue Jul 16, 2019 8:51 pm

Strange GPIO behavior

Sat Jul 20, 2019 4:45 pm

I found something strange. I set one pin as OUTPUT and the other as INPUT and connected them together. The idea was to toggle OUTPUT pin and be able to read it's current value on INPUT pin. Nothing special. My code is:

Code: Select all

for(;;)
  {
    gpioSet(OUTPUT_PIN);	//set 1 on output (takes ~17ns)
    for(int j=0;j<100;j++){}	//wait  (takes ~570ns)
    z1= read(INPUT_PIN);		//read input (takes ~ 150ns)

    gpioClear(OUTPUT_PIN);	//set logic 0 on output pin
    for(int j=0;j<100;j++){}
    z2= read(INPUT_PIN);

    gpioSet(OUTPUT_PIN);	//repeat everything
    for(int j=0;j<100;j++){}
    z3= read(INPUT_PIN);

    gpioClear(OUTPUT_PIN);
    for(int j=0;j<100;j++){}
    z4= read(INPUT_PIN);

   printf ("%u %u %u %u\r\n", z1,z2,z3,z4) ; 
   
  }
As You can see, I toggle OUTPUT pin (which takes 17 ns), then I have empty loop to wait for ~570ns, to make sure that everything settles up and finally i read my INPUT pin. Later I repeat everything, gather 4 values and print them on console. As a result I would expect to get toggling 0's and 1's, but actual output of that code was:

Image

As You can see, the output is ok most of the time, but every now and then there is 0 where I would expect to see 1, and 1 where I would expect 0. What??? Why? Isn't 570ns enought time for Raspberry to settle up?

When I increase wait time in empty loop to around 1us, errors happen very rararely, but they still happen. They stop to exist when wiat time is 2us or longer. But should Raspberry be able to handle toggling output and reading it's value with 570ns waiting time without any problem? It's not even 2Mhz.

I tested the same behavior with my Arduino Due and with simmilar timings (write and read operation took there ~40ns each) there were no problem at all. The output was perfect even for 50ns waiting time (it's 10 times shorter waiting time than I tested for Raspberry).

Can someone expain me what is going on? Why Raspberry needs so much delay to read value which it just toggled?

dl324
Posts: 122
Joined: Mon May 06, 2019 7:33 pm
Location: Pacific Northwest, USA

Re: Strange GPIO behavior

Sat Jul 20, 2019 7:21 pm

Raspian is not a real time operating system and the OS will sometimes swap out a process to do other things that are either higher priority or are at the same priority but haven't gotten their share of CPU time.

Arduino doesn't have an operating system and resources aren't shared.

User avatar
joan
Posts: 14656
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Strange GPIO behavior

Sat Jul 20, 2019 7:39 pm

I notice that occasionally, e.g. if I set a GPIO high and then immediately read it the GPIO sometimes still reads back as low. I don't know how long the GPIO take to reach a stable state.

filon
Posts: 4
Joined: Tue Jul 16, 2019 8:51 pm

Re: Strange GPIO behavior

Sat Jul 20, 2019 9:10 pm

Thanks for answers.

@dl324 I was aware of that, but I thought that process swapping can only work in my favour (increasing waiting time), but now when I think about it I see how it might not.

Additional questions:

1. )Do You think that incorporating "General Purpose GPIO Clocks" and/or PWM to toggle pin can be a good idea, as it should be independent from OS thus stable and predictable? I found this old thread from this forum about that: https://www.raspberrypi.org/forums/view ... hp?t=67741

2) Do You think that disabling interupts for short periods of time (disable just before writing and enabling them again after reading pin) could be a good idea to have more stable results and faster signal propagation?

Return to “Troubleshooting”