Static and Restricted task creation pinned to core
Posted: Thu Sep 11, 2025 1:25 am
I am reading FreeRtos (IDF) ESP32 documentation.
Some issues I have to clarify the reading.
I understood that IDF ESP32 port use bytes instead of words in memory allocation.
When is dynamically allocated is understood that change does not affect, but as is said this is the lazy memory allocation. Basically I understood that each context switch must save and restore all data in each task. So are versions to do task allocated statically that will reduce, if my appreciation is correct, the time of each context switch considerably if have enough memory to work with. Also will have less conflict of both cores using memory under switch.
In the documentation is not described TaskCreatepinnedToCore and TaskCreatepinnedToCoreStstic AND DON'T MENTION IF EXISTS TaskCreatePinnedToCoreRestricted
The documentation of Static creation use an Array and code is copied from FreeRtos Reference Manual in which depth is handled in words and have this detailed example
/* Dimensions the buffer that the task being created will use as its stack. NOTE: This is the
number of words the stack will hold, not the number of bytes. For example, if each stack item
is 32-bits, and this is set to 100, then 400 bytes (100 * 32-bits) will be allocated. */
#define STACK_SIZE 200
/* Structure that will hold the TCB of the task being created. */
StaticTask_t xTaskBuffer;
/* Buffer that the task being created will use as its stack. Note this is an array of
StackType_t variables. The size of StackType_t is dependent on the RTOS port. */
StackType_t xStack[ STACK_SIZE ];
THE IDF ESP32 DOCUMENTATION DON'T HAVE THE FIRST LINE EXPLANATION AND ARRAY SEENS TO BE IN BYTES BEING CORRECT THE DOCUMENTATION AND ARRAY FOR MEMORY ALLOCATION IS ALIGNED BY BYTE.
BUT THE DETAILS OF PINNED TO CORE PARAMETERS IS NOT SHOWN.
IS USAGE OF NOTIFICATIONS IS POSSIBLE BETWEEN STATIC DEFINED TASK?
The restricted memory pinned to core is not described. DOES IT EXIST?
Is mentioned task creation restricted but in other way as in FreeRtos Reference Manual. In thid reference manual is required memory allocation depending on compiler, GCC for example
char cTaskStack[ 1024 ] __attribute__((align(1024));
That is a byte array ALIGNED.
Then requires two structures OUT OF THE MAIN OR ANY FUNCTION to be statically allocated
/* Declare the stack that will be used by the protected task being created. The stack alignment
must match its size, and be a power of 2. So, if 128 words are reserved for the stack then it
must be aligned on a ( 128 * 4 ) byte boundary. This example uses GCC syntax. */
static portSTACK_TYPE xTaskStack[ 128 ] __attribute__((aligned(128*4)));
/* Declare an array that will be accessed by the protected task being created. The task should
only be able to read from the array, and not write to it. */
char cReadOnlyArray[ 512 ] __attribute__((aligned(512)));
/* Fill in a TaskParameters_t structure to define the task - this is the structure passed to the
xTaskCreateRestricted() function. */
static const TaskParameters_t xTaskDefinition =
{
vTaskFunction, /* pvTaskCode */
"A task", /* pcName */
128, /* usStackDepth - defined in words, not bytes. */
NULL, /* pvParameters */
1, /* uxPriority - priority 1, start in User mode. */
xTaskStack, /* puxStackBuffer - the array to use as the task stack. */
/* xRegions - In this case only one of the three user definable regions is actually used.
The parameters are used to set the region to read only. */
{
/* Base address Length Parameters */
{ cReadOnlyArray, 512, portMPU_REGION_READ_ONLY },
{ 0, 0, 0 },
{ 0, 0, 0 },
}
};
THEN IN MAIN IS CREATED THE STATIC RESTRICTED TASK
void main( void )
{
/* Create the task defined by xTaskDefinition. NULL is used as the second parameter as a
task handle is not required. */
xTaskCreateRestricted( &xTaskDefinition, NULL );
In the IDF ESP 32 ALLOCATION IS INSIDE OF THE HANDLE OF A TASK THAT IT SUPPOSED TO BE CREATED RESTRICTED AND ALLOCATE MEMORY
static const MemoryRegion_t xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
{
// Base address Length Parameters
{ ucOneKByte, 1024, portMPU_REGION_READ_WRITE },
{ 0, 0, 0 },
{ 0, 0, 0 }
};
void vATask( void *pvParameters )
{
// This task was created such that it has access to certain regions of
// memory as defined by the MPU configuration. At some point it is
// desired that these MPU regions are replaced with that defined in the
// xAltRegions const struct above. Use a call to vTaskAllocateMPURegions()
// for this purpose. NULL is used as the task handle to indicate that this
// function should modify the MPU regions of the calling task.
vTaskAllocateMPURegions( NULL, xAltRegions );
SO IS MISSING AN EXPLANATION FOR RESTRICTED MEMORY ALLOCATION AND ONLY SHOWS A RELOCATION OF A PREVIOUSLY ALLOCATED MEMORY INSIDE OF A HANDLER. IS IT CORRECT OR SHOULD BE DONE OUT OF MAIN AND DOING AN ALIGNED ARRAY?
IS IT POSSIBLE DO THIS PINNED TO A CORE?
Many thanks in advance for your comments.
Some issues I have to clarify the reading.
I understood that IDF ESP32 port use bytes instead of words in memory allocation.
When is dynamically allocated is understood that change does not affect, but as is said this is the lazy memory allocation. Basically I understood that each context switch must save and restore all data in each task. So are versions to do task allocated statically that will reduce, if my appreciation is correct, the time of each context switch considerably if have enough memory to work with. Also will have less conflict of both cores using memory under switch.
In the documentation is not described TaskCreatepinnedToCore and TaskCreatepinnedToCoreStstic AND DON'T MENTION IF EXISTS TaskCreatePinnedToCoreRestricted
The documentation of Static creation use an Array and code is copied from FreeRtos Reference Manual in which depth is handled in words and have this detailed example
/* Dimensions the buffer that the task being created will use as its stack. NOTE: This is the
number of words the stack will hold, not the number of bytes. For example, if each stack item
is 32-bits, and this is set to 100, then 400 bytes (100 * 32-bits) will be allocated. */
#define STACK_SIZE 200
/* Structure that will hold the TCB of the task being created. */
StaticTask_t xTaskBuffer;
/* Buffer that the task being created will use as its stack. Note this is an array of
StackType_t variables. The size of StackType_t is dependent on the RTOS port. */
StackType_t xStack[ STACK_SIZE ];
THE IDF ESP32 DOCUMENTATION DON'T HAVE THE FIRST LINE EXPLANATION AND ARRAY SEENS TO BE IN BYTES BEING CORRECT THE DOCUMENTATION AND ARRAY FOR MEMORY ALLOCATION IS ALIGNED BY BYTE.
BUT THE DETAILS OF PINNED TO CORE PARAMETERS IS NOT SHOWN.
IS USAGE OF NOTIFICATIONS IS POSSIBLE BETWEEN STATIC DEFINED TASK?
The restricted memory pinned to core is not described. DOES IT EXIST?
Is mentioned task creation restricted but in other way as in FreeRtos Reference Manual. In thid reference manual is required memory allocation depending on compiler, GCC for example
char cTaskStack[ 1024 ] __attribute__((align(1024));
That is a byte array ALIGNED.
Then requires two structures OUT OF THE MAIN OR ANY FUNCTION to be statically allocated
/* Declare the stack that will be used by the protected task being created. The stack alignment
must match its size, and be a power of 2. So, if 128 words are reserved for the stack then it
must be aligned on a ( 128 * 4 ) byte boundary. This example uses GCC syntax. */
static portSTACK_TYPE xTaskStack[ 128 ] __attribute__((aligned(128*4)));
/* Declare an array that will be accessed by the protected task being created. The task should
only be able to read from the array, and not write to it. */
char cReadOnlyArray[ 512 ] __attribute__((aligned(512)));
/* Fill in a TaskParameters_t structure to define the task - this is the structure passed to the
xTaskCreateRestricted() function. */
static const TaskParameters_t xTaskDefinition =
{
vTaskFunction, /* pvTaskCode */
"A task", /* pcName */
128, /* usStackDepth - defined in words, not bytes. */
NULL, /* pvParameters */
1, /* uxPriority - priority 1, start in User mode. */
xTaskStack, /* puxStackBuffer - the array to use as the task stack. */
/* xRegions - In this case only one of the three user definable regions is actually used.
The parameters are used to set the region to read only. */
{
/* Base address Length Parameters */
{ cReadOnlyArray, 512, portMPU_REGION_READ_ONLY },
{ 0, 0, 0 },
{ 0, 0, 0 },
}
};
THEN IN MAIN IS CREATED THE STATIC RESTRICTED TASK
void main( void )
{
/* Create the task defined by xTaskDefinition. NULL is used as the second parameter as a
task handle is not required. */
xTaskCreateRestricted( &xTaskDefinition, NULL );
In the IDF ESP 32 ALLOCATION IS INSIDE OF THE HANDLE OF A TASK THAT IT SUPPOSED TO BE CREATED RESTRICTED AND ALLOCATE MEMORY
static const MemoryRegion_t xAltRegions[ portNUM_CONFIGURABLE_REGIONS ] =
{
// Base address Length Parameters
{ ucOneKByte, 1024, portMPU_REGION_READ_WRITE },
{ 0, 0, 0 },
{ 0, 0, 0 }
};
void vATask( void *pvParameters )
{
// This task was created such that it has access to certain regions of
// memory as defined by the MPU configuration. At some point it is
// desired that these MPU regions are replaced with that defined in the
// xAltRegions const struct above. Use a call to vTaskAllocateMPURegions()
// for this purpose. NULL is used as the task handle to indicate that this
// function should modify the MPU regions of the calling task.
vTaskAllocateMPURegions( NULL, xAltRegions );
SO IS MISSING AN EXPLANATION FOR RESTRICTED MEMORY ALLOCATION AND ONLY SHOWS A RELOCATION OF A PREVIOUSLY ALLOCATED MEMORY INSIDE OF A HANDLER. IS IT CORRECT OR SHOULD BE DONE OUT OF MAIN AND DOING AN ALIGNED ARRAY?
IS IT POSSIBLE DO THIS PINNED TO A CORE?
Many thanks in advance for your comments.