Round-off error in PLL frequency calculation

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Round-off error in PLL frequency calculation

703 Views
acharya_satishb
Contributor I

Hello,

In MK60DN256VLL10, I'm using external 50MHz clock in PEE mode with divide by 13 and multiply by 26 to obtain 100MHz System clock and Core clock. I couldn't find another way to get 100MHz.

Issue is Kinetis SDK code generates frequency as 99,999,978 instead of 100,000,000 which causes problems in FTM functionality.

I think the real issue is in fsl_clock.c,

.......................
uint32_t CLOCK_GetPll0Freq(void)
{
uint32_t mcgpll0clk;

/* If PLL0 is not enabled, return 0. */
if (!(MCG->S & MCG_S_LOCK0_MASK))
{
return 0U;
}

mcgpll0clk = CLOCK_GetPll0RefFreq();

/*
* Please call CLOCK_SetXtal0Freq base on board setting before using OSC0 clock.
* Please call CLOCK_SetXtal1Freq base on board setting before using OSC1 clock.
*/
assert(mcgpll0clk);

mcgpll0clk /= (FSL_FEATURE_MCG_PLL_PRDIV_BASE + MCG_C5_PRDIV0_VAL);
mcgpll0clk *= (FSL_FEATURE_MCG_PLL_VDIV_BASE + MCG_C6_VDIV0_VAL);

return mcgpll0clk;
}
...................

CLOCK_GetPll0RefFreq() sets mcgpll0clk to 50,000,000 which is correct. After we divide by 13 and multiply by 26, expectation is mcgpll0clk will be set to 100,000,000. Instead, it gets set to 99,999,978. This rounding off error creates problem elsewhere. For instance, in fsl_ftm.c,

.....
switch (mode)
{
case kFTM_EdgeAlignedPwm:
case kFTM_CombinedPwm:
base->SC &= ~FTM_SC_CPWMS_MASK;
mod = (ftmClock / pwmFreq_Hz) - 1;
break;
.....

Here, I set pwmFreq_Hz to 25MHz and expectation is ftmClock = 50MHz so that mod is 1. Since ftmClock gets set to half of 99,999,978, mod gets set to 0 instead of 1 and it breaks FTM functionality.

For now, I have hacked the code and restructured it so that 26 gets divided by 13 first and the result is then multiplied to mcgpll0clk to get clean 100MHz. I'm not sure how the PLL is designed in hardware and if this change accurately models the hardware.

Please let me know if there is an alternate way to avoid rounding off error or fix the code in the next release.

Regards,

Satish

0 Kudos
1 Reply

628 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Satish,

Thank you for the report, I'll inform the SDK team about this.
Sorry for the inconvenience we bring you!
Please try to modify the PRDIV=16 and VDIV=32 manually. Then the SDK function will get clean 100MHz.

16 32.png

100000000.png

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos