0
\$\begingroup\$

I'm using a Nucleo board with STM32CubeIDE and regarding a system initialization file I have different implementation comparing the examples I came across online.

In many examples the system initialization system_stm32f3xx.c file is like the one here.

In this file the function SystemInit looks like:

void SystemInit(void)
{
  /* FPU settings ------------------------------------------------------------*/
  #if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
    SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
  #endif

  /* Reset the RCC clock configuration to the default reset state ------------*/
  /* Set HSION bit */
  RCC->CR |= (uint32_t)0x00000001;

  /* Reset CFGR register */
  RCC->CFGR &= 0xF87FC00C;

  /* Reset HSEON, CSSON and PLLON bits */
  RCC->CR &= (uint32_t)0xFEF6FFFF;

  /* Reset HSEBYP bit */
  RCC->CR &= (uint32_t)0xFFFBFFFF; 

Also in the file there is a function called SetSysClock.

But in my case this file is different. First of all, the SystemInit function looks like:

void SystemInit(void)
{
/* FPU settings --------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
  SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2));  /* set CP10 and CP11 Full Access */
#endif

#ifdef VECT_TAB_SRAM
  SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
  SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif
}

And secondly, there is no function called SetSysClock.

File names and descriptions same but the ingredients differ a lot. Why is that so?

\$\endgroup\$

2 Answers 2

0
\$\begingroup\$

There are dozens of different F3xx series MCUs.

Different MCUs need different initialization.

Same MCUs in different applications need different initializations if you want to use them for different purposes.

Different versions/era of the examples will also have different initialization code.

\$\endgroup\$
2
  • \$\begingroup\$ But all of the have them same description at the beginning: @file system_stm32f3xx.c * @ author MCD Application Team * @ brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File. \$\endgroup\$
    – user1245
    Dec 13, 2022 at 18:40
  • \$\begingroup\$ @user1245 Are they all also having the same release version and release date? \$\endgroup\$
    – Justme
    Dec 13, 2022 at 18:51
0
\$\begingroup\$

There are different ways to initialize the system, it depends very much on implementation.

For example, in most basic situation, you don't need to initialize the clock at all, the MCU will run on the default 16MHz internal RC even with external clock attached. It's just the default reset state. So initialization may even never mention clock at all.

Same goes for vector table configuration. If you don't know what it is or how it works (in detail), you don't need to touch it at all, you're totally ok with its default placement. So it will also work without mentioning vector table.

Same applies for FPU. By default it's disabled, and if you don't need it, you don't have to have it in the init. If you use floating point functions, and your compiler is configured to generate FPU instructions, then using floating points will make you hardfault. FPU needs to be activated before the first floating point instruction is executed, which makes logical sense.

None of those things have to be in the init, because you can do any of that in your main code at any point in time. Nothing stops you from reconfiguring clock or activating FPU in main(). It's just a matter of code placement.

Personally, I like having init as tiny as possible. For me its goal is just to make CPU run. As long as reset vector before main() places data from flash into RAM, I'm good, that's all I want MCU to do before my main(). Everything else that I do, I do via function calls from main(), because I won't need to search around system init files to find how to move vector table. I will have my own function that does it, which I can call from main() or any other place and time in my program. And that function will be very similar to what you see in init. If you feel like you want to place some initializations that you probably won't change at runtime (such as system clock in most basic cases, or FPU enable), by all means. If you use someone's init, like here, just take a look in there to see what it does and what it doesn't do so that you know you may have to do that yourself.

Bottom line is, your MCU has default settings that it will run totally ok with without doing anything and with empty init. Where you place clock changing or FPU-activating code doesn't really matter. As long as your code is consistent in style and logic of placement of things.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.