Skip to content

Instantly share code, notes, and snippets.

@shakram02
Created January 30, 2021 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shakram02/bacf931e3dfccdb0bd1ab6b1a2d4fba6 to your computer and use it in GitHub Desktop.
Save shakram02/bacf931e3dfccdb0bd1ab6b1a2d4fba6 to your computer and use it in GitHub Desktop.
This code demonstrates using RTOS tasks with different priorities
#include <stdio.h>
#include <pthread.h>
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "semphr.h"
void task1()
{
while (1)
{
printf("\033[0;36mEnter task1\033[0m\r\n");
xSemaphoreTake(xSemaphore, (TickType_t)0);
TickType_t xLastWakeTime = xTaskGetTickCount();
printf("\033[0;36m[%05d] Task 1: Start (p:%d)\033[0m\r\n", xLastWakeTime, mainTASK_ONE_PRIORITY);
fflush(stdout);
for (size_t i = 0; i < 1800000000; i++)
{
__asm__("nop");
}
xLastWakeTime = xTaskGetTickCount();
printf("\033[1;36m[%05d] Task 1: Done\033[0m\r\n", xLastWakeTime);
vTaskDelay(pdMS_TO_TICKS(200));
}
}
void task2()
{
while (1)
{
printf("\033[0;36mEnter task2\033[0m\r\n");
xSemaphoreTake(xSemaphore, (TickType_t)0);
TickType_t xLastWakeTime = xTaskGetTickCount();
printf("\033[0;32m[%05d] Task 2: Semaphore Taken (p:%d)\033[0m\r\n", xLastWakeTime, mainTASK_TWO_PRIORITY);
fflush(stdout);
for (size_t i = 0; i < 15000000; i++)
{
__asm__("nop");
}
xLastWakeTime = xTaskGetTickCount();
printf("\033[1;32m[%05d] Task 2: Done\033[0m\r\n", xLastWakeTime);
fflush(stdout);
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
/*** SEE THE COMMENTS AT THE TOP OF THIS FILE ***/
void main_blinky(void)
{
xSemaphore = xSemaphoreCreateBinary();
xTaskCreate(
task1,
"Task1",
configMINIMAL_STACK_SIZE,
NULL,
mainTASK_ONE_PRIORITY,
NULL);
xTaskCreate(
task2,
"Task2",
configMINIMAL_STACK_SIZE,
NULL,
mainTASK_TWO_PRIORITY,
NULL);
vTaskStartScheduler();
/* If all is well, the scheduler will now be running, and the following
line will never be reached. If the following line does execute, then
there was insufficient FreeRTOS heap memory available for the idle and/or
timer tasks to be created. See the memory management section on the
FreeRTOS web site for more details. */
for (;;)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment