Skip to content

Instantly share code, notes, and snippets.

@shakram02
Created February 21, 2021 11:39
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/65e742d92e5cda4a9150c60253778a33 to your computer and use it in GitHub Desktop.
Save shakram02/65e742d92e5cda4a9150c60253778a33 to your computer and use it in GitHub Desktop.
FreeRTOS Semaphore API
/**
* Adapted from: https://microcontrollerslab.com/freertos-binary-semaphore-tasks-interrupt-synchronization-u-arduino/
*/
#include <stdio.h>
#include <pthread.h>
#include "FreeRTOS.h"
#include "semphr.h"
SemaphoreHandle_t semaphore;
void semaphore_task1(void *_)
{
while (1)
{
xSemaphoreTake(semaphore, portMAX_DELAY);
printf("[1] Take... \r\n");
xSemaphoreGive(semaphore);
vTaskDelay(1);
}
}
void semaphore_task2(void *_)
{
while (1)
{
xSemaphoreTake(semaphore, portMAX_DELAY);
printf("[2] Take... \r\n");
xSemaphoreGive(semaphore);
vTaskDelay(1);
}
}
void main_semaphore()
{
semaphore = xSemaphoreCreateBinary();
xSemaphoreGive(semaphore);
if (!semaphore)
{
printf("Failed to obtain sempahore.\r\n");
}
xTaskCreate(semaphore_task1, "Take", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
xTaskCreate(semaphore_task2, "Give", configMINIMAL_STACK_SIZE, NULL, 1, NULL);
vTaskStartScheduler();
for (;;)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment