PwmOut generate wrong signal

Hi,

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) {
  my_pwm.period_ms(10000);
  my_pwm.write(0.5f);

  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 ?

cc @bcostm

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

#include "mbed.h"

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

  toto.period_ms(1);
  toto.write(0.5);
  return 0;
}

Hi,

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?

Hi,

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 ?

Hi
OK. I think the source code is here you’re looking at is here
https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F3/pwmout_api.c
Or you may replace the lib “mbed” by “mbed-dev” if you’re working on the online compiler

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

thanks

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.
See:
https://github.com/LMESTM/mbed/commit/140f94a7538189a849d94ec6fd04d4e754c62cea
I’d be interested on your feedback

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.

Hi
I had to go through the reference manual of the chip (from www.st.com) 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:
https://github.com/LMESTM/mbed/commit/21e70e75b5e6b97ac455057fab13286be7e0432f
cheers

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