[Dprglist] Hardware PWM as Clock on the Raspberry Pi

Murray Altheim murray18 at altheim.com
Tue Mar 2 01:58:10 PST 2021


Hi,

Following on from our discussion ("PID-tuned Clock in Python?") I thought
to investigate David's suggestion of using one of the Raspberry Pi's
hardware clocks rather than a separate microcontroller board just to
supply a 20Hz pulse. Just to recap, the idea of either a software PWM or
any kind of software "system clock" (in particular, one written in Python
over Linux) won't be reliable as it's a victim of the Pi's system load.
So David had suggested that since a Pi is, under the hood, just an ARM
CortexA53, why not use one of the hardware clocks? So I gave it a try,
and executive summary: it works.

The Pi can dynamically modify its clock speed, and I've not quite figured
out if the hardware clocks are affected by that or not. I don't think so (?).

In any case, from what I can tell, two of the four hardware clocks on the
Pi can be used for hardware-based pulse-width modulation (PWM), and despite
being thoroughly confused at various points along this journey, the final
result was actually not at all difficult to implement and works just fine.

I found the solution in several places, but the most clearly written was
at [1]. I'll summarise below.

The solution requires no Python but it's possible to enable/disable the
clock via a Python or C++ script. The clocks appear on BCM pins 18 and 19.
I was already using pin 18 for something else and only needed one, so the
following is for a single clock on pin 19 (the original page [1] also
includes pin 18).

So:

* Edit /boot/config.txt.
* Add a line:  dtoverlay=pwm-2chan
* Save the file.
* Reboot.

This creates a new directory at:

   /sys/class/pwm/pwmchip0/

If you write a "1" to the "export" file, e.g.:

   sudo echo 1 > /sys/class/pwm/pwmchip0/export

this will create a new directory

   /sys/class/pwm/pwmchip0/pwm1/

which will contain a bunch of configuration files. We're going to
modify three of them.

For a 20Hz system clock the period is 50ms, but this must be written
to the configuration file in nanoseconds. Likewise, the 50% duty cycle
is expressed as half of the period and written to a separate file.
Then a "1" is written to the 'enable' file to start the timer:

   echo 50000000 > /sys/class/pwm/pwmchip0/pwm1/period
   echo 25000000 > /sys/class/pwm/pwmchip0/pwm1/duty_cycle
   echo 1 > /sys/class/pwm/pwmchip0/pwm1/enable

And that's it. I'd hooked an LED to BCM pin 19 through a resistor to
ground and it's currently vibrating away at 20Hz. I haven't tested
this with a frequency meter (I don't have one) but I believe the
hardware clocks are *not* affected by system load, so I now have a
reliable 20Hz clock.

I went ahead and changed the permissions of the 'enable' file:

   sudo chmod 777 enable

so I can turn the clock on and off from Python using a quick and
dirty script, e.g.,

   https://github.com/ifurusato/ros/blob/master/lib/hardware_clock.py

This script will likely not be a long term solution, just testing.

One thing to note in particular is that rebooting the Pi will remove
the /sys/class/pwm/pwmchip0/pwm1/ directory, so we'd need to create
a bash script or some way of doing all this on boot, maybe as a
systemctl service (which would get around the sudo/permissions issue).

I can now free up Itsy Bitsy M4 Express (a very nice little board)
for a much more dignified task...

I hope this was helpful. Thanks to David for the suggestion.

Cheers,

Murray

----
PS. I'm not claiming to be correct here, so if something is wrong
please somebody jump in and provide a correction. I know you will...

[1] https://blog.oddbit.com/post/2017-09-26-some-notes-on-pwm-on-the-raspb/
...........................................................................
Murray Altheim <murray18 at altheim dot com>                       = =  ===
http://www.altheim.com/murray/                                     ===  ===
                                                                    = =  ===
     In the evening
     The rice leaves in the garden
     Rustle in the autumn wind
     That blows through my reed hut.
            -- Minamoto no Tsunenobu



More information about the DPRGlist mailing list