Skip to main content
freya17365
Associate III
January 18, 2017
Question

STM32F3xx Clock configuration

  • January 18, 2017
  • 6 replies
  • 1915 views
Posted on January 18, 2017 at 15:48

Hi to all,

this is my first project using a STM32F373 MCU, but I am not able even to let a LED blink.

I assume that system clock is not running as I get no response also enabling the MCO and check it with a scope.

For my clock initialization routine I used the cubemx code as base. Here is my code:

--------------------------------------------------------

/*-------------------------------- HSE - HSI Configuration -----------------------*/
 if(!HSEHSI)
 {
 if(!(RCC->CR & RCC_CR_HSION)) RCC->CR |= RCC_CR_HSION; // If HSI in not ON, just enable it
 while(!(RCC->CR & RCC_CR_HSIRDY));
 RCC->CR &= ~RCC_CR_PLLON; // Disable PLL
 RCC->CFGR &= ~RCC_CFGR_PLLSRC_HSE_PREDIV; // HSI as PLL source
 }
 else
 {
 RCC->CR &= ~RCC_CR_HSEBYP; // Disable HSE_BYPASS
 RCC->CR |= RCC_CR_HSEON; // Enable HSE
 while(!(RCC->CR & RCC_CR_HSERDY)); // Wait for HSE being ready
 RCC->CR &= ~RCC_CR_PLLON; // Disable PLL
 while(RCC->CR & RCC_CR_PLLRDY); // Wait for PLL disabled
 RCC->CFGR |= RCC_CFGR_PLLSRC_HSE_PREDIV; // HSE as PLL source
 RCC->CFGR &= ~RCC_CFGR_PLLXTPRE; // HSE not divided
 RCC->CFGR2 &= (RCC->CFGR2 & ~RCC_CFGR2_PREDIV) | RCC_CFGR2_PREDIV_DIV1; // Input clock not divided
 }
 RCC->CFGR = ((RCC->CFGR & ~RCC_CFGR_PLLMUL) | RCC_CFGR_PLLMUL9); // PLL = 9*HSE
 RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_SW) | RCC_CFGR_SW_PLL; // PLL as SYSTEM CLOCK
 RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_PLLXTPRE) | RCC_CFGR_PLLXTPRE_HSE_PREDIV_DIV1; // HSE/PREDIV clock not divided for PLL entry
 RCC->CFGR |= RCC_CFGR_USBPRE_DIV1_5; // USB divided by 3
 RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_PPRE2) | RCC_CFGR_PPRE2_DIV1; // APB2 not divided
 RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_PPRE1) | RCC_CFGR_PPRE1_DIV2; // APB1 divided by 2
 RCC->CFGR = (RCC->CFGR & ~RCC_CFGR_HPRE) | RCC_CFGR_HPRE_DIV1; // AHB not divided
 RCC->CFGR2 &= (RCC->CFGR & ~RCC_CFGR_MCO) | RCC_CFGR_MCO_SYSCLK; // Enable MCO
 RCC->CR |= RCC_CR_PLLON; // Enable PLL
 while(!(RCC->CR & RCC_CR_PLLRDY)); // Wait for PLL enabled
// FLASH->ACR = (FLASH->ACR &(~FLASH_ACR_LATENCY)) | FLASH_ACR_LATENCY_2; // Set FLASH latency = 2
 /*-------------------------------- LSE - LSI Configuration -----------------------*/
 if(!LSELSI)
 {
 if(!(RCC->CSR & RCC_CSR_LSION)) RCC->CSR |= RCC_CSR_LSION; //Enable LSI
 while(!(RCC->CSR & RCC_CSR_LSIRDY)); // Wait for LSI ready
 RCC->BDCR = (RCC->BDCR & ~RCC_BDCR_RTCSEL) | RCC_BDCR_RTCSEL_LSI; // LSI as RTC clock
 }
 else
 {
 PWR->CR |= PWR_CR_DBP; // Disable RTC write protect
 if(!(RCC->BDCR & RCC_BDCR_LSEON)) RCC->BDCR = (RCC->BDCR & ~RCC_BDCR_LSE) | RCC_BDCR_LSEON; // If LSE in not ON, just enable it
 while(!(RCC->BDCR & RCC_BDCR_LSERDY));
 RCC->BDCR = (RCC->BDCR & ~RCC_BDCR_RTCSEL) | RCC_BDCR_RTCSEL_LSE; // LSE as RTC clock
 }
 RCC->CIR = 0x00000000; // Disable all interrupts�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

--------------------------------------------------------

Someone encountered this problem already?

Do I make some mistakes?

Thank you

Freya

    This topic has been closed for replies.

    6 replies

    Khouloud GARSI
    ST Employee
    January 18, 2017
    Posted on January 18, 2017 at 16:00

    Hi Freya,

    I advise you to refer to the 'GPIO_IOToggle' example provided in the

    http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubef3.html

    package or in

    http://www.st.com/content/st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32-standard-peripheral-libraries/stsw-stm32108.html

    (

    STSW-STM32108) 

    under the following paths:
    • STM32Cube_FW_F3_V1.7.0\Projects\STM32373C_EVAL\Examples\GPIO\GPIO_IOToggle (cube)
    • STM32F30x_DSP_StdPeriph_Lib_V1.2.3\Projects\STM32F30x_StdPeriph_Examples\GPIO (SPL)

    Khouloud

    waclawek.jan
    Super User
    January 18, 2017
    Posted on January 18, 2017 at 16:09

    this is my first project using a STM32F373 MCU, but I am not able even to let a LED blink.

    Start with blinky with no touching the clocks, running on whatever default clock is after reset.

    Only if you get that running, start playing with clocks.

    Style advice: don't read-modify-write the registers unless absolutely necessary; prefer direct writes to them.

    JW

    freya17365
    Associate III
    January 19, 2017
    Posted on January 19, 2017 at 21:19

     ,

     ,

    Hi to all,

    I checked GPIO toggle and used deafult values, but nothing changed, so I reduced my program as much as I could:

    --------------------------------------------------------------------------------------------

    ♯ include 'stm32f3xx.h'

    ♯ define LED3 , ,  , , ,  ,1 , ,  , , ,  ,//GPIOD

     ,

    ♯ define LED2 , ,  , , ,  ,2 , ,  , , ,  ,//GPIOD

     ,

    ♯ define LED1 , ,  , , ,  ,3 , ,  , , ,  ,//GPIOD

     ,

    ♯ define LED0 , ,  , , ,  ,4 , ,  , , ,  ,//GPIOD

     ,

    void SystemInit()

     ,

    {

     ,

    // , ,  ,if(!(RCC->,CR &, RCC_CR_HSIRDY)) RCC->,CR |= RCC_CR_HSION,

     ,

    // , ,  ,while((RCC->,CR &, RCC_CR_HSIRDY) == 0), , ,  ,// Wait for HSI being ready

     ,

     , ,  ,return,

     ,

    }

    int main(void)

     ,

    {

     ,

     , ,  ,if(!(RCC->,AHBENR &, RCC_AHBENR_GPIODEN)) RCC->,AHBENR |= RCC_AHBENR_GPIODEN, , ,  , , ,  ,// Enable GPIOD clock , ,  ,

     ,

    // , ,  ,RCC->,AHBENR |= RCC_AHBENR_GPIODEN,

     ,

     , ,  ,

     ,

     , ,  ,GPIOD->,MODER &,= ~(0x03<,<,(2*LED3) | 0x03<,<,(2*LED2) | 0x03<,<,(2*LED1) | 0x03<,<,(2*LED0)), , ,  ,//Alternate function = output

     ,

     , ,  ,GPIOD->,MODER |= 0x01<,<,(2*LED3) | 0x01<,<,(2*LED2) | 0x01<,<,(2*LED1) | 0x01<,<,(2*LED0),

     ,

     , ,  ,GPIOD->,OTYPER &,= ~(0x01<,<,LED2 | 0x01<,<,LED1 | 0x01<,<,LED0), , ,  ,//Output push-pull

     ,

     , ,  ,GPIOD->,OTYPER |= 0x00<,<,LED3 | 0x00<,<,LED2 | 0x00<,<,LED1 | 0x00<,<,LED0,

     ,

     , ,  ,GPIOD->,OSPEEDR &,= ~(0x03<,<,(2*LED3) | 0x03<,<,(2*LED2) | 0x03<,<,(2*LED1) | 0x03<,<,(2*LED0)), , ,  ,//Pin speed = low speed

     ,

     , ,  ,GPIOD->,OSPEEDR |= 0x00<,<,(2*LED3) | 0x00<,<,(2*LED2) | 0x00<,<,(2*LED1) | 0x00<,<,(2*LED0),

     ,

     , ,  ,GPIOD->,PUPDR &,= ~(0x03<,<,(2*LED3) | 0x03<,<,(2*LED2) | 0x03<,<,(2*LED1) | 0x03<,<,(2*LED0)), , ,  ,//No Pull up or Pull Down

     ,

     , ,  ,GPIOD->,PUPDR |= 0x00<,<,(2*LED3) | 0x00<,<,(2*LED2) | 0x00<,<,(2*LED1) | 0x00<,<,(2*LED0),

     ,

     , ,  ,

     ,

     , ,  ,GPIOD->,BSRRL |= 1<,<,LED3 | 1<,<,LED2 | 1<,<,LED1 | 1<,<,LED0,

     , , ,

     ,

     , , , while(1)

     ,

     , ,  ,{  ,

     ,

     , ,  ,}

     ,

    }

    --------------------------------------------------------------------------------------------

    Assuming that clock configuration in automatically set at reset, I configure GPIO pins, but nothing happenes.

    Similar code on STM32F030 works, so I do not understand why not here.

    Thank you

    Freya

    waclawek.jan
    Super User
    January 19, 2017
    Posted on January 19, 2017 at 22:52

    Looks good. Question is, whether the startup code is OK.

    Is this a known good board such as a Nucleo? If not, I'd double-check all ground and all supply pins including VCAPs (measuring the voltage on them and checking against datasheet).

    JW

    freya17365
    Associate III
    January 24, 2017
    Posted on January 24, 2017 at 20:07

    Hi JW,

    it is a my own board, not a welll known one.

    I tried to modify the code using include files for stm32f0xx MCU's, but leaving startup_stm32f373xc.s as startup file and the programs worked fine.

    But every time I try to select stm32f373as MCU, it does not work.

    Is it my fault or is it a compiler (Keil) fault?

    Do I have to check some settings?

    Thank you

    Freya
    waclawek.jan
    Super User
    January 24, 2017
    Posted on January 24, 2017 at 22:44

    I don't understand what did you do, but mixing files intended for different families won't do any good.

    The ST-provided startup makes usually calls to routines setting up clocks etc.

    /

    *

    Call

    the clock system intitialization function.

    *

    /

    bl

    SystemInit

    /

    *

    Call

    static constructors

    *

    /

    bl

    __libc_init_array

    Try to comment them out.

    JW

    freya17365
    Associate III
    January 28, 2017
    Posted on January 28, 2017 at 17:13

    Hi to all,

    the problem is partially solved.

    I added the following lines:

      /* FPU settings ------------------------------------------------------------*/

      #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)

        SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */

      #endif

    and

     SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */

    and now everything works. The remaining part of problem is that MCu works at 36 MHz instead of 72 MHz

    Freya