jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Kernel soft PWM module?

Mon May 09, 2016 5:52 am

I've tried using the soft PWM functions in the wiringPi library, but they seem to just use a real-time thread and toggle the GPIO pin from userspace, albeit with memory mapped I/O, so the toggling itself is fast.
(The thread in turn seems to use polling for hand-off when starting, so start time is only precise to about a millisecond.)

The end result is that the tone sounds "grungy."

Would it be possible to write a kernel module/driver that used timer interrupts to toggle the pin? Interrupt latencies may still cause glitches, but it's likely to be less jittery than a thread-based solution.

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

Re: Kernel soft PWM module?

Mon May 09, 2016 8:15 am

It's certainly possible to write a PWM kernel module. Whether it would be an order of magnitude better or not depends on what sort of job you do.

Given that you are only working with low frequencies a simpler solution would be to use my pigpio waves.

From the command line to generate a 1200 Hz tone on GPIO 4

pigs m 4 w # make GPIO 4 an output

pigs wvag 16 0 416 0 16 417 # 416 on, 417 off, 1200.5 Hz.
pigs wvcre
pigs wvtxr 0 # Assuming 0 returned by wvcre
sleep 1
pigs wvhlt

Note: wvag use triplets of GPIO on,GPIO off, microsecond delay. 16 indicates GPIO 4 (1<<4).

jwatte
Posts: 203
Joined: Sat Aug 13, 2011 7:28 pm

Re: Kernel soft PWM module?

Mon May 09, 2016 4:20 pm

What's the general theory of operation behind that library? What makes it better than a high-priority thread?

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

Re: Kernel soft PWM module?

Mon May 09, 2016 4:45 pm

jwatte wrote:What's the general theory of operation behind that library? What makes it better than a high-priority thread?
The GPIO level changes are timed by hardware, not software threads.

Return to “Advanced users”