cancel
Showing results for 
Search instead for 
Did you mean: 

Hi! I am not sure why I can't recieve a correct value of pvParams inside the task. Pointer value in received correctly but when I cast it to a "const int*" or any other pointer it gives me rubbish when i dereference it.

DMark.1
Associate II
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);
 
  }
}

1 ACCEPTED SOLUTION

Accepted Solutions
hs2
Senior

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.

View solution in original post

3 REPLIES 3
hs2
Senior
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

DMark.1
Associate II

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!!

hs2
Senior

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.