6 years, 3 months ago.

PWM >1MHz on STM32F401RE nucleo, possible with mbed?

Hi everybody, is there some workaround to get with mbed higher output frequencies >>500kHz? For my application I need a permanent running stable 2MHz output clock on a pin, no IRQs attached, no syncronisation etc... I was thinking of the PWM functionality, but that looks like limited to 500kHz with mbed. Any ideas? Possible to tweak a prescaler (I dont care about other clocks that might be affected)


2 Answers

6 years, 3 months ago.

May I suggest the special of the day...consider the use of an external I2C controlled PLL ? There is a great such device from Silabs which we have used on a mix of designs with success (Si5351A). You can pickup an assembled board for such testing from Adafruit:


$8USD for this board.


Using this approach, you will be much more flexible on the clock generation. It is a breeze to work with this PLL and there is a free GUI tool from Silabs (Clock Builder) to allow you to experiment on register values - everything we tested was 0% deviation on the target clock. Post back if that is not practical.

thanks, but my idea was not to fiddle around with extra chips (besides it looks like the STMF4 hardware is capable of doing so, just mbed does not support it(?)). The background reason was, that I needed a stable clock oszillation signal for the NUCLEO PowerSTEP01 evaluation board (2MHz), its internal one is not stable (+/-5%!). Just adding a jumper from the CPU NUCLEO to the STEPPER NUCLEO would be nice&easy.

posted by Hartmut Geisel 09 Jun 2017
6 years, 3 months ago.

This is not necessarily a firmware problem. PWM hardware can only switch the output for you on a clock edge. The F401 can run at 84MHz. You will have to check clock config and peripheral bus config to make sure the particular timer you are using can run this fast. In time domain a tick is then 11.9ns. If you want 8 bit resolution you need to allow for 255 ticks. 255 x 11.9ns = 3.034us period. This is about 330kHz max for 8 bit resolution. The shortcut is to just divide 84MHz by the resolution. So, depending on the resolution you need, the max frequency is about:

10bit: 1024 -> 82kHz 9bit: 512 -> 164kHz 8bit: 255 -> 330kHz 7bit: 128 -> 656kHz

This represents a limit on what the hardware can do. I haven't looked at the mbed pwm libraries in a while. The FastPWM library was created to get around any code limits in standard mbed library. If you can work within the hardware limits, that library might help.

thanks, I am aware that the higher my PWM frequency, the lower my resolution will end up. Which brings me to two more questions: 1.) I am a total newbie to mbed and ARM, how do I configer the clocks with mbed? 2.) I took a look at the FastPWM, yes, those address this issue. But FastPWM&Co. are designed for LPC... chips, will it run with STM32F4 as well automatically? Thanks, again.

posted by Hartmut Geisel 09 Jun 2017

It's been a while, but I have used FastPWM with mbed-dev and the F401 board.

posted by Graham S. 09 Jun 2017