Skip to main content
nate lee
Associate
Apr 20, 2017 5:05 PM UTC
Solved

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

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.

    This topic has been closed for replies.
    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    Apr 20, 2017 7:53 PM UTC
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..

    5 replies

    Tesla DeLorean
    Tesla DeLoreanBest answer
    Guru
    Apr 20, 2017 7:53 PM UTC
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    AvaTar
    Senior III
    Apr 21, 2017 9:39 AM UTC
    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.

    nate lee
    nate leeAuthor
    Associate
    Apr 21, 2017 2:54 PM UTC
    Posted on April 21, 2017 at 14:54

    thanks AvaTar, you let me know more about 

    embedded software.

    S.Ma
    Principal
    Apr 21, 2017 6:27 AM UTC
    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;

    }

    nate lee
    nate leeAuthor
    Associate
    Apr 21, 2017 2:52 PM UTC
    Posted on April 21, 2017 at 14:52

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