A newer version of this document is available. Customers should click here to go to the newest version.
13.3.3. Write FreeRTOS-based Source Codes
The example below shows a simple Hello World application from any “Hello World on Nios V Processor Design Example” in FPGA Design Store.
This simple Hello World creates three tasks that print individual Hello World messages, followed by an idle period using xTaskDelay(). Each task has a defined priority that FreeRTOS refers to. If two or more tasks are out of idle simultaneously, the task with the highest priority is executed first.
A mutex (g_mutex) provides exclusive access to the whole processor system for a single task. The running task claims the mutex using xSemaphorTake() and releases it using xSemaphoreGive() after it completes its operation. Without claiming the mutex, a task waits till a maximum period is reached.
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "semphr.h"
#define TASK_STACKSIZE 2048
/* Task priority setting : Task1 > Task2 > Task3 */
#define TASK1_PRIORITY (tskIDLE_PRIORITY + 3)
#define TASK2_PRIORITY (tskIDLE_PRIORITY + 2)
#define TASK3_PRIORITY (tskIDLE_PRIORITY + 1)
SemaphoreHandle_t g_mutex;
#define NUM_PRINTS_PER_TASK 5
int t1_ms_slept = 750 / portTICK_PERIOD_MS;
int t2_ms_slept = 450 / portTICK_PERIOD_MS;
int t3_ms_slept = 250 / portTICK_PERIOD_MS;
void prvTask1(void* pvParameters)
{
int i = 0;
int ms_slept = (*((int *)pvParameters));
while (1)
{
xSemaphoreTake(g_mutex,portMAX_DELAY);
if (i < NUM_PRINTS_PER_TASK) {
printf("Hello from task1: %d\n\n", i++);
}
xSemaphoreGive(g_mutex);
vTaskDelay(ms_slept);
}
}
void prvTask2(void* pvParameters)
{
int i = 0;
int ms_slept = (*((int *)pvParameters));
while (1)
{
xSemaphoreTake(g_mutex,portMAX_DELAY);
if (i < NUM_PRINTS_PER_TASK) {
printf("Hello from task2: %d\n\n", i++);
}
xSemaphoreGive(g_mutex);
vTaskDelay(ms_slept);
}
}
void prvTask3(void* pvParameters)
{
int i = 0;
int ms_slept = (*((int *)pvParameters));
while (1)
{
xSemaphoreTake(g_mutex,portMAX_DELAY);
if (i < NUM_PRINTS_PER_TASK) {
printf("Hello from task3: %d\n\n", i++);
}
xSemaphoreGive(g_mutex);
vTaskDelay(ms_slept);
}
}
int main(void)
{
printf("Hello FreeRTOS from main...\n");
g_mutex = xSemaphoreCreateMutex();
if (pdFAIL == xTaskCreate( prvTask1, "Task1", TASK_STACKSIZE,
&t1_ms_slept, TASK1_PRIORITY, NULL )){
printf("Task1 creation fail!!!!\n");
}
if (pdFAIL == xTaskCreate( prvTask2, "Task2", TASK_STACKSIZE,
&t2_ms_slept, TASK2_PRIORITY, NULL )){
printf("Task2 creation fail!!!!\n");
}
if (pdFAIL == xTaskCreate( prvTask3, "Task3", TASK_STACKSIZE,
&t3_ms_slept, TASK3_PRIORITY, NULL )){
printf("Task3 creation fail!!!!\n");
}
vTaskStartScheduler();
for( ;; );
}