cancel
Showing results for 
Search instead for 
Did you mean: 

Wrong SystemCoreClock value after SystemCoreClockUpdate (HSE with HSEPRE=2)

BDC_dev123
Associate

Hello,

I use a STM32WLE5JCI6 MCU. Sometimes I need a good clock precision so I switch to HSE as SYSCLK, with a 32MHz quartz. I use 4MHz as HCLK, and I chose to set HSEPRE=2 and HPRE=4. When I call SystemCoreClockUpdate() function the SystemCoreClock value is 8MHz, because HSEPRE value is not considered. Is it a bug or this is the expected value, for any reason I missed? The function HAL_RCC_GetSysClockFreq() uses LL_RCC_HSE_IsEnabledDiv2 to return the expected value, so the SystemCoreClock value is 4MHz if I configure the HSE as sysclock at startup.

Thanks,

Sylvain

 

My code:

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEDiv = RCC_HSE_DIV2;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK
                              |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
                              |RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV8;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV8;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }

  /** Enable the HSE Prescaler
  */
  __HAL_RCC_HSE_DIV2_ENABLE();
  HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_1);
}

and:

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();

  printf("Test HSE. %s - %s\n\r", __DATE__, __TIME__);
  printf("SystemCoreClock = %lu\r\n", SystemCoreClock);
  SystemCoreClockUpdate();
  printf("SystemCoreClock = %lu\r\n", SystemCoreClock);

 

Result:

Test HSE. Oct 22 2025 - 13:59:06
SystemCoreClock = 2000000
SystemCoreClock = 4000000

 

 

2 REPLIES 2
TDK
Super User

Sure looks like a bug. Most chip don't have an HSEPRE field so it probably got missed.

 

If you feel a post has answered your question, please click "Accept as Solution".
BDC_dev123
Associate

Thanks for your answer. Is it safe to do this as a workaround?

 

/* Update the SystemCoreClock global variable */

SystemCoreClock = HAL_RCC_GetHCLKFreq();