Generate High Frequency Square wave on a GPIO pin

Dear All,

Our Aim is to generate a 40Khz square wave on a GPIO pin.

We have tried several methods to achieve this in Android and Linux

  1. By writing a C++ program using the GPIO Class in Linux
  2. Writing a shell script to export the GPIO and modify the GPIO files
  3. We also tried step 2 in Android

The problem we are facing is that File Operations are extremely slow, We measured it and it takes 2ms to open, modify and save the file for OS.

But to generate 40Khz square wave we need to toggle the GPIO pin at 12.5 Micro Seconds

How do we achieve this in Dragon Board 410C ?

Regards
Shashank

Hi @Shashank_Jagadish

Toggling GPIO at micro second accuracy is not possible in Linux userspace AFAIK.

But, you can achieve this in a rather hard way :wink: by writing a FIQ handler in kernel space to toggle the GPIO at a fixed interval (12.5 us)

PoC: http://free-electrons.com/blog/fiq-handlers-in-the-arm-linux-kernel/

Please note that the above mentioned example is for ARMv7 but Dragonboard410c is based on ARMv8, where exception handling is quite different.

Hope this helps!

Thanks,
Mani

You could do it the hard way by using some form of real-time support in Linux (rt patchset or xenomai) but the end result might not be what you’re looking for. A general-purpose operating system unfortunately can’t do what would be considered a fairly simple task.

It might be easier to just add a mezzanine card on the board with a micro-controller that exposes some PWM pins to allow you to generate a precise waveform.

Another possibility, please look at the suggestion in this post: Toggle a GPIO pin using Register Access