Skip to main content
ChristophR
Associate III
November 12, 2019
Question

SystemClock_Config() generated by STM32CubeMX fails after jumping from the built-in bootloader (system memory)

  • November 12, 2019
  • 2 replies
  • 1694 views

Hi,

We are using a STM32G071GB uC, and have created an application using STM32CubeMX 5.4.0 (project file is attached).

When using the go command to jump from the built-in bootloader to the user application, HAL_RCC_OscConfig() called by SystemClock_Config() fails:

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 
 */
 HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
 /** Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
 RCC_OscInitStruct.PLL.PLLN = 8;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler(); // fails here
 }
HAL_StatusTypeDef HAL_RCC_OscConfig(RCC_OscInitTypeDef *RCC_OscInitStruct)
{
...
 else
 {
 /* Check if there is a request to disable the PLL used as System clock source */
 if((RCC_OscInitStruct->PLL.PLLState) == RCC_PLL_OFF)
 {
 return HAL_ERROR;
 }
 else
 { 
 /* Do not return HAL_ERROR if request repeats the current configuration */
 temp_pllckcfg = RCC->PLLCFGR;
 if((READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLSRC) != RCC_OscInitStruct->PLL.PLLSource) ||
 (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLM) != RCC_OscInitStruct->PLL.PLLM) ||
 (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLN) != (RCC_OscInitStruct->PLL.PLLN << RCC_PLLCFGR_PLLN_Pos)) ||
 (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLP) != RCC_OscInitStruct->PLL.PLLP) ||
#if defined (RCC_PLLQ_SUPPORT)
 (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLQ) != RCC_OscInitStruct->PLL.PLLQ) ||
#endif
 (READ_BIT(temp_pllckcfg, RCC_PLLCFGR_PLLR) != RCC_OscInitStruct->PLL.PLLR))
 {
 return HAL_ERROR; // fails here
 }

This happens only when jumping from the bootloader, not when directly booting the user application.

Did I make a mistake, or should this be fixed in the HAL library?

Thanks!

BR,

Christoph

This topic has been closed for replies.

2 replies

ST Employee
November 22, 2019

Dear Christoph,

It is very possible that the System memory, prior executing the jump to your application using go Command don't reset the RCC setting. Maybe check if DeInit those configuration help.

Monitoring the memory map in the SYSCFG register and set it to appropriate value can also be a good point to check for your investigation.

Regards,

Antoine

RaJa
Associate II
April 7, 2021

HAL_RCC_DeInit() before SystemClock_Config() helps properly config in CubeMX 6.2.1. In previous versions of CubeMX this wasn't needed.

Tesla DeLorean
Guru
April 7, 2021

Probably assumptions about currently running clocks, etc.

Could also a) transition to HSI source or b) make a determination about the current run-time conditions, and then not teardown HSE, PLL, etc when not necessary.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..