cancel
Showing results for 
Search instead for 
Did you mean: 

in FreeRtos when the semaphore is created it becomes immediately available the osSemaphoreWait function so it is not blocking for the task waiting for the semaphore

Abdelmalek BELLOULA
Associate III
 
2 REPLIES 2
turboscrew
Senior III

xSemaphoreCreateBinary:

"The semaphore is created in the 'empty' state, meaning the semaphore must first be given using the xSemaphoreGive() API function before it can subsequently be taken (obtained) using the xSemaphoreTake() function. "

Abdelmalek BELLOULA
Associate III

Hello and thank you for your answer

I may have misunderstood my question

Task 2 is supposed to be blocked until the semaphore is released by task 1 after the 2-second osDelay

I fixed the problem by adding just after the creation of the semaphore the function: xSemaphoreTake (BinarySEMHandle, 1).

On the other hand, if I remove this function the second task is directly executed and the function osSemaphoreWait (BinarySEMHandle, osWaitForever); is not blocking which for me confirms that the function BinarySEMHandle = osSemaphoreCreate (osSemaphore (BinarySEM), 1); by default, returns the semaphore available from its creation by this function

This also applies to the semaphore counter, I added two functions to block the third task will be synchronized by the Tasks 1 and 2.

 /* definition and creation of BinarySEM */

  osSemaphoreDef(BinarySEM);

  BinarySEMHandle = osSemaphoreCreate(osSemaphore(BinarySEM), 1);

xSemaphoreTake(BinarySEMHandle, 1);

/* definition and creation of myCountingSem01 */

  osSemaphoreDef(myCountingSem01);

  myCountingSem01Handle = osSemaphoreCreate(osSemaphore(myCountingSem01), 2);

xSemaphoreTake(myCountingSem01Handle, 1);

xSemaphoreTake(myCountingSem01Handle, 1);

void SemTache1(void const * argument)

{

  /* USER CODE BEGIN SemTache1 */

  /* Infinite loop */

  for(;;) {

    for(int i=0; i<10; i++) {

      HAL_GPIO_TogglePin(GPIOB, LED1_Pin);

      osDelay(80);

    }

    HAL_GPIO_WritePin(GPIOB, LED1_Pin, GPIO_PIN_SET);

    osDelay(2000);

    osSemaphoreRelease(BinarySEMHandle);

    //osThreadTerminate(SemTache1);

  }

  /* USER CODE END SemTache1 */

}

void SemTache2(void const * argument)

{

  /* USER CODE BEGIN SemTache2 */

  /* Infinite loop */

  for(;;) {

    osSemaphoreWait(BinarySEMHandle, osWaitForever);

    for(int i=0; i<10; i++) {

      HAL_GPIO_TogglePin(GPIOB, LED2_Pin);

      osDelay(80);

    }

    HAL_GPIO_WritePin(GPIOB, LED2_Pin, GPIO_PIN_SET);

    osDelay(2000);

    osSemaphoreRelease(myCountingSem01Handle);

  }

  /* USER CODE END SemTache2 */

}

void SemTache3(void const * argument)

{

  /* USER CODE BEGIN SemTache3 */

  /* Infinite loop */

  for(;;) {

    osSemaphoreWait(myCountingSem01Handle, osWaitForever);

    osSemaphoreWait(myCountingSem01Handle, osWaitForever);

    for(int i=0; i<10; i++) {

      HAL_GPIO_TogglePin(GPIOB, LED3_Pin);

      osDelay(80);

    }

    HAL_GPIO_WritePin(GPIOB, LED3_Pin, GPIO_PIN_SET);

  }

  /* USER CODE END SemTache3 */

}