Creating a task

When a task is created using dynamically allocated memory, the call will look something like this (see Chapter 7, The FreeRTOS Scheduler, for more details on the parameters that are not related to memory allocation):

BaseType_t retVal = xTaskCreate( Task1, "task1", StackSizeWords, NULL,
tskIDLE_PRIORITY + 2, tskHandlePtr);
assert_param(retVal != pdPASS);

There are a few relevant pieces of information, relevant to memory allocation, to note about this call:

  • The call to xTaskCreate may fail. This is because there is no guarantee that enough space will be available for storing the task's stack and TCB on the FreeRTOS heap. The only way to ensure that it was created successfully is to check the return value, retVal.
  • The only parameter to do with a stack is the requested size of the stack.

When created in this manner, if it is appropriate for a task to terminate itself, it may call xTaskDelete(NULL) and the memory associated with the task's stack and TCB will be available to be reused.

The following are a few points to note regarding dynamic allocation:

  • Primitive creation may fail at runtime if no heap space is available.
  • All memory that FreeRTOS allocates for the primitive will be automatically freed when the task is deleted (as long as Heap_1 is not used and INCLUDE_vTaskDelete is set to 1 in FreeRTOSConfig.h). This doesn't include memory that was dynamically allocated by user-supplied code in the actual task; the RTOS is unaware of any dynamic allocation initiated by user-supplied code. It is up to you to free this code when appropriate.
  • configSUPPORT_DYNAMIC_ALLOCATION must be set to 1 in FreeRTOSConfig.h for dynamic allocation to be available:

When creating a task using dynamic allocation, all of the memory used for the task, the task's stack, and TCB is allocated from the FreeRTOS heap, as shown in the preceding diagram.

Next, let's take a look at the different ways of creating queues.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.125.7