Skip to main content
Fali.1
Associate
November 20, 2021
Solved

HAL_RCC_GetSysClockFrequency() gives wrong value for input clock

I am using stm32f767zi nucleo board and they HSE clock input is coming from st link debugger which according to data sheet of the board generates an 8 MHz MCO signal which is fed into the OSC_in pin. I have configured HSE bypass as the clk source for my mcu and configured the SYSCLK to be 8 MHZ(same as HSE). there is no PLL. However when I try to call the above method to see the current sysclk freq, the degugger shows it as 25 MHz which is clearly wrong.

0693W00000GYISKQA5.pngmy code for configuring clock is shown below.

void SystemClockConfig()
{
 
 
	RCC_OscInitTypeDef osc_init;
	osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSE;
	// after reset HSE is off, so you have to give it a new state
	osc_init.HSEState = RCC_HSE_BYPASS;
	osc_init.PLL.PLLState = RCC_PLL_NONE;
	if (HAL_RCC_OscConfig(&osc_init) != HAL_OK)
	{
		// there is somethign wrong in initialization
		Error_handler();
	}
//
//
	RCC_ClkInitTypeDef clk_init;
 
	clk_init.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 ;
	// next we will enable HSE as the clock source.
	clk_init.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
 
 
 
	// next we will set AHB prescaler to get HWCLK of 4 MHz
	clk_init.AHBCLKDivider = RCC_SYSCLK_DIV2;
	clk_init.APB1CLKDivider = RCC_HCLK_DIV2; // to set APB1 clock as 2 MHz
	clk_init.APB2CLKDivider = RCC_HCLK_DIV2;
 
	if (HAL_RCC_ClockConfig(&clk_init, FLASH_ACR_LATENCY_0WS) != HAL_OK)
	{
		Error_handler();
	}
 
	__HAL_RCC_HSI_DISABLE();
	__HAL_RCC_PLL_DISABLE();
	HAL_SYSTICK_Config(4000);
	HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
 
}

However when i monitor the value of uint32_t SYSCLK = HAL_RCC_GetSysClockFreq(); this shows 25 MHz. the image is shown below.

0693W00000GYIRWQA5.pngWhat could be the issue here

This topic has been closed for replies.
Fali.1
Fali.1Best answer
Associate
November 20, 2021

I see, this was set to 25MHz by default, changed it to 8 MHz in hal_conf.h file and now its working. Thanks

2 replies

waclawek.jan
Super User
November 20, 2021

HAL_RCC_GetSysClockFreq() is open source, you can find out how does it infer the system frequency.

As it can't guess the HSE frequency, you (and by "you" I mean also CubeMX/CubeIDE/whatever-clicky-you've-used which generates code) have set it somewhere incorrectly (search for HSE_VALUE).

JW

Fali.1
Fali.1AuthorBest answer
Associate
November 20, 2021

I see, this was set to 25MHz by default, changed it to 8 MHz in hal_conf.h file and now its working. Thanks