2022-02-16 07:46 AM
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART3_UART_Init();
const int* task_1_Param = 1;
const int* task_2_Param = 2;
xTaskCreate (vTask, "T1", 150, (void *)&task_1_Param, 1, NULL);
xTaskCreate (vTask, "T2", 150, (void *)&task_2_Param, 1, NULL);
vTaskStartScheduler();
}
void vTask(void * pvParams) {
const int *tParam = (const int *)pvParams;
HAL_Delay(100);
for (;;) {
printf("Task_%d Running.\n", *tParam);
printf("Task_%d Complete - Yielding Task.\n", *tParam);
taskYIELD();
HAL_Delay(1000);
}
}
Solved! Go to Solution.
2022-02-17 08:57 AM
A variable with automatic storage is not stored in ROM even if declared const but allocated on stack which is always RAM.
In a normal C program your original implementation would be correct because the variables would be still valid or alive even if stack allocated, because their scope or block (defined by curly braces) is not left.
But in this particular case FreeRTOS is not fully compliant to the C/C++ language because starting the FreeRTOS scheduler on Cortex-M MCUs resets and reuses the main stack for ISRs and uses the process stack for tasks. This corrupts the main stack allocated variables.
As you mentioned static or global variables are not stack allocated and have an unlimited lifetime and remain valid.
2022-02-16 08:19 AM
const int task_1_Param = 1;
would be probably better or more readable if you want an integer (value) as parameter.
However, the problem is that on Cortex-M MCUs FreeRTOS resets and reuses the main stack as ISR stack when starting the scheduler.
Hence declare the parameters e.g. as static const int and you'll be fine.
See also https://www.freertos.org/FAQHelp.html 3.main() stack
2022-02-17 12:17 AM
Thank you. I got the idea.
So even the const variables doesn't live until the end of the whole program??? As far as I know they are stored in ROM, am I right? Compared to const, static variables lives until the end of program execution!!
2022-02-17 08:57 AM
A variable with automatic storage is not stored in ROM even if declared const but allocated on stack which is always RAM.
In a normal C program your original implementation would be correct because the variables would be still valid or alive even if stack allocated, because their scope or block (defined by curly braces) is not left.
But in this particular case FreeRTOS is not fully compliant to the C/C++ language because starting the FreeRTOS scheduler on Cortex-M MCUs resets and reuses the main stack for ISRs and uses the process stack for tasks. This corrupts the main stack allocated variables.
As you mentioned static or global variables are not stack allocated and have an unlimited lifetime and remain valid.