0% found this document useful (0 votes)
39 views

FreeRTOS Coding Procedure

The document describes the coding procedure for using various synchronization primitives in FreeRTOS including tasks, interrupts, mutexes, semaphores, queues. It involves defining thread handles and functions, creating tasks/primitives, starting the scheduler, and using the primitives for synchronization between tasks through take/give calls.

Uploaded by

Irshan Nazir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

FreeRTOS Coding Procedure

The document describes the coding procedure for using various synchronization primitives in FreeRTOS including tasks, interrupts, mutexes, semaphores, queues. It involves defining thread handles and functions, creating tasks/primitives, starting the scheduler, and using the primitives for synchronization between tasks through take/give calls.

Uploaded by

Irshan Nazir
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

FreeRTOS coding procedure

Task creation
• Step 1: Define thread handle and functions
TaskHandle_t led_on, led_off;
// Define the functions later
static void led_on_function(void* params);
static void led_off_function(void* params);

• Step 2: Create tasks


status = xTaskCreate(led_on_function, "LED On", 200,NULL,2,&led_on);
configASSERT(status == pdPASS);

status = xTaskCreate(led_off_function, "LED Off", 200,NULL,2,&led_off);


configASSERT(status == pdPASS);

• Step 3: Start the scheduler


vTaskStartScheduler();
Interrupt handling
• Step 1: Define thread handle and functions
TaskHandle_t led, button;
// Define the functions later
static void led_function(void* params);
static void button_handler(void* params);

• Step 2: Create tasks


status = xTaskCreate(led_function, "LED", 200, NULL, 2, &led);
configASSERT(status == pdPASS);

status = xTaskCreate(button_handler, "Button", 200,NULL,2,&button);


configASSERT(status == pdPASS);
• Inside button_handler():

static void button_handler(void* parameters)


{
// ...
BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
while(!) {
if(/* Button pressed */)
{
xTaskNotifyFromISR(led,0,eNoAction,&pxHigherPriorityTaskWoken);

portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
printf("Button pressed!\n");
}
// ...
}
• Inside led_function():

static void led_function(void* params)


{
while(1)
{
status = xTaskNotifyWait(0,0,NULL,pdMS_TO_TICKS(1000));
if(status == pdTRUE){
portENTER_CRITICAL();
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin); //LED toggle
portEXIT_CRITICAL();
printf("LED toggled!\n");
}
}
}
Binary semaphore and mutex
• Step 1: Create semaphore handle
#include “semphr.h”
SemaphoreHandle_t myhandle;

• Step 2: Create mutex or binary semaphore

myhandle = xSemaphoreCreateMutex();

vSemaphoreCreateBinary( myhandle );
• Step 3: Create tasks if mutex is successfully created.

if(myhandle != NULL)
{
status = xTaskCreate(task_1, "Task 1", 200,NULL,2,&t1);
configASSERT(status == pdPASS);

status = xTaskCreate(task_2, "Task 2", 200,NULL,2,&t2);


configASSERT(status == pdPASS);

vTaskStartScheduler();
}
• Inside the tasks:

static void task_1(void* params)


{
while(1)
{
xSemaphoreTake(myhandle, portMAX_DELAY);
// ...
xSemaphoreGive(myhandle);
}
}
Counting semaphore

• Step 1: Create semaphore handle


#include “semphr.h”
SemaphoreHandle_t mycountsem;

• Step 2: Create mutex


mycountsem = xSemaphoreCreateCounting(4,0);
• Step 3: Create tasks if counting semaphore is successfully created.
if(mycountsem != NULL)
{
status = xTaskCreate(task_1, "Task 1", 200,NULL,2,&t1);
configASSERT(status == pdPASS);

status = xTaskCreate(task_2, "Task 2", 200,NULL,2,&t2);


configASSERT(status == pdPASS);

status = xTaskCreate(task_3, "Task 3", 200,NULL,2,&t3);


configASSERT(status == pdPASS);

status = xTaskCreate(task_4, "Task 4", 200,NULL,2,&t4);


configASSERT(status == pdPASS);

// ...
vTaskStartScheduler();
}
• Step 4: Give semaphores

xSemaphoreGive(mycountsem);
xSemaphoreGive(mycountsem);
xSemaphoreGive(mycountsem);
xSemaphoreGive(mycountsem);

printf("%s: Semaphore count: %d\n", __func__,


uxSemaphoreGetCount(mycountsem));
• Inside the tasks:

static void task_1(void* params)


{
while(1)
{
xSemaphoreTake(myhandle, portMAX_DELAY);
// ...
xSemaphoreGive(myhandle);
}
}
Queue
• Step 1: Create handle
#include "queue.h“
QueueHandle_t Queue;

• Step 2: Create the queue


Queue = xQueueCreate( 2, sizeof( uint8_t ) );
• Step 3: Create tasks if queue is successfully created
if(Queue != NULL)
{
status = xTaskCreate(writing_task_1, "Task 1", 100, NULL, 1, &t1);
configASSERT(status == pdPASS);

status = xTaskCreate(writing_task_2, "Task 2", 100, NULL, 3, &t2);


configASSERT(status == pdPASS);

status = xTaskCreate(reading_task_1, "Task 3", 100, NULL, 2, &t3);


configASSERT(status == pdPASS);

status = xTaskCreate(reading_task_2, "Task 4", 100, NULL, 4, &t4);


configASSERT(status == pdPASS);

vTaskStartScheduler();
}
• Inside writing task:

void writing_task_1 (void* params)


{
// ...
uint8_t var = 10; // Some sample variable
/* Write to the queue */
status = xQueueSend(Queue, (void*)&var, (TickType_t)10 );
if(status != pdPASS)
printf("Could not add data to queue!\n");
else
printf("Data added to the queue: %d\n", var);
// ...
}
• Inside reading task:

void reading_task_1 (void* params)


{
// ...
uint8_t value;
/* Read from the queue */
status = xQueueReceive(Queue,(void*)&value,(TickType_t)20);
if(status != pdPASS)
printf("Could not read data from the queue!\n");
else
printf("Data read from the queue: %d\n", value);
// ...
}
Further reading
• https://www.freertos.org/a00106.html

You might also like