honda4life
Posts: 75
Joined: Thu Mar 15, 2012 7:27 pm

How the pwm works (in theory)

Fri Aug 31, 2012 11:04 pm

Hello everyone,

I'm used to use fast pwm with avr's, but I can't figure it out how it works on the pi.
You have the clock frequency of 19,2 MHz, you use a prescaler of 32 What gives 600 KHz.
I suppose this is a counter tick, but what with the range? It doesn't work like what I'm used to, the frequency is variable and can't find the solution.

Who can explain the pwm signal generating.

Thanks

User avatar
jojopi
Posts: 3421
Joined: Tue Oct 11, 2011 8:38 pm

Re: How the pwm works (in theory)

Sat Sep 01, 2012 3:06 am

honda4life wrote:You have the clock frequency of 19,2 MHz, you use a prescaler of 32 What gives 600 KHz.
I suppose this is a counter tick, but what with the range?
Right. Then for 10 bits of precision, say, you set a range of 0x400 = 1024. Each full output period lasts 1024 of the 600kHz ticks, or 1.706667ms. The valid data values are 0..1024. The selected number of output ticks are high, and the rest are low. So the average voltage is (data/1024) * 3.3V.

Now, in a traditional PWM implementation each output period consists of one contiguous run of high ticks and one run of low ticks. So an external frequency counter or scope will tell you that the output frequency is always 585.9375Hz and only the duty cycle varies from 0 to 100%. The BCM2835 supports this mode, which it calls mark-space, if you set MSEN=1 in the PWM CTL register.

But the default MSEN=0 mode is Pulse Density Modulation. This spreads the high and low ticks out as evenly as possible throughout the output period. For an output of 512/1024 it gives a pattern of 01010101..., which has frequency 300kHz. For an output of 409/1024 it gives 001001010010100101..., which is alternating between 200kHz 33% duty and 300kHz 50% duty.

PDM has the property that every substring of the output sequence is within one bit of the correct average value. So it is much smoother than traditional PWM (except for outputs at the extreme ends of the range, where it is the same).

honda4life
Posts: 75
Joined: Thu Mar 15, 2012 7:27 pm

Re: How the pwm works (in theory)

Sat Sep 01, 2012 7:22 am

Now I understand.
I've read about MSEN but it wasn't clear to me ;)

Thank you very much ;)

makbut
Posts: 25
Joined: Mon Nov 18, 2013 9:04 am

Re: How the pwm works (in theory)

Wed Feb 12, 2014 5:33 pm

Hello, thank you for the useful information about the MSENi bit.

I have some more questions. In the BCM2835-Arm-Peripherals at page 141 it says: "PWM clock source and frequency is controlled in CPRMAN" . Can someone explain what does it means?

(So the pwm clock is used to define the period which will then be multiplied by the RNGi Register value and give the output period. Am i correct?)

I didn't also understand what happens when the RNGi Register has a value below 32 ?

Sorry for posting in such an old topic, but i also have theory questions so i thought it would be ok here.

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

Re: How the pwm works (in theory)

Wed Feb 12, 2014 7:45 pm

As for CPRMAN: CPRMAN stands for Clock Power Reset MANager.
In the BCM2835 all the clocks and power and reset are controlled by this one central block.
(But in the datasheet you will find that the clock section is described separate from the power/reset manager.)

makbut
Posts: 25
Joined: Mon Nov 18, 2013 9:04 am

Re: How the pwm works (in theory)

Thu Feb 13, 2014 2:45 pm

I see, thank you.

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

Re: How the pwm works (in theory)

Thu Feb 13, 2014 3:45 pm

I've no idea what RNG is meant to do either. I do not understand what the documentation is saying. I just find a setting which seems to work and then leave it alone.

User avatar
jojopi
Posts: 3421
Joined: Tue Oct 11, 2011 8:38 pm

Re: How the pwm works (in theory)

Thu Feb 13, 2014 4:27 pm

makbut wrote:(So the pwm clock is used to define the period which will then be multiplied by the RNGi Register value and give the output period. Am i correct?)
I didn't also understand what happens when the RNGi Register has a value below 32 ?
(Yes.) In PWM/PDM modes there is nothing special about range values smaller or larger than 32. Obviously the choice of output values becomes very limited at short ranges. A range of 255 or 1024 would be typical.

The references to 32 are for when the peripheral is in serialiser mode, CTL.MODEi=1. Instead of DATi setting the number of high ticks during each full output period, it sets the actual pattern of ones and zeros to be sent. Because the DATi register is 32 bits wide, if RNGi<32 not all of its bits are sent, and if RNGi>32 the extra ticks are padded as zeros.

makbut
Posts: 25
Joined: Mon Nov 18, 2013 9:04 am

Re: How the pwm works (in theory)

Tue Feb 18, 2014 8:32 pm

Now it makes perfect sense. Thank you very much!

Return to “Interfacing (DSI, CSI, I2C, etc.)”