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

Issue: NUCLEO_F446RE: PWM_TIMER->PSC not set correctly

When calling period_ticks directly from main function, PWM_TIMER->PSC is not set.

PWM_TIMER->PSC is set in function setPrescaler.

The function setPrescaler is called in period_ms and period_us via calcPrescaler. Howeever when calling period_ticks directly setPrescaler is not called and PWM_TIMER->PSC has incorrect value.

Workaround is simply set PWM_TIMER->PSC = 0 in function period_ticks This will work for ticks and us, but then breaks period_ms

void FastPWM::period_ticks( uint32_t ticks ) {

    /* TEMPORARY FIX for "ticks" and "us" but breaks on "ms" *******************/
    PWM_TIMER->PSC = 0;
    /* END TEMPORARY FIX *******************/

    PWM_TIMER->ARR = ticks - 1;

Have only tested this on NUCLEO_F446RE, but suspect that this error also will happen on other NUCLEO boards. Maybe one solution is a public version of period_ticks where calcPrescaler or setPrescaler is called, and a private version of period_ticks only for use by period_ms and period_us?


01 Feb 2016

I see my documentation can use some refinement, since it says ticks sets the period in clock ticks. (Which was the case in the very first version of FastPWM, since it only included the LPC1768). It is supposed to keep the current prescaler, if you want to set it to set the prescaler to a certain value you can use the setPrescaler function for that, but period_ticks is mainly intended to be very fast with low overhead, and do nothing besides setting the ticks.

Am I correct that addresses your problem, or am I missing another issue? Otherwise I just clarify the documentation better.

01 Feb 2016

Yes, that clarify: "FastPWM mypwm(mypin,1)" or "mypwm.prescaler(1)" set the values correct when using function period_tick;

"Otherwise I just clarify the documentation better": A remark that prescale has to be set (= 1) before calling function period_tick when programmer want to run pwm in clock ticks.

Now it works so fast that my 40Mhz Oscilloscope are struggling to keep up.......

Thumps up :)