cancel
Showing results for 
Search instead for 
Did you mean: 

HSE not enabled in SystemClock_Config code generated by STM32CubeMX

BitBanger46
Associate III

Even though I configure the RCC to use an external high speed crystal as a clock source (HSE), the HSI remains selected even after the SystemClock_Config routine is executed!! (I have checked the HSE pins and it appears that the clock driver (PH0/PH1) is not turned on.)

The SystemClock_Config code generated is:

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  /** Configure LSE Drive Capability 

  */

  HAL_PWR_EnableBkUpAccess();

  /** Configure the main internal regulator output voltage 

  */

  __HAL_RCC_PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

  /** Initializes the CPU, AHB and APB busses clocks 

  */

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

  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_HSE;

  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_0) != HAL_OK)

  {

   Error_Handler();

  }

  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2

               |RCC_PERIPHCLK_SDMMC1;

  PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;

  PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

  PeriphClkInitStruct.Sdmmc1ClockSelection = RCC_SDMMC1CLKSOURCE_SYSCLK;

  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)

  {

   Error_Handler();

  }

}

The oscillator type is set to HSE and the oscillator state is set to HSE_ON.

I added the following code at the end of the routine:

   uint32_t lClkFreq=HAL_RCC_GetHCLKFreq ();

The value of lClkFreq is 16000000 (16 MHz) which is the frequency of the internal HSI.

I should mention that I did not check the Master Clock Output 1 box in the RCC Configuration and Mode dialog box.

I have attached an image of the RCC Configuration and Mode as well as the Clock Configuration diagram.

10 REPLIES 10

OMG!! It took me 10 minutes to find the link embedded in "this code"!! (I cannot believe that it is almost impossible to find the link since the hue of the link is only slightly darker than the surrounding text!!) In any event, I single cycled through the SystemClock_Config routine in order to see the values in the RCC_OscInitStruct. I was surprised to see that the value of the HSEState variable (in the structure) was set to 0H upon entry to the HAL_RCC_OscConfig routine. I discovered that, when initializing the RCC_OscInitStruct, I incorrectly set the HSIState variable to RCC_HSE_ON instead of the HSEState variable to RCC_HSE_ON!!! I corrected the error and ... tada ... the HSE oscillator works!!

I have to thank you for your suggestion to look at the initialization code because I NEVER would have found the bug without it!! I doubly appreciate your help because this has frustrated me for more than a week and my confidence level in my ability to solve the problem was waning by the hour!!