Hi. I'm using freeRTOS on the FRDM KL25Z board. How could I use semaphore to manage 2 tasks? Does anyone have any example?
If it is only between two tasks then you can also use a direct to task notification.
Hi Murilo,
not sure what you mean with 'manage 2 tasks' with a semaphore?
Have a look at the API and examples here: FreeRTOS semaphore and mutex API functions vSemaphoreCreateBinary, xSemaphoreCreateCounting, xSemaph...
Other then that: semaphore are used more synchronization, and you can use it either for message passing or for locking/unlocking.
I have here an example of using semaphores for task execution control:
tatic void vSlaveTask(void *pvParameters) {
xSemaphoreHandle sem;
sem = (xSemaphoreHandle)pvParameters;
for(;;) {
if (sem != NULL) {
if (xSemaphoreTake(sem, portMAX_DELAY)==pdTRUE) {
LED1_Neg();
}
}
}
}
static void vMasterTask(void *pvParameters) {
/*! \todo Implement functionality */
xSemaphoreHandle sem = NULL;
(void)pvParameters; /* parameter not used */
sem = xSemaphoreCreateBinary();
if (sem==NULL) { /* semaphore creation failed */
for(;;){} /* error */
}
vQueueAddToRegistry(sem, "IPC_Sem");
/* create slave task */
if (xTaskCreate(vSlaveTask, "Slave", configMINIMAL_STACK_SIZE, sem, tskIDLE_PRIORITY+1, NULL) != pdPASS) {
for(;;){} /* error */
}
for(;;) {
if (sem != NULL) { /* valid semaphore? */
(void)xSemaphoreGive(sem); /* give control to other task */
vTaskDelay(1000/portTICK_PERIOD_MS);
}
}
}
I hope this helps,
Erich !
Hi Erich. Thanks for the help. I was forgetting to put a delay after of "give the semaphore", this way the same task took the semaphore again, and my application was "locked" in a only task.