question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Abnormal waveform of Raspberry Pi Linux GPIO driver

See original GitHub issue

Recently, I tested the speed of Raspberry Pi Linux GPIO driver, and observed the square wave output on the oscilloscope. I found that the output waveform is very strange. It only outputs high level in a very short time, and compared with WiringPi ©, the speed is very very slow (although I know it will be slow, I didn’t expect the gap will be so big). I have two questions:

  1. The existing Raspberry Pi driver should have some problems in speed, although it can be used normally. Can we further optimize and simplify the code to improve the running efficiency.
  2. At the same time, it can be observed that the speed of LibGpiodDriver is not very fast. If there is no logic problem in C# code, can we further improve the execution efficiency of P/Invoke. Of course, I think it should be very difficult to solve this problem.

Test result

Drivers Language Library Version Average Frequency
RaspberryPi3Driver C# System.Device.Gpio 1.3.0 363 KHz
SysFsDriver C# System.Device.Gpio 1.3.0 1.74 KHz
LibGpiodDriver C# System.Device.Gpio 1.3.0
libgpiod 1.2-3
198 KHz
WiringPi C e9821ab 17.8 MHz

Test program

C#

int pin = 6;
GpioController controller;

Console.WriteLine("Select GPIO driver: ");
Console.WriteLine("1. SysFsDriver; 2. LibGpiodDriver; 3. RaspberryPi3Driver ");

string key = Console.ReadLine();
switch (key)
{
    case "1":
        controller = new GpioController(PinNumberingScheme.Logical, new SysFsDriver());
        break;
    case "2":
        controller = new GpioController(PinNumberingScheme.Logical, new LibGpiodDriver());
        break;
    case "3":
        controller = new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver ());
        break;
    default:
        Console.WriteLine("Exit");
        Environment.Exit(0);
        return;
}

using (controller)
{
    controller.OpenPin(pin, PinMode.Output);
    Console.WriteLine("Press any key to exit.");

    while (!Console.KeyAvailable)
    {
        controller.Write(pin, PinValue.High);
        controller.Write(pin, PinValue.Low);
    }
}

C

#include <wiringPi.h>

int main (void) {
  int pin = 2;
  
  wiringPiSetup();
  pinMode(pin, OUTPUT);

  while(1) {
    digitalWrite(pin, HIGH);
    digitalWrite(pin, LOW);
  }

  return 0;
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
krwqcommented, Mar 18, 2021

@ZhangGaoxing can you try the same measurements but with while (!Console.KeyAvailable) replaced with while (true)?

wiringPi seems to be LGPL so we can’t look at the sources to see what they use - looking at the speed I’d assume some sort of direct memory access.

I think there are some optimizations we could potentially do in the RPI3Driver:

0reactions
raffaelercommented, Nov 12, 2022

Probably not here, but I often use Benchmark.NET and you would be surprised to see how the optimizer can do a huge difference. I also see apparently small and insignificant changes making a huge difference in benchmarks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GPIO speed - Raspberry Pi Forums
Need a low latency link between a processor and an FPGA, PCIe has way too much latency for us. As the eMMC bus...
Read more >
GPIO Linux Device Driver Basic using Raspberry PI
This is the Linux Device Driver Tutorial Part 35 – GPIO Linux Device Driver Basic using Raspberry PI. This is the Series on...
Read more >
Real-Time Linux Testbench on Raspberry Pi 3 using ...
The Xenomai Cobalt approach was evaluated on a Raspberry Pi (RPi) 3 using its. General-Purpose Input/Output (GPIO) pins and a latency test.
Read more >
How to use Raspberry Pi GPIO pins with Ubuntu
This means packages such as RPi.GPIO no longer function properly with newer kernels. This post by Dave Jones (waveform) explains the reasons for...
Read more >
23. API - Pins — GPIO Zero 1.6.2 Documentation
Uses the lgpio library to interface to the local computer's GPIO pins. The lgpio library simply talks to Linux gpiochip devices; it is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found