Semaphore - freeRTOS

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Semaphore - freeRTOS

2,103 次查看
mjc
Contributor I

Hi. I'm using freeRTOS on the FRDM KL25Z board. How could I use semaphore to manage 2 tasks? Does anyone have any example?

标签 (1)
0 项奖励
回复
3 回复数

1,548 次查看
FreeRTOS_org
Contributor IV

If it is only between two tasks then you can also use a direct to task notification.

0 项奖励
回复

1,548 次查看
BlackNight
NXP Employee
NXP Employee

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 !

0 项奖励
回复

1,548 次查看
mjc
Contributor I

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.

0 项奖励
回复