Setting kCLOCK_PllVideo in SDK_25_12_00_EVK-MIMXRT1064 fsl_clock.c

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

Setting kCLOCK_PllVideo in SDK_25_12_00_EVK-MIMXRT1064 fsl_clock.c

Jump to solution
527 Views
choix361
Contributor II

Here is the code from fsl_clock.c line 1072~1080. SDK version is 25_12_00.

case kCLOCK_PllVideo:
/* PLL output frequency = Fref * (DIV_SELECT + NUM/DENOM). */
divSelect =
(CCM_ANALOG->PLL_VIDEO & CCM_ANALOG_PLL_VIDEO_DIV_SELECT_MASK) >> CCM_ANALOG_PLL_VIDEO_DIV_SELECT_SHIFT;

freqTmp = ((clock_64b_t)freq * ((clock_64b_t)(CCM_ANALOG->PLL_VIDEO_NUM)));
freqTmp /= ((clock_64b_t)(CCM_ANALOG->PLL_VIDEO_DENOM));
freq = freq * divSelect + (uint32_t)freqTmp;

 

The last line should be freq = freq * (divSelect + (uint32_t)freqTmp); ?

Is this potential bug? Also, when CCM_ANALOG->PLL_VIDEO_DENOM is 0, this does not handle DIV 0 fault.

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
401 Views
Gavin_Jia
NXP TechSupport
NXP TechSupport

Hi @choix361 ,

Thanks for your interest in NXP MIMXRT series!

The SDK line is correct. freqTmp is already calculated as Fref * (NUM/DENOM), so the final output should be Fref*DIV_SELECT + Fref*(NUM/DENOM), i.e. freq = freq * divSelect + (uint32_t)freqTmp;. Using freq = freq * (divSelect + (uint32_t)freqTmp) would multiply freq twice and give a wrong result. 


You are right about the divide-by-zero risk: if PLL_VIDEO_DENOM == 0, the current code would divide by zero and there is no guard in this function. In normal use, DENOM should be programmed to a non-zero value by the PLL init/config, but for extra robustness you can add a simple check (or assert) before the division.

Best regards,
Gavin

View solution in original post

0 Kudos
Reply
1 Reply
402 Views
Gavin_Jia
NXP TechSupport
NXP TechSupport

Hi @choix361 ,

Thanks for your interest in NXP MIMXRT series!

The SDK line is correct. freqTmp is already calculated as Fref * (NUM/DENOM), so the final output should be Fref*DIV_SELECT + Fref*(NUM/DENOM), i.e. freq = freq * divSelect + (uint32_t)freqTmp;. Using freq = freq * (divSelect + (uint32_t)freqTmp) would multiply freq twice and give a wrong result. 


You are right about the divide-by-zero risk: if PLL_VIDEO_DENOM == 0, the current code would divide by zero and there is no guard in this function. In normal use, DENOM should be programmed to a non-zero value by the PLL init/config, but for extra robustness you can add a simple check (or assert) before the division.

Best regards,
Gavin

0 Kudos
Reply
%3CLINGO-SUB%20id%3D%22lingo-sub-2327381%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ESetting%20kCLOCK_PllVideo%20in%20SDK_25_12_00_EVK-MIMXRT1064%20fsl_clock.c%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2327381%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHere%20is%20the%20code%20from%20fsl_clock.c%20line%201072~1080.%20SDK%20version%20is%2025_12_00.%3C%2FP%3E%3CP%3Ecase%20kCLOCK_PllVideo%3A%3CBR%20%2F%3E%2F*%20PLL%20output%20frequency%20%3D%20Fref%20*%20(DIV_SELECT%20%2B%20NUM%2FDENOM).%20*%2F%3CBR%20%2F%3EdivSelect%20%3D%3CBR%20%2F%3E(CCM_ANALOG-%26gt%3BPLL_VIDEO%20%26amp%3B%20CCM_ANALOG_PLL_VIDEO_DIV_SELECT_MASK)%20%26gt%3B%26gt%3B%20CCM_ANALOG_PLL_VIDEO_DIV_SELECT_SHIFT%3B%3C%2FP%3E%3CP%3EfreqTmp%20%3D%20((clock_64b_t)freq%20*%20((clock_64b_t)(CCM_ANALOG-%26gt%3BPLL_VIDEO_NUM)))%3B%3CBR%20%2F%3EfreqTmp%20%2F%3D%20((clock_64b_t)(CCM_ANALOG-%26gt%3BPLL_VIDEO_DENOM))%3B%3CBR%20%2F%3Efreq%20%3D%20freq%20*%20divSelect%20%2B%20(uint32_t)freqTmp%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EThe%20last%20line%20should%20be%20freq%20%3D%20freq%20*%20(divSelect%20%2B%20(uint32_t)freqTmp)%3B%20%3F%3C%2FP%3E%3CP%3EIs%20this%20potential%20bug%3F%20Also%2C%20when%20CCM_ANALOG-%26gt%3BPLL_VIDEO_DENOM%20is%200%2C%20this%20does%20not%20handle%20DIV%200%20fault.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2327381%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3Ei.MXRT%20106x%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2328666%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%20translate%3D%22no%22%3ERe%3A%20Setting%20kCLOCK_PllVideo%20in%20SDK_25_12_00_EVK-MIMXRT1064%20fsl_clock.c%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2328666%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F192846%22%20target%3D%22_blank%22%3E%40choix361%3C%2FA%3E%26nbsp%3B%2C%3C%2FP%3E%0A%3CP%3EThanks%20for%20your%20interest%20in%20NXP%20MIMXRT%20series!%3C%2FP%3E%0A%3CP%3EThe%20SDK%20line%20is%20correct.%20freqTmp%20is%20already%20calculated%20as%20Fref%20*%20(NUM%2FDENOM)%2C%20so%20the%20final%20output%20should%20be%20Fref*DIV_SELECT%20%2B%20Fref*(NUM%2FDENOM)%2C%20i.e.%20freq%20%3D%20freq%20*%20divSelect%20%2B%20(uint32_t)freqTmp%3B.%20Using%20freq%20%3D%20freq%20*%20(divSelect%20%2B%20(uint32_t)freqTmp)%20would%20multiply%20freq%20twice%20and%20give%20a%20wrong%20result.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3EYou%20are%20right%20about%20the%20divide-by-zero%20risk%3A%20if%20PLL_VIDEO_DENOM%20%3D%3D%200%2C%20the%20current%20code%20would%20divide%20by%20zero%20and%20there%20is%20no%20guard%20in%20this%20function.%20In%20normal%20use%2C%20DENOM%20should%20be%20programmed%20to%20a%20non-zero%20value%20by%20the%20PLL%20init%2Fconfig%2C%20but%20for%20extra%20robustness%20you%20can%20add%20a%20simple%20check%20(or%20assert)%20before%20the%20division.%3C%2FP%3E%0A%3CP%3EBest%20regards%2C%3CBR%20%2F%3EGavin%3C%2FP%3E%3C%2FLINGO-BODY%3E