.Net Core seems to max out reading GPIO pin at about 1000 / second. How to go faster?

554 Views Asked by At

I wrote a C# .Net Core 3.1 app to read pin state changes on the GPIO of an Orange Pi Zero. I am using the System.Devices.Gpio IoT library. I am running Armbian Focal (based on Ubuntu 20.04).

I am trying to read the frequency of a square wave signal, which makes a pin on the GPIO rise and fall. So I'm trying to read how many rising (or falling) edges occur in 1 second.

I have tried both polling the pin in a loop, and registering to events/callbacks to pin changes, and I can't seem to read any faster than about 1000 times per second (1 Khz)

Unfortunately, I need to read at least 5000 times per second (and preferably more).

I don't think the limitation is on the hardware side. I think it's the fact that .Net Core runs on top of a CLR which is an extra layer that is slowing things down.

I am looking at writing this portion of the app in Python (I'll leave the rest of the app in C# / .Net Core), but I don't know if it will speed things up? I know Python is sort of the de facto programming language for SBC's nowadays, which is why I'm considering it (lots of community support and samples). But it's an interpreted language. Would it not be slower than C# which is compiled? But then again, maybe Python talks directly to Linux, unlike C# .Net Core which goes through the CLR. Also Python is baked into Ubuntu so maybe that tight integration has some benefits.

I know C would be the quickest, but also the hardest of the languages to write in (other than Assembly). I'm not too familiar with either C nor Python, but I thought Python would have the less steep learning curve.

In summary, my question is: Is there anything I can perhaps do in .Net Core to speed up reading the GPIO? Would perhaps using a third party library instead of System.Devices.Gpio help (I doubt it)? Is the limitation perhaps on the hardware or maybe in Amrbian (Ubuntu) (I doubt it)? Should I even bother trying it in Python or would it be a waste of time and I should go straight to C?

Sorry, I know my question is probably not according to StackOverflow policies. I was just wondering if anyone maybe has had any experience in reading GPIO pin state changes really fast (as in to measure frequency in the KHz range) - on an Orange or Raspberry Pi or something like that...

I'd appreciate any pointers.

Thank you.

1

There are 1 best solutions below

0
On

Although re-writing the code in a faster language (perhaps Python, definitely C) would have helped, at the end we decided to put a hardware assist in place (as recommended by some on other posts I've read). We are going for Raspberry Pi RF Frequency Counter solution.

Thanks to @r_ahlskog and PMF for your inputs however. It's very much appreciated.