Where in Pico MP sources gets default PWM frequency set to 1907Hz?

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Where in Pico MP sources gets default PWM frequency set to 1907Hz?

Post by HermannSW » Fri Dec 03, 2021 2:15 pm

https://github.com/micropython/micropyt ... /ports/rp2

Code: Select all

>>> 
MPY: soft reboot
MicroPython v1.17-140-g0be3b91f1-dirty on 2021-11-12; Raspberry Pi Pico with RP0
Type "help()" for more information.
>>> from machine import Pin,PWM
>>> p=PWM(Pin(18))
>>> p.freq()
1907                                                                            
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Where in Pico MP sources gets default PWM frequency set to 1907Hz?

Post by Roberthh » Fri Dec 03, 2021 3:42 pm

It looks as if this is not set at all in the MicroPython code. That could be added in mp_machine_pwm_make_new() of machine_pwm.c.

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: Where in Pico MP sources gets default PWM frequency set to 1907Hz?

Post by HermannSW » Fri Dec 03, 2021 3:54 pm

Thanks, 1907Hz seems to be the default setting from Pico poweron then.
I would expect there is an explanation for this frequency when looking up RP2040 datasheet.

It is same 1907Hz PWM frequency and duty 0 for all pins:

Code: Select all

>>>                                                                             
MPY: soft reboot                                                                
MicroPython v1.17 on 2021-09-02; Raspberry Pi Pico with RP2040                  
Type "help()" for more information.                                             
>>> from machine import PWM,Pin                                                 
>>> for i in range(2, 22+1):                                                    
...     p=PWM(Pin(i))                                                           
...     print(p.freq(), p.duty_u16())                                           
...                                                                             
...                                                                             
...                                                                             
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
1907 0                                                                          
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Where in Pico MP sources gets default PWM frequency set to 1907Hz?

Post by scruss » Fri Dec 03, 2021 7:44 pm

Looks like it's calculated like this in machine_pwm.c:
uint32_t pwm_freq = 16 * source_hz / div16 / top;

With source_hz = 1.25e8, div16 = 16 and top = 65534 (which looks like the max or default value), you get 1907 Hz.

PWM is quite complicated on the RP2040: long, boring thread here - RP2040 PWM - Algorithm time - Raspberry Pi Forums.

Important takeaway / gotcha: if you change PWM frequency, you're likely to change duty, as there's a complicated divider based on the CPU clock speed.

hippy
Posts: 130
Joined: Sat Feb 20, 2021 2:46 pm
Location: UK

Re: Where in Pico MP sources gets default PWM frequency set to 1907Hz?

Post by hippy » Sat Dec 04, 2021 9:17 pm

While a default could be set, it doesn't really matter what frequency is set when duty is 0. One would usually explicitly set a desired frequency before setting a duty.

I also recall that not all memory is cleared when MicroPython is soft rebooted so you can end up with what was previously set when some functions are called and optional parameters are not explicitly specified. So a good recommendation would be to never rely on defaults unless it can be proven they always default to the same value.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Where in Pico MP sources gets default PWM frequency set to 1907Hz?

Post by scruss » Sat Dec 04, 2021 11:24 pm

hippy wrote:
Sat Dec 04, 2021 9:17 pm
One would usually explicitly set a desired frequency before setting a duty.
Most simple ucs that I've used have a fixed PWM frequency. It also stays at 1907 if you increase the duty above 0.

Post Reply