cancel
Showing results for 
Search instead for 
Did you mean: 

which file starts STM32

MNapi
Senior III

I do have this demo program in which main.c is disabled so it would start from main_pinpong.c which is the only file with

int main( void )

{

included in target build

 

startup_stm32l476xx.s is supposed to be the startup file but I do not see any reference to

main.c or main_pinpong.c

 

I see that main.c is not included in target build but main_pinpong.c is included

so they question is what points the compiler to execute int main( void ) from main_pinpong.c

 

 

 Untitled.png

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

The program starts from ResetHandler which is typically in startup_stm32*.s.

main() is called at the end

 

 

 

Reset_Handler:
  ldr   sp, =_estack      /* set stack pointer */

...


/* Call static constructors */
    bl __libc_init_array
/* Call the application's entry point.*/
  bl  main

 

 

 

When the program is being linked, the linker looks for the "main" symbol and jumps to that on the "bl main" line. It doesn't care which file it's in. If multiple "main" functions are compiled in your project, the linker will give you an error about duplicate symbols.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
TDK
Guru

The program starts from ResetHandler which is typically in startup_stm32*.s.

main() is called at the end

 

 

 

Reset_Handler:
  ldr   sp, =_estack      /* set stack pointer */

...


/* Call static constructors */
    bl __libc_init_array
/* Call the application's entry point.*/
  bl  main

 

 

 

When the program is being linked, the linker looks for the "main" symbol and jumps to that on the "bl main" line. It doesn't care which file it's in. If multiple "main" functions are compiled in your project, the linker will give you an error about duplicate symbols.

If you feel a post has answered your question, please click "Accept as Solution".

@MNapi wrote:

what points the compiler to execute int main( void ) from main_pinpong.c


The compiler doesn't execute any of the source code - it just compiles it.

The C language specification states that the entry point is called main.

As @TDK said, the startup code populates the Reset vector and provides the jump to main.

thanks for clarification this is only reference to main I can find, I guess this must be the place.

 

Untitled1.png

In Keil __main is a wrapper function that calls scatterload and then main

You can generate a disassembly using FROMELF -CSD project.axf  >project.lst

The scatter loader unpacks the RAM initialization from FLASH, basically the equivalent code to that is used by GNU/GAS to copy and zero memory space, but via tables generated by the linker.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@MNapi wrote:

thanks for clarification this is only reference to main I can find, I guess this must be the place.


Yes, it is.

This is the "call":

LDR  R0, =__main
BX R0

 

Please see  How to insert source code - not as an image!