-1

I have read in link, This xTaskCreate FreeRTOS API is used to create a task. Using this API we can create more number of tasks:

/* Task to be created. */


/* Task to be created. */
void vTaskCode( void * pvParameters )
{
    /* The parameter value is expected to be 1 as 1 is passed in the
    pvParameters value in the call to xTaskCreate() below. 
    configASSERT( ( ( uint32_t ) pvParameters ) == 1 );

    for( ;; )
    {
        /* Task code goes here. */
    }
}

I have seen this program example. I want to create two tasks. First task blink led1, second task blink led2.

I don't understand how to write program for two tasks.

herrbischoff
  • 3,294
  • 1
  • 29
  • 50
abhimanyu143
  • 11
  • 1
  • 2
  • 4

1 Answers1

3

to create the second task just call xTaskCreate twice, like below:

void vTaskLedGreen( void * pvParameters )
{
    /* The parameter value is expected to be 1 as 1 is passed in the
    pvParameters value in the call to xTaskCreate() below. 
    configASSERT( ( ( uint32_t ) pvParameters ) == 1 );

    for( ;; )
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
        GreenLedOff();
        vTaskDelay(pdMS_TO_TICKS(1000));
        GreenLedOn();
    }
}

void vTaskLedRed( void * pvParameters )
{
    /* The parameter value is expected to be 1 as 1 is passed in the
    pvParameters value in the call to xTaskCreate() below. 
    configASSERT( ( ( uint32_t ) pvParameters ) == 1 );

    for( ;; )
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
        RedLedOff();
        vTaskDelay(pdMS_TO_TICKS(1000));
        RedLedOn();
    }
}

/* Function that creates a task. */
void main( void )
{
BaseType_t xReturned;
TaskHandle_t xHandle = NULL;

    xReturned = xTaskCreate(
                    vTaskLedRed,       /* Function that implements the task. */
                    "RedLed",          /* Text name for the task. */
                    STACK_SIZE,      /* Stack size in words, not bytes. */
                    ( void * ) 1,    /* Parameter passed into the task. */
                    tskIDLE_PRIORITY,/* Priority at which the task is created. */
                    &xHandle );      /* Used to pass out the created task's handle. */

    if( xReturned != pdPASS )
    {
         ErrorHandler();
    }

    xReturned = xTaskCreate(
                    vTaskLedGreen,       /* Function that implements the task. */
                    "GreenLed",          /* Text name for the task. */
                    STACK_SIZE,      /* Stack size in words, not bytes. */
                    ( void * ) 1,    /* Parameter passed into the task. */
                    tskIDLE_PRIORITY,/* Priority at which the task is created. */
                    &xHandle );      /* Used to pass out the created task's handle. */

    if( xReturned != pdPASS )
    {
         ErrorHandler();
    }

    // Start the real time scheduler.
    vTaskStartScheduler();
}
denis krasutski
  • 618
  • 4
  • 9