简体   繁体   中英

How to Restart tasks in FreeRTOS?

I create 2 tasks which consumes some RAM, and when they are done doing what they have to do, i need to restart them automatically, is it even possible to do that? i have read many articles and still not found the answer, please help.

How is the RAM consumed. If it is from the FreeRTOS heap then you will need to free the heap RAM manually before restarting or deleting the tasks.

When you say "restart" the task, what exactly do you mean?

1) Delete the task then create it again? If so then you can use the vTaskDelete() and xTaskCreateStatic API functions. However, while it is common for people to want to do this, it is rare for people to actually NEED to do this - much better to keep the same task and have the task structured such that it can loop back to the top of its implementation rather than be re-created.

2) As per the second half of point (1) above, if you just mean have the task start from the beginning of its implementation again you can just place the task in a loop that, when you reach the bottom of the loop, you go back to the top and wait for a signal to start again.

If you absolutely must have the task deleted then automatically restarted again then create a function that creates the task, then use the xTimerPendFunctionCall() API function to pend the function just before the task deletes itself [by calling vTaskDelete( NULL )].

Generally U should not restart tasks. That is not good idea. Better run them in endless loop. Create with minimal stacksize, allocate and free memory while working.

void* create_taskX()
{
   xTaskCreate( ..., configMINIMAL_STACK_SIZE, ...);
}

void taskX(void*arg)
{
  // Do not create big variables on stack to prevent overflow
  while(1)
  {
    void *resource = malloc(resource_size);  // Allocate resources before job
    // Do the work. Repeat with delay
    free(resource); // Free resources after job
    vTaskDelay(timeout_ticks);
    // or xSemaphoreTake();  // Wait for event from another task or interrupt
    // There are many other ways to block task, make it wait some event
  }
}

When you create Task Good memory deallocation(free) implemented in heap_4.c and heap_5.c . Or use heap_3.c to redirect FreeRTOS malloc and free to STL functions. Don't use Heap 1 and 2 if you want to use malloc/free often or recreate tasks.

You also can create mutex to block task1 while task2 works and vise versa. That will allow to use memory by one task at the time.

And at the end. You question is very general. The answer is too. Be concrete. Describe more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM