AnsweredAssumed Answered

Bug in HAL_RCC_GetSysClockFreq() / STM32Cube_FW_L1_V1.3.0 ?

Question asked by Bellgardt.Andreas on Aug 18, 2015
Latest reply on Aug 19, 2015 by Amel N
I think I found a bug in STM32CubeL1 in function
HAL_RCC_GetSysClockFreq(). Clock settings are:

#define HSE_VALUE    ((uint32_t)12000000) // external oscillator at 12 MHz

/* Enable HSE Oscillator and Activate PLL with HSE as source */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL8;    //RCC_PLL_MUL12;
    RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
        Error_Handler();
    }


/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
     clocks dividers */
    RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
            | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) {
        Error_Handler();
    }

So I expected a SystemCoreClock = 32000000. But instead HAL_RCC_GetSysClockFreq() returned 24000000. It turns out that parentheses are wrong in the following section of HAL_RCC_GetSysClockFreq():

/* HSE used as PLL clock source */
pllvco = HSE_VALUE * (pllm / plld); // HSE_VALUE = 12000000, pllm = 8, plld = 3

After changing into

pllvco = (HSE_VALUE * pllm) / plld;

it returns 32000000 as expected.
This is because in the incorrect version above 8/3=2,6666 and the remainder is dropped, so 12MHz*2=24MHz!

Outcomes