cancel
Showing results for 
Search instead for 
Did you mean: 

Why does CubeMX "turn on" HSI if it's not referenced?

Grant Bt
Senior

This is a bit nit-picky, but it's confusing. For a USB experiment on an STM32F103, I'm seeing an odd clock setting that isn't used and wondering why it is this way. The USB application uses an 8MHz HSE crystal as per usual.

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
   :
 etc

Why is the below line included at all? In stepping through the code, I don't see it being relevant:

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

Following the flow, since the clock type is HSE, then the above element is never used.

if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSE) == RCC_OSCILLATORTYPE_HSE)
   {
   // is executed.
   }

if (((RCC_OscInitStruct->OscillatorType) & RCC_OSCILLATORTYPE_HSI) == RCC_OSCILLATORTYPE_HSI)
   {
   // not executed and this is the only place RCC_HSI_ON is evaluated
   }

(I think this is a curious implementation of osc types via bit positions, but that's beside the point)

3 REPLIES 3
TDK
Guru

OscillatorType defines which clocks you are changing. You don't have RCC_OSCILLATORTYPE_HSI defined, so it doesn't change the state of the HSI.

The HSI is on because it's on after reset. And it's still on because you haven't asked to change it.

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

Thanks, so it's set for posterity? I think a comment might be more useful. That value evaporates after we return from SystemClock_Config(), unreferenced and all.

Edit: I'm talking about a local (unreferenced) variable, not the HSI itself, which you correctly state is running the show at this moment.

TDK
Guru

I don't know why it's set, but clearly it's not used in the call. Having it listed with the correct state (ON), does provide some limited info to the user. If it wasn't listed, I can imagine another user coming here and asking why HSI is on even though it's not listed, which is a similar but slightly different version of your question. If I wrote it, I wouldn't have listed it at all.

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