Code inside vs. outside tasks

anguel78 wrote on Friday, November 04, 2011:


I have a general question regarding tasks. Let’s say that I have non-RTOS code that I would like to convert to a FreeRTOS task. The existing code performs some initialization (e.g. HW setup) only at startup and then enters the main program loop. Now if I move the code to FreeRTOS I could put all the code as is including those initialization stiff inside a task function or I could try to isolate the init stuff into my main() so that it runs before the scheduler is started. Only the main program loop would then go into the task function. Are there any benefits (e.g. resource saving) that are worth that additional program modification? Thank you in advance for the clarification.


davedoors wrote on Friday, November 04, 2011:

Normally hardware would be set up before the scheduler was started. You can disable interrupts if necessary, as they will be automatically be re-enabled when the first task starts.

anguel78 wrote on Friday, November 04, 2011:

Thank you very much for the help.

richard_damon wrote on Friday, November 04, 2011:

My general rule is hardware initilzation (which tends to want to be done before interrupts are put “on the air”) and the creating of communication resources (queue, semaphores, and mutes) generally want to be done before the scheduler starts.  Sometimes a device needs a longer “configuration” protocol that might go into the beginning of a task, especially if it needs delays or interrupt responses.