STM32 Start-up code explore

STM32 Start-up code explore

If you an Embedded Software engineer, you should know what is start-up code and what it’s job 

It’s a .c or .s file that executed before main()

It responsible for 

- copy the initialised data region(s)

- clear the BSS region(s)

- initialise the system

- run the preinit/init array (for the C++ static constructors)

- initialise the arc/argv

- branch to main()

- run the fini array (for the C++ static destructors)

- call _exit(), directly or via exit()

Frist, we start by assign the start and end addresses of the memory or its size from the linker files 

No alt text provided for this image


Then declaration of main() function and exit() , actually you could change the entry point to any other name .

No alt text provided for this image


Then the declaration of functions for hardware Initialise hardware right after reset, to switch clock to higher frequency and have the rest of the initialisations run faster.

This is the place where Cortex-M core will go immediately after reset, via a call or jump from the Reset_Handler.

No alt text provided for this image

 

Inside _start() we call __initialize_hardware_early () then Call the CMSIS system initialisation routine SystemInit() that config the RCC with high frequency to boost the startup process

No alt text provided for this image


Then we need to initialize .data and .bss sections

Early we know that always the .bss is initialized by 0 , but we could easly change it in line #158

No alt text provided for this image


If you write in c++ the compiler will use these two functions for the C++ static constructors and static destructors

No alt text provided for this image


Now we need to restore the clock source to default 

No alt text provided for this image


Then Get the argc/argv (useful in semihosting configurations)

No alt text provided for this image


After that we call  __run_init_array () to execute the constructors for the static objects).

Now call your program to be executed and save the return in case you want to check on it 

No alt text provided for this image


After exit from main Run the C++ static destructors.

No alt text provided for this image


Finally we want to stop the PC from increment to prevent to fetch old or bad instructions outside our actual program space in flash 

No alt text provided for this image


And its implementation only have simple infinite loop

No alt text provided for this image


That was the startup code of STM32F1XX built with newlib

Hope it helps .

Anil Dhanawade

Lead CoC Engineer

3 年

It's very important information about stm32. And even booting sequence also was a nice reading. Keep it up ??

要查看或添加评论,请登录

Eslam Shahin的更多文章

  • Interrupt Handling Techniques

    Interrupt Handling Techniques

    Hello all Today we will talk about Interrupt Handling techniques , 1 . Frist type is The Fixed Priority Interrupt…

    1 条评论
  • USB protocol

    USB protocol

    Introduction Universal Serial Bus (USB) is a widely-used protocol in most devices Its a industry standard way to…

  • Boot Sequence in ARM Cortex-M4

    Boot Sequence in ARM Cortex-M4

    what happens when you press the reset button? your main code does not start immediately as you think , lets know how…

    2 条评论

社区洞察

其他会员也浏览了