FreeRTOS Lecture 29 – Exercise: Creating FreeRTOS Tasks Part-2

 

Exercise: Creating FreeRTOS Tasks Part-2

 

 

After creating a task using xTaskCreate, you may observe lots of red lines, as shown below.

This is because before using any FreeRTOS related header file, that means the header file related to different services like tasks, queues, semaphores, mutexes, etc. always include the main header file, i.e., FreeRTOS.h, which is there in the 3rd party FreeRTOS source include shown in Figure 1.

This is a very important header file that must be included first.

int main(void)
{
    //1. Reset the RCC clock configuration to the default reset state.
    //HSI ON, PLL OFF, HSE OFF, system clock = 16MHz, cpu_clock = 16MHz
    RCC_DeInit();

    //2. update the SystemCoreClock variable
    SystemCoreClockUpdate();

    //3. lets create 2 tasks , task-1 and task-2
    xTaskCreate( vTask1_handler,"Task-1", configMINIMAL_STACK_SIZE,NULL,2,&xTaskHandle1 );

    for(;;);
}

Creation of task 1 by using xTaskCreate() API

 

Including the FreeRTOS header file into the project
Figure 1. Including the FreeRTOS header file into the project.

 

Once you include the header file, you can see that most of the errors will vanish. There are some other errors that we will address later. Now let’s create another task. Before that, let’s implement the vTask1_handler or task function. The prototype of the task function is as shown in Figure 2. Now let’s implement the task function for Task1 as follows:

Prototype for task function
Figure 2. Prototype for task function.

 

  • The vTask1_handler takes a void pointer. Let’s give the parameter name as params, and since this function returns void, mention the return type as void. The handler for Task 1 is shown in Figure 3.
Prototype of vTask1_handler, xTaskCreate
Figure 3. Prototype of vTask1_handler.

 

Now let’s create another task, i.e., Task 2. Just copy and paste the statement you wrote to create Task1. Change the handle name, task name, and address of the task handle to vTask2_handler, Task-2, and &xTaskHandle2, respectively, as shown below. Keep the remaining information as it is.

int main(void)
{
   //1. Reset the RCC clock configuration to the default reset state.
   //HSI ON, PLL OFF, HSE OFF, system clock = 16MHz, cpu_clock = 16MHz
   RCC_DeInit();

  //2. update the SystemCoreClock variable
  SystemCoreClockUpdate();

  //3. lets create 2 tasks , task-1 and task-2
  xTaskCreate( vTask1_handler,"Task-1", configMINIMAL_STACK_SIZE,NULL,2,&xTaskHandle1 );
  xTaskCreate( vTask2_handler,"Task-2", configMINIMAL_STACK_SIZE,NULL,2,&xTaskHandle2 );

  for(;;);
}

Creation of task 2 by using xTaskCreate() API

 

Again, implement the task function for task2 and call it vTask2_handler, as shown in Figure 4.

Prototype of vTask2_handler, xTaskCreate
Figure 4. Prototype of vTask2_handler.

 

Give the prototype of Task 1 handler and Task 2 handler in main.c.

#include "stm32f4xx.h"

#include "FreeRTOS.h"
#include "task.h"

TaskHandle_t xTaskHandle1=NULL;
TaskHandle_t xTaskHandle2=NULL;

//Task functions prototypes
void vTask1_handler(void *params);
void vTask2_handler(void *params);

int main(void)
{

Prototype of Task 1 and Task 2 in main.c

 

Now let’s try to build this code (Figure 5), and let’s see how this goes.

Building the project, xTaskCreate
Figure 5. Building the project.

 

In Figure 6, you can see that the project was built successfully without any errors.

Result of compilation, xTaskCreate
Figure 6. Result of compilation.

 

Summary:

Here we created two tasks. The name of the first task is Task 1, and the name of the second task is Task 2. When Task 1 runs, it actually executes vTask1_handler, and when Task 2 runs, it executes vTask2_handler. Task 1 consumes configMINIMAL_STACK_SIZE words of the stack in the RAM of the microcontroller, and Task 2 also consumes the same amount of the stack memory on the RAM of the microcontroller. Both the tasks have equal priorities, i.e., 2. &xTaskHandle1 and &xTaskHandle2 are the task handles in order to handle the task we created.

After that, vTask1_handler and vTask2_handler are the task handlers. Remember that, in FreeRTOS, the task handler should never return since they will always be running. That’s why let’s keep while(1) in the task handlers, as shown in Figure 7.

Creating FreeRTOS Tasks Part-2, xTaskCreate
Figure 7. Placing infinite loops inside the task handlers.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.