cancel
Showing results for 
Search instead for 
Did you mean: 

SOS:Why the main function will automatically cycle three times??

nate lee
Associate II
Posted on April 20, 2017 at 19:05

To who may help:

    I am a beginer with stm32. Today, I have some promble with my board(stm32f407xx): the main fuction will automatically cycle three times. codes are follow:

#include ''stm32f4xx.h''

int main(void)

{

   /* this function just do some init work for uart1 */

   uart_init(115200);

   /* printf already r

edirect to UART1

 */

   printf(''please....­\r\n\r\n'');

   return 0;

}

    the result from Serial debugging assistant:

0690X00000606jqQAA.png

        My friend told me I should add while(1) at the end of code. But I have write some test code without while(1) before, and it work well. It is really puzzled me. Why?Why?Why?  By the way, I use Keil MDK5 to build my code. hope someone can help me.

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on April 20, 2017 at 21:53

Your friend is right, you shouldn't exit main(), it goes nowhere you want to go. Most likely crashing. It is called with no expectation of returning. Look at code in startup_stm32xxxx.s

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

View solution in original post

5 REPLIES 5
Posted on April 20, 2017 at 21:53

Your friend is right, you shouldn't exit main(), it goes nowhere you want to go. Most likely crashing. It is called with no expectation of returning. Look at code in startup_stm32xxxx.s

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
Posted on April 21, 2017 at 08:27

MCU main function is not called from a command prompt with passing parameter.

Typically, bare metal would look like this:

int main(void) {

// Init functions

while(1) {

// here is the main loop as long as MCU has power

 };

return 0;

}

Posted on April 21, 2017 at 09:39

What it means - there is no OS (like Windows, Linux, etc.) your application can return to.

It goes nowhere, as Clive mentioned, or, if the startup code contains a kind of 'safety net', catches a return from main in an endless loop.

That is one major characteristic difference of embedded software.

Posted on April 21, 2017 at 14:52

haha, it is a stupid question of a beginer.thank you so much.

Posted on April 21, 2017 at 14:54

thanks AvaTar, you let me know more about 

embedded software.