PwmOut generate wrong signal

(Nicolas COMTE) #1


Today I tested both a Nucleo F411RE and a Nucleo F303K8 with a very simple code to generate a PWM of 1000ms of period and 0.5f of pulse width so 500ms. I tried many pin on both boards.
But nothings works and to visualise I plugged a led on it and it didn't blink. The led say illuminated.

After few minutes and a lot of value randomization.... I wrote this code that "blink" the led.

#include "mbed.h"

PwmOut my_pwm(PC_7);

int main(void) {

  return 0;

But with an oscilloscope I can read a period of 40ms and 20ms of pulse width... So the pulse width is respected as it is half of the period time, but the period time is... Not really what I expected ! And only the value of 10000 works to blink the led.

Does anyone found a bug on the mbed PwmOut library or anything that can explain my problem ? Or Did I misused the PwmOut ?

(Martin Kojtal) #2

cc @bcostm

(Nicolas COMTE) #3

More strange, when I do that, it works perfectly well.

#include "mbed.h"

int main(int argc, char const *argv[]) {
  PwmOut toto(D9);

  return 0;

(Laurent Meunier) #4


From what I have seen so far, I think the PWM is primary meant to run short periods. The spec says
Pulse lengths and waveform periods can be modulated from a few microseconds to several milliseconds

And the PWM driver as of now is configured to use a 1us unit.
As the period register is 16 bits, this will allow to define period up to 65535us = around 65ms.
When you set the period to 10000ms (eg 10000000us), it will be actually masked to only the 16 valid bits which gives you 38528 us. That probably explains the 40ms you're reading.

So my conclusion is that for now, you should limit to 65ms maximum period. Maybe this could be improved somehow but at least this explains your observations.

Also I don't see a way to return an error to the caller of the pwmout API ... maybe this can be improved as well?

(Nicolas COMTE) #5


Nice analyse !
Sounds right for me..
Anyway I don't really need a pwm with 10sec of period but it was interesting to have the right information about why it does not work :slight_smile:

Do you know where I can found the sources, and may patch the problem then do a merge request ?

(Laurent Meunier) #6

OK. I think the source code is here you're looking at is here

Or you may replace the lib "mbed" by "mbed-dev" if you're working on the online compiler

(Laurent Meunier) #7

Hi Nicolas
you may also log an issue here so that the topic can be tracked

(Laurent Meunier) #8

Hi Nicolas
I finally opened an issue:
Limited PWM period range #1682
You may try out a prelim patch that is still under validation on my side.

I'd be interested on your feedback

(Nicolas COMTE) #9

Hi LaurentM_ST

Good news !
I'm interested in how you patched that if you have 2 minutes to explain ?

If you need any help to try your new version I'm in.
Follow the issue on github.

(Laurent Meunier) #10

I had to go through the reference manual of the chip (from and check the way the pwm timers work.
The code was using a fixed prescaler setting which is fine for any period below 65ms, so we had to made use of this prescaler to also address wider range periods.
Please tell me if you can test the fix I propose here:


(Nicolas COMTE) #11

Thanks for the info :slight_smile:
I'll have a look when I have some time :wink:
Thanks !

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.