Skip to content

Instantly share code, notes, and snippets.

@shakram02
Last active February 21, 2021 12:50
Show Gist options
  • Save shakram02/8e6b0306c9028c0ba3c7e98aeb7be877 to your computer and use it in GitHub Desktop.
Save shakram02/8e6b0306c9028c0ba3c7e98aeb7be877 to your computer and use it in GitHub Desktop.
/**
* Adapted from: https://www.freertos.org/RTOS_Task_Notification_As_Binary_Semaphore.html
*/
#include <stdio.h>
#include <pthread.h>
#include "FreeRTOS.h"
#include "semphr.h"
#define TASK_LOOP_DELAY 10
TaskHandle_t task4 = NULL;
void handover_task1(void *_)
{
uint16_t c1 = 0;
while (1)
{
c1++;
vTaskDelay(pdMS_TO_TICKS(TASK_LOOP_DELAY));
}
}
void handover_task2(void *_)
{
uint16_t c2 = 0;
while (1)
{
c2++;
vTaskDelay(pdMS_TO_TICKS(TASK_LOOP_DELAY));
}
}
void handover_task3(void *_)
{
uint16_t c3 = 1;
while (1)
{
c3 = c3 << 1;
printf("C3: %hu\r\n", c3);
if (c3 == 0) // Overflow
{
c3 = 1; // Reset counter
xTaskNotifyGive(task4); // Notify task4 to wake.
}
vTaskDelay(pdMS_TO_TICKS(TASK_LOOP_DELAY));
}
}
void handover_task4(void *_)
{
uint16_t c4 = 0;
while (1)
{
ulTaskNotifyTake(0, portMAX_DELAY);
printf("[4] UP\r\n");
c4++;
vTaskDelay(pdMS_TO_TICKS(TASK_LOOP_DELAY));
}
}
void main_handover()
{
xTaskCreate(handover_task1, "T1", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(handover_task2, "T2", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(handover_task3, "T3", configMINIMAL_STACK_SIZE, NULL, 3, NULL);
xTaskCreate(handover_task4, "T4", configMINIMAL_STACK_SIZE, NULL, 3, &task4);
vTaskStartScheduler();
for (;;)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment