Lab 3
Lab 3
Lab 3 Manual
Fall 2024
Lab Engineers:
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() {