Page 1 of 2
PWM period vs. frequency
Posted: Tue Apr 16, 2013 12:05 am
by Dmytro
I'm curious what's the difference between period and frequency in PWM API?
For me that's the same thing: period or frequency define the total length of the pulse(low + high), frequency = 1/period.
It's a bit confusing to define the period since it's very platform-dependent(clock speed, timer resolution, etc.). Also it's not clear why API allows to change period but not duty cycle.
IMHO it would be much more clear if API exposes for configuration only frequency and duty cycle in range from 0 to 100. The underneath calculation should be done by LLD itself.
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 5:14 am
by Giovanni
Both period an duty cycle are measured as number of clock cycles so a period makes more sense than frequency. Using frequency one would also have to deal with arithmetic rounding.
Giovanni
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 7:27 am
by Dmytro
I understand this point, but this field should be hidden from the user.
The user should only define frequency and duty cycle. The period should be calculated by LLD and stored in private field which might be used by HAL to calculate width for instance. But still it's possible to avoid exposing period to everyone.
Still curious, what's the purpose of changing period on the fly but not pulse width?
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 7:36 am
by Giovanni
You can change pulse width, changing period has been added because an user request.
Giovanni
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 7:46 am
by Dmytro
But only by means of pwmEnableChannel. There should be pwmChangeWidth and pwmChangeFrequency.
IMHO exposing pulse width in ticks is not portable. I can't run same code on different devices, or worse with different generations.
Say, I have 8-bit timer associated with PWM and at some point I need better resolution so I start to use 16-bit timer. This means that I have to change the period. If I'm not very familiar with the SoC I'm working on then I'm going to spend hours to figure out what is the correct value for the period.
Also if you set the period, setting the frequency does not make sense.
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 8:10 am
by Tectu
I have to agree that the PWM API is currently a bit of a mess. I'd like to see something similar with the pwmChangeWidth() / pwmChangeFrequency() as well.
~ Tectu
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 8:57 am
by Dmytro
Also it's a bit confusing to change the period as you change frequency and duty cycle with the call to pwmChangePeriod. Or the driver should internally recalculate the width accordingly ...
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 8:09 pm
by Giovanni
Hi,
If you have the cycle expressed as frequency and the width expressed as percentage or something like that then adjusting the duty cycle when you change the frequency would make sense.
However in the current implementation you measure both the period and the duty cycle as number of clock cycle so there is no need to adjust anything. A duty cycle of 1000 cycles is still 1000 cycles regardless the period.
About specifying the frequency, it is can be done by writing a layer above the driver if needed, there is no need to bring math inside the driver and have to deal with truncation errors and similar, you could not really hide details like the counter width to the user. Probably you would just hide potential problems. For example: "I programmed a frequency of 900Hz and a 50% duty cycle but I get a 40% duty cycle, fix your stuff" and instead it could be originated by rounding errors or insufficient resolution of the counter.
Another function to change the duty cycle is not really required IMHO because pwmEnableChannel() does exactly that, you could also write:
#define pwmChangeChannellDutyCycle pwmEnableChannel
and have the new API, no new code is really required. Probably it would be a good idea to add just that since it takes no space.
Giovanni
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 10:22 pm
by Dmytro
Thanks Giovanni for the explanation.
I would disagree about
Code: Select all
#define pwmChangeChannellDutyCycle pwmEnableChannel
as you may have more code there than needed to change the duty cycle or even worth the code is not suitable for that purpose. pwmEnableChannel states to enable channel and set duty cycle. On particular architecture you may need to stop the PWM or timer HW to change that which might not be done in pwmEnableChannel by design.
However in the current implementation you measure both the period and the duty cycle as number of clock cycle so there is no need to adjust anything. A duty cycle of 1000 cycles is still 1000 cycles regardless the period.
I agree but would argue that either LLD should drop frequency from configuration or provide a function to convert the frequency into timer cycles and report error.
Imagine I'm a newcomer(which is true) and I really don't know STM HW(this is also true). I take a demo and see that I have to specify period, frequency and width. I know from the past experience that PWM is defined as a tuple of frequency and duty cycle(width). I have found a macro that transforms 50% duty cycle into some ticks and I'm happy about that. Next, I put desired frequency, say 30kHz. Awesome. Now I need to define period. Wait, why did I have to define frequency then? What is the relation between frequency and period? I recall that F=1/T(F-frequency, T-period). Fine, I take a look into different demos to verify whether my math is correct and see following(real code in testhal/STM32F4xx):
Code: Select all
static PWMConfig pwmcfg = {
10000, /* 10kHz PWM clock frequency. */
10000, /* Initial PWM period 1S. */
What that suppose to mean? 10kHz gives me period of 100uS, 1S period should give me frequency of 1Hz. Is there anything wrong with the comments? I take another example(now from SPC):
Code: Select all
250000, /* 250kHz PWM clock frequency.*/
50000, /* Initial PWM period 0.2s.*/
Hum, one more mismatch. I go to google and ask what is relationship between frequency and period in terms of PWM and still can't find an answer that explains what I saw in the code. And to be honest I'm lost.
Also for me it's a bit confusing the presence of pwmChangePeriod since it is not obvious whether it changes the pulse width or not. According to documentation the pulse width is not adjusted(and according to you meaningful argument that LLD should not do the computation that may expose some error). However this function changes duty cycle. What is the main purpose of this function: change duty cycle or frequency?
If you initially set period to 1000(which corresponds to 1kHz on your HW) and width to 100 you have 10% duty cycle at 1kHz. When you change period to 2000, you divide the frequency and now your duty cycle is 5%.
It might be confusing for the beginners as me.
Re: PWM period vs. frequency
Posted: Tue Apr 16, 2013 10:30 pm
by Dmytro
Maybe I'm raising so stupid questions because I'm trying to fit PIC32's PWM HW into PWM HAL model which was designed for completely different HW.
