Page 1 of 1

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

Posted: Fri Dec 03, 2021 2:15 pm
by HermannSW
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                                                                            
>>> 

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

Posted: Fri Dec 03, 2021 3:42 pm
by Roberthh
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.

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

Posted: Fri Dec 03, 2021 3:54 pm
by HermannSW
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                                                                          
>>> 

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

Posted: Fri Dec 03, 2021 7:44 pm
by scruss
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.

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

Posted: Sat Dec 04, 2021 9:17 pm
by hippy
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.

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

Posted: Sat Dec 04, 2021 11:24 pm
by scruss
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.