User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Pi Zero hardware PWM precision

Mon Aug 22, 2016 3:04 pm

In this posting I used my new 100MHz logic analyzer first to measure 10KHz PWM signal created by Pi Zero (can I trust the logic analyzer?), then to verify 26.21MHz square wave frequency generated by this

Code: Select all

...
  for (;;)
  {
    *GPSET = GPIO17;
    *GPCLR = GPIO17;
  }
...
Pi Zero program (based on WiringPi library code analysis and leaving only the base minimum) and measured with Arduino Due tc_lib library before, and finally to measure highest hardware PMW frequency PI Zero can generate (19.2/4=4.8MHz).

Today I wanted to know how exact the measured 4.8MHz (with 100MHz sample rate) are, and how different two Pi Zero measured the same time are. I did measure 1 second (roughly) @100MHz, both Pi Zeros on pin GPIO12, and both initialized for PWM by these steps:

Code: Select all

gpio -g mode 18 pwm
gpio -g pwm-ms
gpio -g pwmc 2
gpio -g pwmr 2
gpio -g pwm 18 1
This is the left end of the measurement zoomed in (right click for details):
Image

This is a view where both Pi Zero curves are opposite:
Image

And this is the right end of the measurements:
Image

The full period count is 4949043 for first Pi Zero, for the second it is 494034, both for 1.031s.
So the first has a real average frequency of 4949043/1.031=4.800236MHz, the second of 4.800227MHz.
The difference of full period counts for both Pi Zero is less than 9 for 1 second, quite precise.

Hermann.
Last edited by HermannSW on Thu Sep 01, 2016 10:17 pm, edited 1 time in total.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

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

Re: Pi Zero hardware PMW precision

Mon Aug 22, 2016 3:43 pm

You can generate higher frequencies than 4.8MHz with hardware PWM.

E.g. http://abyz.co.uk/rpi/pigpio/pigs.html#HP

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PMW precision

Mon Aug 22, 2016 6:44 pm

Hi,

thank you for that link, you were right, higher frequencies can be achieved!
I thought the "gpio" frequencies as integer divisors of 19.2MHz were hardware PWM.
Minimal range is 2 (with "1" specifying 50% duty), minimal clock divider is 2 (1 results in 5KHz only).
Thus maximal frequency with "gpio" command is 19.2/(2*2)=4.8MHz.
But that must be software PMW because hardware PWM achieves much higher frequencies.

What I saw with 100MHz logic analyzer is that only integral divisor of 125MHz frequencies look good.
The highest "perfect" frequency is 25MHz:
Image

25031460/1.001253490=25000122.5=25.000123Mhz

Both, 125/4=31.25MHz as well as 125/3=41.67MHz are precise as well, although the curves do not look perfect anymore with my 100MHz logic analyzer (24$ were fine for that, but going to 200MHz jumps to >250$ minimal price even on aliexpress).

I am not sure whether 125/2=62.5MHz and 125MHz do work (125MHz is outside of measuring range, and 100MHz logic analyzer shows 21.45MHz for 75MHz; but the curve shows high for 1/6 and low for 5/6 although I did specify 50% duty by "pigs hp 12 75000000 500000"). That can be answered by somebody with logic analyzer or scope in GHz range.

Hermann.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

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

Re: Pi Zero hardware PMW precision

Mon Aug 22, 2016 7:05 pm

The maximum hardware PWM frequency depends on the clock being used to drive PWM. wiringPi uses the 19.2MHz crystal, pigpio uses the 500MHz PLLD.

The PWM clock can be set from one of the sources OSC (19.2MHz), HDMI (216MHz), PLLD (500MHz), or PLLC (1000MHz).

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PMW precision

Tue Oct 11, 2016 1:06 am

> but going to 200MHz jumps to >250$ minimal price even on aliexpress
>
Not true anymore, have not bought this, but you can get 400Msps for 70$ (only):
https://www.aliexpress.com/wholesale?ca ... c+analyzer

Hermann.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PMW precision

Tue Dec 27, 2016 7:38 pm

I got that 400Msps logic analyzer as christmas present, nice!
I used Pi Zero again to play with it:
Image

Downloading the DSLogic Ubuntu software and following the build+install instructions just worked.

Sampling at 200Msps looked good, but with 400Msps I saw small 2.5ns peaks:
Image

I was not sure whether Pi Zero generated these little peaks, or something else was responsible.
I read that the logic analyzer has shielded cable fly line connector.
So I tried to separate both cables from above photo and used another GND pin of Pi Zero.
This works fine with 400Msps sampling:
Image

Finally I used pigs to generate 50MHz PWM signal (pigs hp 12 50000000 500000).
I learned that DSLogic software allows to analyze "as PWM":
Image

Hermann.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

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

Re: Pi Zero hardware PMW precision

Tue Dec 27, 2016 8:08 pm

Does the 50 MHz signal look clean? It is rather faster than the GPIO are expected to reliably work.

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PMW precision

Fri Jan 06, 2017 9:52 am

Yes, 50MHz signal looks clean.
If I read pigpiod reference correctly, 200MHz should be possible:
http://abyz.co.uk/rpi/pigpio/

I took 1M samples @400Msps several times and on two different Pi Zeros just to be sure. I found no way to save period and duty cycle in a single file, these two files are a sample of Pi Zero 50MHz measurement for above 50% duty cycle pigs command:
https://stamm-wilbrandt.de/en/forum/pwm ... period.txt
https://stamm-wilbrandt.de/en/forum/pwm_50MHz_duty.txt

Simple bash analysis of period ...

Code: Select all

$ cut -f3 -d\; pwm_50MHz_period.csv | sort -u
17.5 ns
20.0 ns
22.5 ns
PWM: Period
$ grep ";20.0" pwm_50MHz_period.csv | wc --lines
130141
$ grep ";17.5" pwm_50MHz_period.csv | wc --lines
370
$ grep ";22.5" pwm_50MHz_period.csv | wc --lines
538
$ 
... and duty cycle:

Code: Select all

$ cut -f3 -d\; pwm_50MHz_duty.csv | sort -u
37.500000%
42.857143%
44.444444%
50.000000%
PWM: Duty cycle
$ grep ";37.500000%" pwm_50MHz_duty.csv | wc --lines
32333
$ grep ";42.857143%" pwm_50MHz_duty.csv | wc --lines
370
$ grep ";44.444444%" pwm_50MHz_duty.csv | wc --lines
538
$ grep ";50.000000%" pwm_50MHz_duty.csv | wc --lines
97808
$ 
Hermann.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PMW precision

Fri Jan 06, 2017 10:36 am

Gnerating 125MHz signal seems to be clean as well, but for measuring this cleanly more than 400Msps are needed:
Image

The repeating period pattern is "...|10.0, 7.5, 7.5, 7.5, 7.5|..." which corresponds to 125MHz:

Code: Select all

$ cut -f3 -d\; pwm_test.csv | sort -u
10.0 ns
7.5 ns
PWM: Period
$ grep ";10.0 ns" pwm_test.csv | wc --lines
65693
$ grep ";7.5 ns" pwm_test.csv | wc --lines
261933
$ 
$ bc -ql
(10*65693+7.5*261993)/(65693+261993)
8.00118863790335870314
1000/((10*65693+7.5*261993)/(65693+261993))
124.98143029184239156877
Hermann.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

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

Re: Pi Zero hardware PMW precision

Fri Jan 06, 2017 11:21 am

That's a surprise. I had assumed the GPIO were only good for about 30MHz (because of electrical pad considerations).

jdb
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2696
Joined: Thu Jul 11, 2013 2:37 pm

Re: Pi Zero hardware PMW precision

Fri Jan 06, 2017 11:47 am

If you're not driving any appreciable load (e.g. logic analyzer inputs) and you are only toggling a single pin, it's quite possible to get a "reasonable" logic-level signal at up to ~100MHz. It'll be more a sine wave than a square wave so the edges will be poorly defined, though.
Rockets are loud.
https://astro-pi.org

User avatar
Gert van Loo
Posts: 2487
Joined: Tue Aug 02, 2011 7:27 am

Re: Pi Zero hardware PMW precision

Fri Jan 06, 2017 12:35 pm

joan wrote:The maximum hardware PWM frequency depends on the clock being used to drive PWM. wiringPi uses the 19.2MHz crystal, pigpio uses the 500MHz PLLD.

The PWM clock can be set from one of the sources OSC (19.2MHz), HDMI (216MHz), PLLD (500MHz), or PLLC (1000MHz).
I just wanted to come back to the above remark:
The precision depends on what your basic time source is. In the Raspberry-Pi that is the crystal oscillator: 19.2 MHZ.
Thus the 500MHZ PLLD is running faster and gives smaller steps but as it is ultimately derived from the 19.2 MHz
it has the same accuracy. In effect it is less accuracy as a PLL frequency is derived from a control loop which
will always jitter more then the reference frequency it compares against.

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PWM precision

Fri Jan 06, 2017 5:26 pm

http://abyz.co.uk/rpi/pigpio/pigs.html#HC says:
This function is only valid if the pigpio main clock is PCM. The main clock defaults to PCM ... The actual frequency set is 250 million / steps.
Maximal frequency that can be set with HP is 125,000,000. So PCM clock is 250MHz?
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

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

Re: Pi Zero hardware PWM precision

Fri Jan 06, 2017 5:46 pm

HermannSW wrote:http://abyz.co.uk/rpi/pigpio/pigs.html#HC says:
This function is only valid if the pigpio main clock is PCM. The main clock defaults to PCM ... The actual frequency set is 250 million / steps.
Maximal frequency that can be set with HP is 125,000,000. So PCM clock is 250MHz?
The clock is 500MHz. The minimum divisor is 2 so the maximum clock rate is 250MHz. PWM is pointless if you can't at least have one step between on and off which is why the limit become 125MHz.

Try one of the clock GPIO for a 250 MHz signal, e.g.

pigs hc 4 250000000 # 250 MHz clock on GPIO 4.

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PWM precision

Fri Jan 06, 2017 10:42 pm

Thanks, "pigs hc 4 250000000" works (BCM 4, GPIO.7, pin 7), can be seen for frequencies <=125MHz.

But 400Msps logic analyzer is not capable to display that, smallest period on it is 5ns, whereas 250MHz corresponds to 4ns period.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PMW precision

Wed Feb 22, 2017 7:37 pm

HermannSW wrote:... Downloading the DSLogic Ubuntu software and following the build+install instructions just worked.
That was partially true, it did work on family computer in living room with Ubuntu 14.04 (see photo above).

It did not work on RHEL Linux, nor on very old Thinkpad T42 laptop with Ubuntu 12.04.

Today I tried again to compile DSView software on the T42.
The problem was that the required package "qt5-default" could not be installed under Ubuntu 12.04.

I decided to install Ubuntu 14.04 on the old T42 (Bios date is from 2007, processor is an Intel Pentium M @1.6GHz (Pentium Ms started shipping in 2003), but has 15.4" 1400x1050 display).

First I downloaded "i386" iso image from:
http://de.releases.ubuntu.com/trusty/
Then I stored that (1GB) image on a USB stick ("sudo dd if=ubuntu... of=/dev/sdc").
Finally I booted from USB stick (click "Access IBM" key on startup, press F12 and select USB media to boot from).

That ended in an error message that PAE is missing for that laptop :-(

I googled and found this cool posting:
http://askubuntu.com/questions/464376/c ... 387#464387

After appending "forcepae" to boot line Ubuntu 14.04 did boot and started from USB stick without issues.
I clicked "install Ubuntu" icon there and after installation the reboot came back successfully without any actions needed.

The next step was to download and install DSView software, worked exactly as described in INSTALL file.
Then I downloaded and installed Arduino IDE.

400Msps logic analyzer worked easily, and the old T42 is no problem because all data gets captured inside the logic analyzer first, and only after the capture ended, the data gets downloaded to the laptop.

Good to have this old laptop for 400Msps logic analyzer, work with Arduinos and ssh/scp into Raspberries.

Hermann.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PWM precision

Sat Apr 11, 2020 8:12 am

HermannSW wrote:
Fri Jan 06, 2017 10:42 pm
Thanks, "pigs hc 4 250000000" works (BCM 4, GPIO.7, pin 7), can be seen for frequencies <=125MHz.

But 400Msps logic analyzer is not capable to display that, smallest period on it is 5ns, whereas 250MHz corresponds to 4ns period.
Time has passed, and I did install DSView software on Raspbian two days ago
viewtopic.php?f=33&t=270197&p=1641091#p1640608

and learned that "buffer mode" is needed to be able to capture with 400MHz yesterday:
viewtopic.php?f=33&t=270197&p=1641091#p1641091

What I read is that clock frequency of GPIO4 hardware clock is 500MHz, but minimal divisor is 2.
And that maximal PWM frequency (with duty cycle 50%) is 125MHz.

I did start 250MHz hardware clock command "pigs hc 4 250000000" again on Pi3B and measured with Pi4B:
hc25.png
hc25.png
hc25.png (53.07 KiB) Viewed 4930 times
I have not found out how to determine frequency over a range with DSView yet (manual is no real help with that, I know how to do that with my Salea Logic 100Msps logic analyzer). So I just exported and determined frequency "by hand" as 150MHz -- reason for not seeing 250MHz is the too bad for this task 2.5ns resolution of 400MHz sampling [light travels only 75cm(!) in 2.5ns]:

Code: Select all

🍓 wc --lines DSLogic-la-200411-083951.csv 
3001346 DSLogic-la-200411-083951.csv
🍓 head -1000000 DSLogic-la-200411-083951.csv | tail -1
0.003332465,0
🍓 head -3000000 DSLogic-la-200411-083951.csv | tail -1
0.0099974325,0
🍓 echo '10^6/(0.0099974325-0.003332465)' |bc -ql
150038240.99667402729270622849
🍓 
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PWM precision

Sun Dec 27, 2020 6:00 pm

Until today my view on electical signals was digital, since I only had logic analyzers.

I got another nice Christmas present, Hanmatek DOS1102 oscilloscope:
(2 Channels and 7 Inch/18 cm TFT LCD Screen, 110MHz Bandwith, 1GSa/s real-time sampling rate)
https://www.amazon.de/gp/product/B0833X ... le_o03_s00


After doing initial calibration my older son and I looked for some other signals besides 5V 1Hz callibration signal.
(screenshot onto front mounted USB stick was super easy by just pressing "Copy" button)

I took Pi0W and used pigpio pigs command hp to set hardware PWM on GPIO13.
pigpio documentation for hp command states "Frequencies above 30MHz are unlikely to work":
http://abyz.me.uk/rpi/pigpio/pigs.html#HP

They work, but curves do not look like a rectangular wave above 25MHz:
x.anim.gif
x.anim.gif
x.anim.gif (214.67 KiB) Viewed 3621 times

I did zoom in horizontally, here only 10MHz and 83.33MHz curves are shown with more details:
x.anim.gif
x.anim.gif (2)
x.anim.gif (103.29 KiB) Viewed 3621 times
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

guym
Posts: 47
Joined: Fri Jun 14, 2019 5:28 pm

Re: Pi Zero hardware PWM precision

Tue Dec 29, 2020 2:28 pm

The capacitance loading on the pin acts as a low pass filter at the upper frequencies. You'll need a special probe to reduce parasitic capacitance. The ground clip itself can be problematic.

I also wonder if the GPIO pulldown resistor - on by default - is influencing your result.

cleverca22
Posts: 6042
Joined: Sat Aug 18, 2012 2:33 pm

Re: Pi Zero hardware PWM precision

Tue Dec 29, 2020 2:50 pm

as an extra datapoint, here is some "square" waves i bit-banged on an rpi4b

first is a ~80mhz signal
Image

and then a ~125mhz signal
Image

you can see that something is very clearly struggling at 125mhz, but since my scope is only rated for 100mhz, its not clear what component i should blame
the scope is also using repetitive store to cheat and measure faster signals

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 13009
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Pi Zero hardware PWM precision

Tue Dec 29, 2020 7:32 pm

cleverca22 wrote:
Tue Dec 29, 2020 2:50 pm
as an extra datapoint, here is some "square" waves i bit-banged on an rpi4b

you can see that something is very clearly struggling at 125mhz, but since my scope is only rated for 100mhz, its not clear what component i should blame
the scope is also using repetitive store to cheat and measure faster signals
Nyquist's theorem. Disregard any results above half the sampling frequency.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

cleverca22
Posts: 6042
Joined: Sat Aug 18, 2012 2:33 pm

Re: Pi Zero hardware PWM precision

Tue Dec 29, 2020 7:35 pm

6by9 wrote:
Tue Dec 29, 2020 7:32 pm
Nyquist's theorem. Disregard any results above half the sampling frequency.
the math as i cranked it up one step at a time says the numbers are in the right ballpark, and i think the repetitive store lets you get around nyquist a bit, by interleaving 8 separate sets of samples, that each have a slightly different delay from the trigger point
but i do need to get a better scope and get better traces, and cant fully trust the numbers my current scope gives

guym
Posts: 47
Joined: Fri Jun 14, 2019 5:28 pm

Re: Pi Zero hardware PWM precision

Tue Dec 29, 2020 8:34 pm

It looks like the RPi's output slew rate may be the limiting factor with the swing down to a little over 1V. Could varying the pad strength have any affect?

... but a scope with more bandwidth is needed to see what is really going on.

cleverca22
Posts: 6042
Joined: Sat Aug 18, 2012 2:33 pm

Re: Pi Zero hardware PWM precision

Tue Dec 29, 2020 9:54 pm

guym wrote:
Tue Dec 29, 2020 8:34 pm
... but a scope with more bandwidth is needed to see what is really going on.
yep, thats why i didnt investigate further, i need better hw before the results are meaningful
and with how fuzzy things are, i dont want to trust anything purely digital (logic analyzer or another pi), it must be something analog

User avatar
HermannSW
Posts: 5245
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Pi Zero hardware PWM precision

Tue Dec 29, 2020 10:28 pm

My new oscilloscope has 110MHz Bandwith and 1GSa/s real-time sampling rate.
So I am pretty sure that the curves I showed are real.
And they showed that no PWM signal above 25MHz was anything near to rectangular wave.
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Return to “General discussion”