User avatar
HermannSW
Posts: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
joan
Posts: 14006
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: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
joan
Posts: 14006
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: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
HermannSW
Posts: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
joan
Posts: 14006
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: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
HermannSW
Posts: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
joan
Posts: 14006
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: 1997
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: 2481
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

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: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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?
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
joan
Posts: 14006
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: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

User avatar
HermannSW
Posts: 1235
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

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.
bookmark list: https://stamm-wilbrandt.de/en/Raspberry_camera.html

https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://github.com/Hermann-SW/fork-raspiraw
https://github.com/Hermann-SW/userland
https://twitter.com/HermannSW

Return to “General discussion”