Library that allows for higher resolution and speed than standard mbed PWM library using same syntax (drop-in replacement).

Dependents:   PwmOscillator FastStepDriver TLC5940 CameraTest ... more

FastPWM is a library that unlocks alot more of the potential of the mbed's PWM units than the normal PWM library. It is currently available for the LPC1768, LPC11u24, KLxxZ, K20D50M and most STM32 targets (those lacking are fairly easy to add). (Since I generally forget to update this list, if you want to know if your target is supported see if it compiles).

The two main points it allows for compared to the regular PwmOut library is clock cycle precision PWM and (automated) changing prescalers. It isn't perfect yet, but for now it will have to do ;). For those familiar with version 1, version 2 is almost completely rewritten to add more functions.


FastPWM is largely a drop-in replacement for the normal PwmOut library. All the same functions are available, with some extras.



All prescaler options are disabled for 32-bit PWM units currently, the prescaler is fixed at 1


With this function you can set the value of the prescaler. Aditionally the second argument of the constructor is used for the same to directly set it from the constructor. It returns the actual prescaler which is set. If the requested one isn't available it is always rounded up (unless it is larger than the maximum prescaler).

There are three options for this function. Any value larger than zero will simply be set. (Yes it is signed, so yes you cannot use the full 32-bit prescaler if your device supports it, I cannot imagine why you possibly would want that). If the value is zero dynamic prescaling is disabled and the current prescaler is returned. If the value is -1 dynamic prescaling is enabled and the current prescaler is returned.

So what is dynamic prescaling? This is the default option for FastPWM, don't use any prescaler option and it is enabled. To start with the negative, it adds quite some processing cycles, so changing the period takes longer. Luckily generally the PWM period is constant. The good part is that it automatically adapts the prescaler unit to give as much accuracy as possible: It gives highest accuracy for the duty-cycle, and also allows you to generate a wide range of periods. For example you can now create a LED blinking at 1Hz with FastPWM on the LPC11u24/Nucleo 16-bit PWM units. (On the KL25Z this isn't possible due to limitted value of the prescaler).

As the nice warning message above says, this is currently only implemented for 16-bit PWM units, simply because normally you won't need it for 32-bit PWM units. For those it is automatically disabled, and you cannot enable it. However for example the majority of the PWM units of the LPC11u24 can't be used to make servo signals with PwmOut, with FastPWM they can.

TL;DR, by default it uses dynamic prescaling. Unless period is changed very often just keep it on default and enjoy your larger range of possible periods and higher accuracy.



These two functions allow you to directly write the pwm period and pulsewidth in clock ticks. This is useful if you need to have very little overhead. It is dependent on which device you use, since they have different clock rates. You can get the current clock speed of your device with SystemCoreClock.


PwmOut uses floats for setting the time in seconds, and ints for milliseconds and microseconds. All three of those don't give enough accuracy to fully use the PWM units. Which is why FastPWM uses besides int for milliseconds and microseconds, it uses doubles for seconds and also for microseconds. Generally it is adviced to use these doubles, sometimes you might need to explicitly cast your variables to doubles.

Currently setting pulsewidth in microseconds with an int is a risk with some prescaler values (not on the 32-bit timers). See known-issues.

Adding other microcontrollers

Look at the other device files on how to add other microcontrollers. Functions that need to be implemented:

  • initFastPWM(): Any setups required can be done here. Must set the number of bits of the PWM unit.
  • pulsewidth_ticks( uint32_t ticks ): Set the pulsewidth in ticks
  • period_ticks( uint32_t ticks ): Set the period in ticks
  • getPeriod(): Return the period in ticks
  • setPrescaler(uint32_t reqScale): Set the prescaler. If reqScale is zero, return the current prescaler value only. Otherwise set the requested prescaler, if needed round up to first available prescaler, return the actually set prescaler. If the PWM unit is 32-bit (or for another reason), you can set dynamicPrescaler as false, disabling the dynamic prescaler.

Known Issues

  • Changing the prescaler manually does not adapt periods/pulsewidth
    • Manually re-set the period of each FastPWM object on that PWM unit, this should also set the duty cycle.
  • Changing the period of one FastPWM object does not keep the duty cycle of other PWM objects on that PWM unit constant, but the pulsewidth.
    • Manually re-set the duty cycle of other PWM objects.
  • PwmOut objects run at wrong speed when you use FastPWM
    • Don't use PwmOut objects.
  • On certain prescaler values setting period/pulsewidth in especially microsecond integers, also to lesser extend also millisecond integers, can result in wrong values.
    • The problem is that the number of clock ticks per microsecond/millisecond as integers are pre-calculated for improved speed. However if it isn't an integer number that gives an error.
    • Solution is to preferably use doubles (or ticks). On the 32-bit pwm units this is not an issue, so for them it doesn't matter.
    • I am planning to have a further look into it, but I expect it to stay an issue.

Here the TL;DR is: Preferably set the period/prescaler once at the beginning before setting the duty-cycle/pulsewidth. If that isn't possible, take into account duty cyles need to be set again. And preferably use doubles.


Some of the ideas are 'loaned' from Jochen Krapf's fork of the original FastPWM:

Sun Sep 03 16:26:07 2017 +0000
KSDK (K64F) update
; Now it should properly synchronize PWM updates again
; Serious @mbed, clearing the timer is not a proper synchronisation mechanic for PWM!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 14:b30038fbba51 1 #if defined(TARGET_KPSDK_MCUS)
Sissors 14:b30038fbba51 2
Sissors 14:b30038fbba51 3 #include "FastPWM.h"
Sissors 31:10e2e171f430 4 #include "fsl_ftm.h"
Sissors 14:b30038fbba51 5
Sissors 14:b30038fbba51 6
Sissors 14:b30038fbba51 7 #define PWM_CNV (*(((fastpwm_struct*)fast_obj)->CnV))
Sissors 14:b30038fbba51 8 #define PWM_MOD (*(((fastpwm_struct*)fast_obj)->MOD))
Sissors 14:b30038fbba51 9 #define PWM_SC (*(((fastpwm_struct*)fast_obj)->SC))
Sissors 33:2ca2e47f9650 10 #define PWM_SYNC (*(((fastpwm_struct*)fast_obj)->SYNC))
Sissors 14:b30038fbba51 11
Sissors 14:b30038fbba51 12 typedef struct {
Sissors 14:b30038fbba51 13 __IO uint32_t *CnV;
Sissors 14:b30038fbba51 14 __IO uint32_t *MOD;
Sissors 14:b30038fbba51 15 __IO uint32_t *SC;
Sissors 33:2ca2e47f9650 16 __IO uint32_t *SYNC;
Sissors 14:b30038fbba51 17 } fastpwm_struct;
Sissors 14:b30038fbba51 18
Sissors 14:b30038fbba51 19 static uint32_t pwm_prescaler;
Sissors 31:10e2e171f430 20 static FTM_Type *const ftm_addrs[] = FTM_BASE_PTRS;
Sissors 14:b30038fbba51 21
Sissors 14:b30038fbba51 22 void FastPWM::initFastPWM( void ) {
Sissors 14:b30038fbba51 23 fast_obj = new fastpwm_struct;
Sissors 14:b30038fbba51 24 bits = 16;
Sissors 31:10e2e171f430 25
Sissors 31:10e2e171f430 26 pwm_prescaler = SystemCoreClock / CLOCK_GetFreq(kCLOCK_BusClk);;
Sissors 14:b30038fbba51 27
Sissors 31:10e2e171f430 28 unsigned int ch_n = (_pwm.pwm_name & 0xF);
Sissors 31:10e2e171f430 29 FTM_Type *ftm = ftm_addrs[_pwm.pwm_name >> TPM_SHIFT];
Sissors 14:b30038fbba51 30
Sissors 14:b30038fbba51 31 ((fastpwm_struct*)fast_obj)->CnV = &ftm->CONTROLS[ch_n].CnV;
Sissors 14:b30038fbba51 32 ((fastpwm_struct*)fast_obj)->MOD = &ftm->MOD;
Sissors 14:b30038fbba51 33 ((fastpwm_struct*)fast_obj)->SC = &ftm->SC;
Sissors 33:2ca2e47f9650 34 ((fastpwm_struct*)fast_obj)->SYNC = &ftm->SYNC;
Sissors 33:2ca2e47f9650 35
Sissors 33:2ca2e47f9650 36 //Do not clear counter when writing new value, set end of period as loading value
Sissors 33:2ca2e47f9650 37 ftm->SYNCONF &= ~FTM_SYNCONF_SWRSTCNT_MASK;
Sissors 33:2ca2e47f9650 38 ftm->SYNC |= FTM_SYNC_CNTMAX_MASK;
Sissors 14:b30038fbba51 39 }
Sissors 14:b30038fbba51 40
Sissors 14:b30038fbba51 41 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
Sissors 14:b30038fbba51 42 PWM_CNV = ticks;
Sissors 33:2ca2e47f9650 43 PWM_SYNC |= FTM_SYNC_SWSYNC_MASK;
Sissors 14:b30038fbba51 44 }
Sissors 14:b30038fbba51 45
Sissors 14:b30038fbba51 46 void FastPWM::period_ticks( uint32_t ticks ) {
Sissors 14:b30038fbba51 47 PWM_MOD = ticks - 1;
Sissors 33:2ca2e47f9650 48 PWM_SYNC |= FTM_SYNC_SWSYNC_MASK;
Sissors 14:b30038fbba51 49 }
Sissors 14:b30038fbba51 50
Sissors 14:b30038fbba51 51 uint32_t FastPWM::getPeriod( void ) {
Sissors 14:b30038fbba51 52 return PWM_MOD + 1;
Sissors 14:b30038fbba51 53 }
Sissors 14:b30038fbba51 54
Sissors 14:b30038fbba51 55 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
Sissors 14:b30038fbba51 56
Sissors 14:b30038fbba51 57 uint32_t prescalers[] = {1, 2, 4, 8, 16, 32, 64, 128};
Sissors 14:b30038fbba51 58
Sissors 14:b30038fbba51 59 for (int i = 0; i<8; i++)
Sissors 14:b30038fbba51 60 prescalers[i] = prescalers[i] * pwm_prescaler;
Sissors 14:b30038fbba51 61
Sissors 14:b30038fbba51 62 //If prescaler is 0, return current one
Sissors 14:b30038fbba51 63 if (reqScale == 0)
Sissors 14:b30038fbba51 64 return (prescalers[(PWM_SC) & 0x07]);
Sissors 14:b30038fbba51 65
Sissors 14:b30038fbba51 66 uint32_t retval = 0;
Sissors 14:b30038fbba51 67 char bin;
Sissors 14:b30038fbba51 68
Sissors 14:b30038fbba51 69 for (bin = 0; bin<8; bin++) {
Sissors 14:b30038fbba51 70 retval = prescalers[bin];
Sissors 14:b30038fbba51 71 if (retval >= reqScale)
Sissors 14:b30038fbba51 72 break;
Sissors 14:b30038fbba51 73 }
Sissors 14:b30038fbba51 74 if (bin == 8)
Sissors 14:b30038fbba51 75 bin = 7;
Sissors 14:b30038fbba51 76
Sissors 14:b30038fbba51 77 //Clear lower 5 bits, write new value:
Sissors 14:b30038fbba51 78 char clockbits = PWM_SC & (3<<3);
Sissors 14:b30038fbba51 79
Sissors 14:b30038fbba51 80 //For some reason clearing them takes some effort
Sissors 14:b30038fbba51 81 while ((PWM_SC & 0x1F) != 0)
Sissors 14:b30038fbba51 82 PWM_SC &= ~0x1F;
Sissors 14:b30038fbba51 83
Sissors 14:b30038fbba51 84
Sissors 14:b30038fbba51 85 PWM_SC = bin + clockbits;
Sissors 14:b30038fbba51 86 return retval;
Sissors 14:b30038fbba51 87 }
Sissors 14:b30038fbba51 88 #endif