/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / HSE and HSI usage in scheduler.c

Username:     
Password:     
             

Forum

# 1   2008-06-04 15:41:09 HSE and HSI usage in scheduler.c

armaniake
Member
From: USA, Texas
Registered: 2007-08-22
Posts: 38

HSE and HSI usage in scheduler.c

Hi,

I can see in scheduler.c that we use the external high speed oscillator via:

   /* Enable HSE */
   RCC_HSEConfig( RCC_HSE_ON );

We have a 12MHz oscillator on board. It makes sense we use the external clock, and not the HSI from STM32.

In the same file, I can see a call to  UTIL_SetPll( UTIL_ReadBackupRegister( BKP_PLL ) ).

Inside that function, we configure the SYSCLK to be derived from HSI, while the comments states in fact we want to use PLL in fact:

   /* Select PLL as system clock source */
   RCC_SYSCLKConfig( RCC_SYSCLKSource_HSI );


Also, is that not strange we set here SYSCLK to be derived from HSI (8MHz), while at the same time, we setup PLL related multipliers that are related to HSE like:

RCC_PLLConfig( RCC_PLLSource_HSE_Div1, RCC_PLLMul_6 );

And also, it seems that the PLL is disbaled, even though the comments states it should be enabled:

   /* Enable PLL */
   RCC_PLLCmd( DISABLE );


So, what is the clock tree branch we are wanting to select in fact?

Thanks you very much

Offline

 

# 2   2008-06-04 17:14:39 HSE and HSI usage in scheduler.c

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: HSE and HSI usage in scheduler.c

I will check why there is still a 'HSI' in the source files, but all calculations are done considering a 12MHz clock  (USB, ...) and it works.

Offline

 

# 3   2008-06-04 17:55:34 HSE and HSI usage in scheduler.c

armaniake
Member
From: USA, Texas
Registered: 2007-08-22
Posts: 38

Re: HSE and HSI usage in scheduler.c

Thank you for that,

The reason I need to know is to accurately setup the clock for my I2C bus. Right now, when I request 400KHz, I have 600KHz on the SCL. And I'm wandering if that has anything to do with it.

On an other platform, I have the expected 400KHz by using the HSE and PLL (but set to mult x 9, as it uses an HSE of 8MHz).

Appreciated your help

Offline

 

# 4   2008-06-04 21:23:11 HSE and HSI usage in scheduler.c

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: HSE and HSI usage in scheduler.c

I took a look at the code of the RCC_Configuration function. It has been copied from the examples provided by ST with the STM32 library.
I understand that the general idea is the following:
   1. Switch the system clock source to HSI (that is known as being stable, and already started),
   2. Set the PLL to the correct frequency, considering HSE as clock source
   3. When the PLL is stabilized, switch back the system clock source to the PLL output (iissued from HSE).
My understanding is that the HSI selection is just something temporary, to make the PLL stable. When stable, we can switch back to the PLL output.

Offline

 

# 5   2008-06-06 23:16:05 HSE and HSI usage in scheduler.c

armaniake
Member
From: USA, Texas
Registered: 2007-08-22
Posts: 38

Re: HSE and HSI usage in scheduler.c

Thanks for looking into it Francis. But I cannot relate what you are explaining to the datahseet,
and other projects (examples) provided by ST.


When the STM32 boots, SYSCLOCK is derived from HSI (chapter 4.2.6 of datasheet). There should be no need
to specify in the code to use it as a SYSCLOCK, as it is the only clock enabled at bootup anyway.

Also, in scheduler.c,

We are starting the HSE clock with:

RCC_HSEConfig( RCC_HSE_ON );

Then we are waiting (blocking) for the HSE clock to be ready

while( RCC_GetFlagStatus( RCC_FLAG_HSERDY ) == RESET )
{;}

This means that the whole application is blocked up to the moment the HSE is ready.

A few lines below, we find the jump to UTIL_SetPll() which sets only at that time in the code the SYSCLOCK
as being HSI. But why then, as by then HSE is ready to go anyway. You suggested we are starting from HSI while
waiting the PLL to stabilize. But that does not match what the code does, as we enable the HSE (while
HSI is the only clock running), we wait for HSE to stabilize and then we switch to HSI to finally switch back
to HSE clock a few line below in UTIL_SetPll().

If you look at example5 provided in the I2C example set from STLib, you will see that they set SYSCLOCK to HSE in main.c, and that is it. They do not seem to switch from HSI to HSE in their example. I check also other projects (non OS Circle related), and no one is really switching back and forth for standard application (no power down, etc.).


The following way of proceeding makes more sense to me, if you don't mind:

void RCC_Configuration(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();

  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);

  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();

  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);

    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
 
    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);

    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);

    /* PLLCLK = 12MHz * 6 = 72 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6);

    /* Enable PLL */
    RCC_PLLCmd(ENABLE);

    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }

    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }

A final thought for UTIL_SetPll().

The first two lines have wrong comments. That puzzels me. Does this say something? Are those lines old lines,
not being removed when the code was finalized?


   /* Select PLL as system clock source */
   RCC_SYSCLKConfig( RCC_SYSCLKSource_HSI );

   /* Enable PLL */
   RCC_PLLCmd( DISABLE );




Thanks for your continued and very good support Francis,

Offline

 

# 6   2008-06-08 17:13:36 HSE and HSI usage in scheduler.c

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: HSE and HSI usage in scheduler.c

You are right, the comments are wrong. Copied from the symetric actions done after setting the PLL. We will fix the comments.
We will also look at RCC_Configuration. Perhaps we kept the source of the very first version of the STM32 library that has been updated already several times. We will release the version 1.8 in the next few weeks.
Thanks for your suggestion.

Offline

 

Board footer