Skip to main content
deep_rune
Associate III
Feb 27, 2022 2:44 PM UTC
Question

SystemClock_Config() runs over and over

Im using a STM32L412, I've made the config in STMcube but when I run the program (no main code, just the setup routine) it never reaches the main loop. By placing breakpoints I have deduced that  SystemClock_Config(); runs over and over again. It seems that this gets to the line  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) after which it dives into an endless parade of different functions, im not sure it's worth the effort to try and follow it, but breakpoints indicate that SystemClock_Config() never gets any further and starts from the beginning some time after that.

What might be the problem? I included the SystemClock_Config() below

Apologies if this is a vague question, I'm doing my best to understand but I feel overwhelmed with the endless detail of stm32

any help much appreciated

void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
 /** Configure the main internal regulator output voltage 
 */
 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
 {
 Error_Handler();
 }
 /** Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_MSI;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.MSIState = RCC_MSI_ON;
 RCC_OscInitStruct.MSICalibrationValue = 0;
 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLM = 1;
 RCC_OscInitStruct.PLL.PLLN = 10;
 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 /** Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |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_3) != HAL_OK)
 {
 Error_Handler();
 }
 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USB;
 PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
 PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_MSI;
 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
 {
 Error_Handler();
 }
}

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
Feb 27, 2022 2:56 PM UTC

What board?

Does the HSE actually start properly?

If the source is an external clock, ie TCXO or clock from ST-LINK, perhaps you should be the BYPASS mode?

Should be possible to output the internal clock via MCO/PA8, you should route HSE there, and simply start the clock via the RCC register. If the clock doesn't start, check compentry.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
deep_rune
deep_runeAuthor
Associate III
Feb 27, 2022 3:17 PM UTC

Thanks for your reply. I'm using a board I designed and I have a 12MHz external crystal

So just to clarify - you're saying I can check if the clock is starting by outputting the internal clock (which is based on the external crystal) onto MCO/PA8 (presumably in clock config on STMcube?) and instead of running SystemClock_Config() i just set the RCC register.

what do you mean by 'check compentry'?

edit: I think routing the HSE to PA8 is fine, but the setup code for this then resides at the end of SystemClock_Config() so I'm not sure how to get this to work

Tesla DeLorean
Guru
Feb 27, 2022 5:36 PM UTC

Sorry componentry, parts chosen, parts placed.

Clock enable, without involving a thousand lines of code

RCC->CR |= RCC_CR_HSEON;

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..