cancel
Showing results for 
Search instead for 
Did you mean: 

[Solved][Doubt] HAL Clock Config

GHern.2
Associate

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

4 REPLIES 4
Muhammed Güler
Senior III

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 Employee

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

TDK
Guru

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
Associate

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