This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CCS/TM4C1294NCPDT: What will happen if I remove the “for( ;; );” from the main?

Part Number: TM4C1294NCPDT

Tool/software: Code Composer Studio

Actually I read from the Mastering in the FreeRTOS documentation that,

/* If all is well then main() will never reach here as the scheduler will
now be running the tasks. If main() does reach here then it is likely that
there was insufficient heap memory available for the idle task to be created.
Chapter 2 provides more information on heap memory management. */
for( ;; );

what if I removed for( ;; ); from the main? I mean in case if anything happens in the project and if the control goes to that infinite loop then the product will hang forever.

So my question is why we use this?? can we remove it??

  • Let me start by stating we do not provide support for FreeRTOS as we do not have the expertise. We do provide support for TIRTOS.

    But your question is simple. If you remove the for(;;); loop at the end of main, and there are not issues creating the idle task, then nothing happens. If there is an error in creating the idle task, you will execute the return 0, which will take you to ResetISR(). .The code will try to return from that function, but there is no valid return address on the stack. What happens next depends on the stack content or location. Most likely you will end up executing a fault and end in a fault routine.

  • lets say if we have code

    void main(void)
    {
    
    }

    at the end contains a return statement although we didn't explicitly put it there ourselves. When main returns the CPU executes the next instruction which is simply a GOTO to go back to the beginning of the code. main() is simply called over and over again. So why we preffer to stuck in while loop (at the end of the main) rather than calling main function again and again.

    Sir please can you elaborate what you are saying. I really want to clear this concept in my mind.

  • I don't know what implementation of FreeRTOS you are looking at. The port that comes with TivaWare uses startup code such as found in:

    C:\ti\TivaWare_C_Series-2.2.0.295\third_party\FreeRTOS\Demo\CORTEX_LM3Sxxxx_Eclipse\RTOSDemo\startup.c

    The reset vector points to the function ResetISR(). That function calls main(). If you return from main(), you return to ResetISR(). You are at the end of that function so you would return to a function that called ResetISR, but ResetISR was never called. Where the PC goes depends on where the stack was located and what was on it. That is the behavior I described above. (In truth the ResetISR calls main() directly because the call is at the end of ResetISR and the compiler does function tail call optimization.) In our implementation there is no GOTO to go back to the beginning of the code.