Creating Tasks

Thread Starter

rt694157

Joined Dec 15, 2019
78
I have been visited on the page 49-53 https://www.freertos.org/wp-content...eal_Time_Kernel-A_Hands-On_Tutorial_Guide.pdf I do not understand complete example code

C:
void vTask1( void *pvParameters )
{
  const char *pcTaskName = "Task 1 is running\r\n";
  volatile uint32_t ul; /* volatile to ensure ul is not optimized away. */
 
  for( ;; )
  {
    /* Print out the name of this task. */
    vPrintString( pcTaskName );
    /* Delay for a period. */
    for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
     {
        
     }
  }
}

void vTask2( void *pvParameters )
{
   const char *pcTaskName = "Task 2 is running\r\n";
   volatile uint32_t ul; /* volatile to ensure ul is not optimized away. */
  
   for( ;; )
   {
     /* Print out the name of this task. */
     vPrintString( pcTaskName );
      /* Delay for a period. */
     for( ul = 0; ul < mainDELAY_LOOP_COUNT; ul++ )
     {
      
     }
   }
}


int main( void )
{

 xTaskCreate( vTask1, /* Pointer to the function that implements the task. */
 "Task 1", /* Text name for the task. This is to facilitate debugging only. */
 1000, /* Stack depth - small microcontrollers will use much
 less stack than this. */
 NULL, /* This example does not use the task parameter. */
 1, /* This task will run at priority 1. */
 NULL ); /* This example does not use the task handle. */
 /* Create the other task in exactly the same way and at the same priority. */
 xTaskCreate( vTask2, "Task 2", 1000, NULL, 1, NULL );
 /* Start the scheduler so the tasks start executing. */
 vTaskStartScheduler();

 /* If all is well then main() will never reach here as the scheduler will
 now be running the tasks. If main() does reach here then it is likely that
 there was insufficient heap memory available for the idle task to be created.
 Chapter 2 provides more information on heap memory management. */
 for( ;; );
}
What is main purpose of the code ?
 

Papabravo

Joined Feb 24, 2006
21,159
To demonstrate the creation of two tasks, which are each scheduled and run by the procedure vTaskScheduler() which never returns. You can see that a task is running because it prints a message to that effect and then enters a delay loop. Since both tasks have the same priority, they should alternate, and you should see about the same number of lines printed by each task. We can't see the rest of the code so I don't know what mechanism the scheduler will use to preempt the runnning task.
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
To demonstrate the creation of two tasks, which are each scheduled and run by the procedure vTaskScheduler() which never returns. You can see that a task is running because it prints a message to that effect and then enters a delay loop. Since both tasks have the same priority, they should alternate, and you should see about the same number of lines printed by each task. We can't see the rest of the code so I don't know what mechanism the scheduler will use to preempt the runnning task.
there is no more detail for API vTaskScheduler() in documentations There is two tasks vTask1 and vTask2 , both tasks have the same priority,

What's difference between task scheduling and task priority's, both are the same for me
 

Papabravo

Joined Feb 24, 2006
21,159
At any time there is one and only one task which is "Running".

The purpose of the function vTaskScheduler() is to allow the "Running" task to execute for some period of time, When the time expires, vTaskScheduler() will preempt the "Running" task and place it in a "Suspended" state. It will then pick the highest priority task from a "Ready To Run" list and make it the "Running" task. That is not all that happens in vTaskScheduler(), but that is the basic function.
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
At any time there is one and only one task which is "Running".

The purpose of the function vTaskScheduler() is to allow the "Running" task to execute for some period of time, When the time expires, vTaskScheduler() will preempt the "Running" task and place it in a "Suspended" state. It will then pick the highest priority task from a "Ready To Run" list and make it the "Running" task. That is not all that happens in vTaskScheduler(), but that is the basic function.
so priority decide which task will run and when it will run and Scheduler decode time for task for example run task A for 100ns , run Task B for 200ns and Run task C for 300ns

priority : Task B first, Task A second and Task C last
 

Papabravo

Joined Feb 24, 2006
21,159
so priority decide which task will run and when it will run and Scheduler decode time for task for example run task A for 100ns , run Task B for 200ns and Run task C for 300ns

priority : Task B first, Task A second and Task C last
There may be other factors involved in the decision, but that is the general idea. Besides priority, you might want to consider how long the task wants to run, and how long it has been since the last time it ran.
 

Thread Starter

rt694157

Joined Dec 15, 2019
78
At any time there is one and only one task which is "Running".

The purpose of the function vTaskScheduler() is to allow the "Running" task to execute for some period of time, When the time expires, vTaskScheduler() will preempt the "Running" task and place it in a "Suspended" state. It will then pick the highest priority task from a "Ready To Run" list and make it the "Running" task. That is not all that happens in vTaskScheduler(), but that is the basic function.
If I want to prioritize the tasks, do I have to pass the priority number in function? as shown passing the priority number in function

Code:
int main( void )
{
   xTaskCreate( vTask1, "Task 1", 1000, NULL, 3, NULL );
   xTaskCreate( vTask2, "Task 2", 1000, NULL, 4, NULL );
   xTaskCreate( vTask1, "Task 3", 1000, NULL, 2, NULL );
   xTaskCreate( vTask2, "Task 4", 1000, NULL, 1, NULL );

   vTaskStartScheduler();
 
    for( ;; );
}
 
Top