-3

I want to create two tasks that run simultaneously in FreeRTOS. The first task will deal with the LED, the second task will monitor the temperature. I have two questions:

  1. Will this code create two tasks that run simultaneously?
  2. How do I send the data between the tasks, for example: if the temperature is more than x degrees, turn on the LED?

    void firstTask(void *pvParameters) {
        while (1) {
            puts("firstTask");
        }
    }
    
    void secondTask(void *pvParameters) {
        while (1) {
            puts("secondTask");
        }
    }
    
    int main() {
        xTaskCreate(firstTask, "firstTask", STACK_SIZE, NULL, TASK_PRIORITY, NULL);
        xTaskCreate(secondTask, "secondTask", STACK_SIZE, NULL, TASK_PRIORITY, NULL);
        vTaskStartScheduler();
    }
    
Mathew
  • 1
  • 1

2 Answers2

2

Tasks of equal priority are round-robin scheduled. This means that firstTask will run continuously until the end of its time-slice or until it is blocked, then secondTask will run for a complete timeslice or until it is blocked then back to firstTask repeating indefinitely.

On the face of it you have no blocking calls, but it is possible that if you have implemented RTOS aware buffered I/O for stdio, then puts() may well be blocking when its buffer is full.

The tasks on a single core processor are never truly concurrent, but are scheduled to run as necessary depending on the scheduling algorithm. FreeRTOS is a priority-based preemptive scheduler.

Your example may or not behave as you intend, but both tasks will get CPU time and run in some fashion. It is probably largely academic as this is not a very practical or useful use of an RTOS.

Clifford
  • 88,407
  • 13
  • 85
  • 165
1

Tasks never really run simultaneously - assuming you only have one core. In you case you are creating the tasks with the same priority and they never block (although they do output strings, probably in a way that is not thread safe), so they will 'share' CPU time by time slicing. Each task will execute up to the next tick interrupt, at which point it will switch to the other.

I recommend reading the free pdf version of the FreeRTOS book for a gentle introduction into the basics https://www.freertos.org/Documentation/RTOS_book.html

Richard
  • 3,081
  • 11
  • 9