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.

1 ACCEPTED SOLUTION

Accepted Solutions

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!!

View solution in original post

10 REPLIES 10
TDK
Guru

Your clock configuration is using HSI as a clock source. HSE might be enabled, but it's not being used as a clock source, at least in the picture you linked.

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

The picture is incorrect! The correct picture is attached.

TDK
Guru

What's probably happening is one of these functions is failing, Error_Handler is being called, but because Error_Handler has no content, it returns as if nothing went wrong.

Consider adding "while (1);" within Error_Handler, or step through your code to find out what's failing.

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

You are indeed correct!! I stepped through the code and found that the failure occurs in the HAL_RCC_ClockConfig routine when the following code is executed:

/* HSE is selected as System Clock Source */

  if(RCC_ClkInitStruct->SYSCLKSource == RCC_SYSCLKSOURCE_HSE)

  {

    /* Check the HSE ready flag */

    if(__HAL_RCC_GET_FLAG(RCC_FLAG_HSERDY) == RESET)

    {

     return HAL_ERROR;

    }

  }

The RCC_FLAG_HSERDY is clear indicating that the HSE oscillator is not running!

Any thoughts???

TDK
Guru

Is this a custom board? Is the HSE connected properly? Not real sure.

If you feel a post has answered your question, please click "Accept as Solution".
Piranha
Chief II

And what does this code do?

Yes, it is a custom board and I have verified that the external crystal is connected across PH0 and PH1 with 18 pF load capacitors on each lead.

The code shown above is generated by the STM32CubeMX IDE in accordance with the RCC clock configuration shown in the second picture above. It is supposed to set the appropriate bits in the RCC register control the clock sources and speeds.

Look carefully - there is a link in my post... Did you see it? The recent change of color for links in this forum is "great"!