Created
February 21, 2021 11:39
-
-
Save shakram02/65e742d92e5cda4a9150c60253778a33 to your computer and use it in GitHub Desktop.
FreeRTOS Semaphore API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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