Featured post

Arduino FreeRTOS Mutex Examples

In this section, we will explain using an example to demonstrate mutexes in Arduino using freeRTOSThere are two freeRTOS Mutex examples in this tutorial, the first example demands some hardware (LCD) While the second does not need any hardware, you can try out both if you have the resources.

In the last tutorial, we considered in detail: semaphores and mutexes and we also established the difference between binary semaphores and mutexes.
Just for a review:Recall that a mutex is a locking mechanism that implements the take and gives functionality in itself, unlike a binary semaphore. See this tutorial if you have not before continuing.
Example 1: Protecting the LCD Resource Using Mutex in freeRTOS
Program Description

In this program, we Demonstrated the use a 16x2 LCD display to implement a mutex.

The LiquidCrystal library works with all LCD displays that are compatible with the Hitachi HD44780 driver. you can usually tell them by their 16-pin interface.

THE CIRCUIT

* LCD RS pin to digital…

Arduino FreeRTOS Beginner Tutorial: Task Creation, Scheduling, Deleting

Tasks are basically functions that you want to execute. A task can be as simple as blinking a led, printing a string on an LCD.

Your program or embedded application is a combination of various tasks.

In the previous tutorial, we showed how to install freeRTOS in Arduino and we have also covered a quick introduction to RTOS.

In this tutorial we will be considering the following:

  • How to create tasks

  • Prioritizing tasks

  • How to delete tasks

  • How to run the scheduler

Features of tasks in freeRTOS.


In free RTOS, tasks have some features that you must adhere to when creating them.

  1. It must not have an end. There should be no terminating statement (return statement)

  2. A task should run continuously. To implement this, you will need to use an infinite loop in the tasks

  3. Tasks have priorities. The priority of a task determines its pre-emptive nature. A task of higher priority can pre-empt one of lower priority. This is useful to enable you to achieve your hard and soft real-time requirements.

How to Create Tasks.


To create a task, you make use of the API or function.

xTaskCreate(name of task, task label, size, parameter, priority, handle);

The function above takes five parameters in the order below.

  1. Name of the task, i.e. the function name.

  2. Task Label. Just a short label for the task to enable you to remember what it was created for if the name is not descriptive enough.

  3. Memory/Stack Size: Numeric value that assigns memory to the task. Recall in our introduction to RTOS, we specified that one of the functions of the real-time kernel is memory management. This assignment specifies one of the ways to implement it in freeRTOS.

  4. Task Parameters: Just as functions take in parameters to perform their function, tasks also can take parameters or values that it would use to execute.

  5. Priority: The prioritization of tasks is relative, assigning of priorities to tasks as you would see later on affects its execution and other scheduling parameters. It is always recommended that you assign higher priorities to tasks with hard real-time requirements.

  6. Task Handle: This is the name that you would use to address the task. In performing some functions like deleting. You would use the name you put here.

Creating a simple task function



  • First, create the function that you would use to implement the task.

Void TaskBlink(void *pvParameters){

//this function blinks an Led connected to pin 13 of the Arduino

For(; ;) {//implementation of infinite loop

if(digitalRead(13)==HIGH){

digitalWrite(13,LOW);

}else{

digitalWrite(13, HIGH);

}

vTaskDelay(200/portTICK_Period_MS);

 

}

}

freeRTOS API for creating task:


xTaskCreate(TaskBlink,”Blinking Task”,200,null,1,null);

At this time, the task has been created.

Task Scheduling: How to start execution.


To begin task execution, you use the API
vTaskStartScheduler();

It takes no arguments. This API hands over control to the kernel. Once this API is met, no other code below it can run unless there is a problem. In task scheduling, there are two methods supported by freeRTOS. Cooperative and Preemptive. We are making use of pre-emptive scheduling for the tutorial series.

Ok, before proceeding to task deletion, let us run the program above.

Putting it all Together


Running the codes above will show no response. To actually run the code, you need to add the source files . This we have implemented below.
#include <Arduino_FreeRTOS.h>

//Lets first create the blinkingTask
* Blinking Task*/
void BlinkingTask(void* pvParameters)
{
for(;;)
{
if(digitalRead(13) == HIGH){
digitalWrite(13, LOW);
}else{
digitalWrite(13,HIGH);
}
vTaskDelay(100/portTICK_PERIOD_MS);//explanation found below
}
}
void setup()
{
pinMode(13,OUTPUT);// SETS THE DIGITAL PIN 13 AS OUTPUT

xTaskCreate(BlinkingTask, "MY Blinking Task", 100, NULL, 1, NULL);
vTaskStartScheduler(); // hands over control to kernel. Task execution begins

}
void loop(){
//leave empty
}

vTaskDelay(100/portTICK_PERIOD_MS): The vTaskDelay is analogous to the delay() function in Arduino. However, it is different in that delay() takes time as an argument in milliseconds, but this"vTaskDelay()" takes time in ticks. Where one tick may not be one millisecond. It is dependent on the microcontroller. To account for the lapses, you divide the time by the constant portTICK_PERIOD_MS. it is a macro that enables you to get the accurate time in milliseconds.

Deleting a Task


You have seen in this tutorial, that tasks are continuously running and should never be terminated. However, in situations where you no longer need a task to run, you can delete it from the program.

To delete a task, you make use of the API.

Comments

Popular posts from this blog

Arduino FreeRTOS Tutorial 05: Binary Semaphores and Mutexes

Digital Systems and Stability Analysis using Jury's Stability Test