0% found this document useful (0 votes)
17 views3 pages

Lab 3

Uploaded by

Muqdas Satti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Lab 3

Uploaded by

Muqdas Satti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EE-434 Real-Time Embedded Systems

Lab 3 Manual
Fall 2024

Lab Engineers:

Muhammad Uzair Arshad

Instructor: Dr. Naeem Iqbal


USING THE TASK PARAMETER
1.1. Objectives
This lab will teach you how to create tasks in FreeRTOS with parameters on the Arduino Due, which
has only one onboard LED. You will pass arguments to tasks and use serial printing to observe task
behavior. By the end of the lab, you will have created multiple tasks using parameters for different
serial outputs.

1.2. Introduction
In FreeRTOS, when you create tasks using xTaskCreate(), there is an option to pass a parameter (via the
pvParameters argument) to the task function. This parameter is of type void*—a void pointer.
Understanding how a void pointer works and how to properly cast and dereference it is essential when
passing data between tasks.
What is a Void Pointer?
A void pointer is a type of pointer that can point to any data type. It is often used when you don't know
the exact type of data you're pointing to, or when you want to create a function (like a task) that is flexible
enough to accept different types of data. A void pointer is a generic pointer that must be cast to the
correct data type before it can be dereferenced.
In the context of FreeRTOS tasks:
• The pvParameters argument is of type void*, meaning it can accept any data type (integers, arrays,
structures, etc.).
• To use this parameter in a task, it must first be cast to the appropriate type (since you cannot
directly dereference a void pointer).
• Once cast, you can dereference the pointer to access or modify the data.
Casting and Dereferencing in Tasks
Let's take a deeper look into how this works in practice. Suppose you want to pass an integer to a task
function:
void myTask(void *pvParameters) {
int myVar = *((int*)pvParameters); // Cast and dereference

while(1) {
// Now you can use myVar in the task
}
}
1. Casting the Void Pointer:
(int*) pvParameters
This part converts the pvParameters from a void* to an int*. This tells the compiler that the pointer you
received is actually pointing to an integer.
2. Rereferencing the Pointer:
*((int*) pvParameters)
The dereferencing step * fetches the value that the pointer is pointing to. The data type of the pointer (int
in this case) represents how many bytes will be fetched from the given address pointed by the pointer.
For Example if the type of the pointer is integer it will fetch 4 bytes from the memory at address pointed
by the pointer pvParamenter. The value is stored in the variable myVar so it can be used in the task.
Note: When a task parameter is a string (a character array or char*), it behaves differently from other
data types. A string is already a pointer to characters that end with a null character, so it doesn’t require
dereferencing like an integer or structure. Instead, you can use the string pointer directly in functions
such as Serial.print().
Sample Code:
#include <Arduino_FreeRTOS.h>
#include <task.h>
void taskFunction(void *parameter)
{
int myVar = *((int*) parameter);
while(1)
{
// Use myVar as required
}
}
void setup() {
int x = 10;
xTaskCreate(taskFunction, "myTask", 128, (void*)&x, 1, NULL);
vTaskStartScheduler();
}

void loop() {

1.3. Lab Tasks:


1. Task1:
Make three task to print name of each student of the group by using a single task function with
name of the student as a task parameter.
2. Task 2:
Modify the above code so that each task prints the name with different time periods (use
vTaskDelayUntil() for execution of a task at fixed time period), the time period should also be
passed to the task as a parameter i.e. there should be two parameters passed to each task “Student’s
name” and “Task time period (an integer)”. You are required to use only one task function.
Hint: Use a Struct as a paramerter.

You might also like