Skip to main content
GHern.2
Associate
December 1, 2021
Question

[Solved][Doubt] HAL Clock Config

  • December 1, 2021
  • 4 replies
  • 2715 views

Hi everyone. It's my first time posting here. I'm learning how to program STM32, especifically an STM32F303RE Nucleo Board using HAL libraries.

These are my resources to learn:

  • Mastering STM32 from Carmine Noviello
  • STM32F303RE User Manual and Datasheet
  • UM1786 Description of STM32F3 HAL and low-layer drivers.

I'm trying to configure HSI oscilator using HAL drivers this is a part of my code (still incomplete):

void SystemClock_Config(){
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
 
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
 RCC_OscInitStruct.HSEPrediValue = RCC_HSE_PREDIV_DIV1;
 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
 RCC_OscInitStruct.LSIState = RCC_HSI_OFF;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.LSIStae = RCC_LSI_OFF;
 RCC_PLLInitTypeDef.RCC_OscInitTypeDef = ???
}

As you can see, I pretend use HSI (its on), so, HSEState are OFF also LSE = OFF and so one. I compared my code with the generated with CubeMX:

void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
 
 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
 RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;

As you can see, in this code CubeMX doesn't specify that HSE, LSI, are off. Only apears HSI = ON.

So my doubt is:

Can I skip to initialize these structure members for configure the osc?

Do they have a predefined value?

I hope you can help me. Ty

This topic has been closed for replies.

4 replies

Muhammed Güler
Senior III
December 1, 2021

After the MCU resets, very little hardware is operational. Since you can assign other functions to the HSE and LSE pins, it is closed at startup.

simply everything is turned off except mandatory hardware like ram rom. If you don't install the hardware at boot, it will remain closed.

Mike_ST
ST Technical Moderator
December 1, 2021

>> Can I skip to initialize these structure members for configure the osc?

Yes, as example, HSEState field will only be taken in account when calling the HAL_RCC_OscConfig if OscillatorType field is OR'ed with RCC_OSCILLATORTYPE_HSE.

Same for other like LSE,LSI, etc...

>> Do they have a predefined value?

As default they are 0 since RCC_OscInitStruct is declared with ={0};

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. 
TDK
December 1, 2021

The only thing this piece of code does is try to turn off the HSI (since HSIState = 0 = RCC_HSI_OFF):

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
 RCC_OscInitStruct.HSEPrediValue = RCC_HSE_PREDIV_DIV1;
 RCC_OscInitStruct.LSEState = RCC_LSE_OFF;
 RCC_OscInitStruct.LSIState = RCC_HSI_OFF;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.LSIStae = RCC_LSI_OFF;

There are also two typos here:

>  RCC_OscInitStruct.LSIState = RCC_HSI_OFF;

>  RCC_OscInitStruct.LSIStae = RCC_LSI_OFF;

"If you feel a post has answered your question, please click ""Accept as Solution""."
GHern.2
GHern.2Author
Associate
December 2, 2021

Ok, now I understand. Ty so much guys !!!. :D