cancel
Showing results for 
Search instead for 
Did you mean: 

bug: HAL_RCC_GetSysClockFreq() in STM32Cube_FW_F4_V1.5.0

zh_jianjun
Associate II
Posted on April 23, 2015 at 03:18

Test data: HSE_VALUE = 24000000, pllm=196, plln=14, pllp=2,

result: sysclockfreq=167,999,930,but the correct one is 168,000,000

reason: in expression

pllvco = ((

HSE_VALUE / pllm

) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN)));

(HSE_VALUE / pllm) =1,714,285.71 round to 1,714,285

fix:

pllvco = ((uint64_t)HSE_VALUE * plln / pllm)

full code:

uint32_t HAL_RCC_GetSysClockFreq(void)

{

  uint32_t pllm = 0, plln=0, pllvco = 0, pllp = 0;

  uint32_t sysclockfreq = 0;

  /* Get SYSCLK source -------------------------------------------------------*/

  switch (RCC->CFGR & RCC_CFGR_SWS)

  {

    case RCC_CFGR_SWS_HSI:  /* HSI used as system clock source */

    {

      sysclockfreq = HSI_VALUE;

       break;

    }

    case RCC_CFGR_SWS_HSE:  /* HSE used as system clock  source */

    {

      sysclockfreq = HSE_VALUE;

      break;

    }

    case RCC_CFGR_SWS_PLL:  /* PLL used as system clock  source */

    {

      /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN

      SYSCLK = PLL_VCO / PLLP */

      pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;

      plln = (RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> POSITION_VAL(RCC_PLLCFGR_PLLN);

      pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >> POSITION_VAL(RCC_PLLCFGR_PLLP)) + 1 ) *2;

      if(__HAL_RCC_GET_PLL_OSCSOURCE() != RCC_PLLSOURCE_HSI)

      {

        /* HSE used as PLL clock source */

        pllvco = ((uint64_t)HSE_VALUE * plln / pllm);

      }

      else

      {

        /* HSI used as PLL clock source */

        pllvco = ((uint64_t)HSI_VALUE * plln / pllm);    

      }

      

      

      sysclockfreq = pllvco/pllp;

      break;

    }

    default:

    {

      sysclockfreq = HSI_VALUE;

      break;

    }

  }

  return sysclockfreq;

}

1 REPLY 1
zh_jianjun
Associate II
Posted on April 23, 2015 at 06:50

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6qN&d=%2Fa%2F0X0000000bwc%2F.pHiv9vD6rjXt0zWFj6j4UraoJOTMk0DNRA_5A9vi6E&asPdf=false