I'm playing around with RTOS these days. I've set up a small project on a Texas Instruments Hercules LaunchPad to try out tasks and messaging.

 

Don't expect anything complex. This is a minimal setup that allows me to exchange info between RTOS tasks. The simplicity of the example makes it easy to see what's happening.

 

 

The Project

 

 

We'll have an RTOS message queue, and 3 RTOS tasks that communicate via that queue.

freertos rtos message queue and tasks

2 tasks will write info to the queue, each at its own tempo.

1 task will poll the queue constantly, consume every message from that queue and do something with it.

Of course, that something is 'Blink a LED' .

 

 

Porting freeRTOS to the Hercules microcontroller family

 

That's easy. The HALCoGen configuration application can generate a ready-to-compile ported freeRTOS version for you.

When you create a new project, you have the choice to make that a generic project, or a freeRTOS one.
When you select the latter, the RTOS code is generated together with any peripheral driver that you configure in HALCoGen.

 

The Firmware

 

We have to include some header files for RTOS tasks and queues. And we'll define task and queue handler variables.

 

/* Include FreeRTOS scheduler files */
#include "FreeRTOS.h"
#include "os_task.h"
#include "os_queue.h"

// ...

/* Define Task Handles */
xTaskHandle xTask1Handle;
xTaskHandle xTask2Handle;
xTaskHandle xTask3Handle;
// Global Queue Handle
QueueHandle_t qh = 0;




 

 

We also define what each of these tasks do, and what their execution frequency is. You'll see that the two sending tasks #1 and #2 are almost identical

 

/* Task1 */
void vTask1(void *pvParameters)
{
    for(;;)
    {
    xQueueSend(qh, "LED1", 50);
        vTaskDelay(100);
    }
}
/* Task2 */
void vTask2(void *pvParameters)
{
    for(;;)
    {
    xQueueSend(qh, "LED2", 50);
        vTaskDelay(200);
    }
}

/* Task3 */
void vTask3(void *pvParameters)
{
    char led[5];
    for(;;)
    {
        if(xQueueReceive(qh, &led[0], 1000)) { // we don't need to use a vTaskDelay here - we'll sleep 1000 ms if queue is empty

            if (!strcmp(led, "LED1")) {
                gioSetBit(gioPORTB, 6, gioGetBit(gioPORTB, 6) ^ 1);
            }
            if (!strcmp(led, "LED2")) {
                gioSetBit(gioPORTB, 7, gioGetBit(gioPORTB, 7) ^ 1);
            }
       }
    }
}




 

In the main function, we initialise our queue, register the tasks and kick off the RTOS scheduler. That's it. All the rest is magic.

Tasks 1 and 2 will start filling the queue with their messages. Task 3 will poll that queue, and execute its actions every time a value is available on that queue.

 

void main(void)
{
  // ...
  qh = xQueueCreate(5, 5);

    /* Create Task 1 */
    if (xTaskCreate(vTask1,"Task1", configMINIMAL_STACK_SIZE, NULL, 1, &xTask1Handle) != pdTRUE)
    {
        /* Task could not be created */
        while(1);
    }

    /* Create Task 2 */
    if (xTaskCreate(vTask2,"Task2", configMINIMAL_STACK_SIZE, NULL, 1, &xTask2Handle) != pdTRUE)
    {
        /* Task could not be created */
        while(1);
    }

    /* Create Task 3 */
    if (xTaskCreate(vTask3,"Task3", configMINIMAL_STACK_SIZE, NULL, 1, &xTask3Handle) != pdTRUE)
    {
        /* Task could not be created */
        while(1);
    }

    /* Start Scheduler */
    vTaskStartScheduler();

    /* Run forever */
    while(1);
  // ...
}




 

 

The result,as can be seen in the video at the top of this blog, is that the two LEDs flash. Each at its own pace. The LED1 will flash twice as fast as LED2, because Task2 asks to be woken up after a twice as long pause as Task1.

Of course, that's not a scenario where you would involve an RTOS. But an example of how the pieces integrate. You can see and understand how tasks, a scheduler and a queue cooperate.

 

reference and thank you: http://www.socialledge.com/sjsu/index.php?title=FreeRTOS_Tutorial