freeRTOS port to RH850

Here I have also checked the return type of xSemaphoreGive , it is not returning pdTRUE

That should not happen and would explain the behavior you are seeing. Can you step though the function and see why it is not succeeding?

#define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )

As per the defination xSemaphoreGive is calling xQueueGenericSend in background and inside this function it return from this below if loop


            if( xTicksToWait == ( TickType_t ) 0 )
            {
                /* The queue was full and no block time is specified (or
                 * the block time has expired) so leave now. */
                taskEXIT_CRITICAL();

                /* Return to the original privilege level before exiting
                 * the function. */
                traceQUEUE_SEND_FAILED( pxQueue );
                return errQUEUE_FULL;
            }

It is returning errQUEUE_FULL from xsemaphoregive

You can check this image i have put the validation before xSemaphoreGive so that is fullfilling but still it stucks in while loop after coming out from xSemaphoreTake.

As xSemaphoreTake will not work because in when we execute xSemaphoreGive this itself not return pdTrue

Can you share the code where you create semaphore? Does any other part of your application use the same semaphore?

Please find below code in which inside main function after init all peripherals i am creating a semaphore

void main(void)
{
    r_main_userinit();
    /* Start user code for main. Do not edit comment generated here */

    Init_Peripherals();

    /* Create a semaphore for display delay */
    sem1 = xSemaphoreCreateBinary();

	xTaskCreate( Task1, "Task1", configMINIMAL_STACK_SIZE * 2, (void*)0, 1,(xTaskHandle*)&xTask1);
	xTaskCreate( Task2, "Task2", configMINIMAL_STACK_SIZE * 2, (void*)0, 2,(xTaskHandle*)&xTask2);
	xTaskCreate( Task3,  "Task3", configMINIMAL_STACK_SIZE * 2 , (void*)0, 3,(xTaskHandle*)&Task3 );

	vTaskStartScheduler();

	for( ;; )
	{
	}
    /* End user code. Do not edit comment generated here */
}

void Task1(void) 
{
	/* After some variable initialization */
	
   if (uxSemaphoreGetCount(sem1) == 0) {
       if(xSemaphoreGive(sem1) != pdTRUE) {
		while(1);
	   }
	}
	 if (xSemaphoreTake(sem1, pdMS_TO_TICKS(500)) != pdTRUE) {
		 while(1);
	 }
}

here my code stucks inside the while loop at xSemaphoreGIve only.

That’s broken.
If the task get’s preempted after checking the semaphore count and before signaling the semaphore and the other now running task signals this semaphore … when the original task runs again and tries to signal (the meanwhile signaled) semaphore, you run into troubles.
The trouble also applies to counting semaphores or similar mechanisms.

Can you please provide to correct sequence of execution so that semaphore works fine

There is not really a correct sequence. You simply have to use it right resp. as your application requires. This could mean for example signaling a binary semaphore twice could be a programming error or not. It depends on your application design.

However, if you really want to deal with count dependent signaling of a semaphore (I wouldn’t recommend) you might enclose it by a critical section to avoid a (count) race condition as explained before:

   taskENTER_CRITICAL();
   if (uxSemaphoreGetCount(sem1) == 0) {
       if(xSemaphoreGive(sem1) != pdTRUE) {
		while(1);
	   }
	}
    taskEXIT_CRITICAL();

After using taskCriticalEnter and Exit functions still it is not working

Unfortunately again that can’t work … you can’t use possibly blocking xSemaphoreTake inside a critical section.
A critical section is for small pieces of non-blocking code to run it exclusively without preemption means atomically.
The part which must run atomically in your example is just the part I posted. The count dependent semaphore signaling (I wouldn’t recommend to do so).
Also these things are completely unrelated to the topic of this posting. Let’s stop this discussion here.

But Can you help me why Semaphore give and take is not working ?

Is there any configurations or something i am missing can you please help in this?

I likely source of the SemaphoreGive failing is that the Semaphore was already in the given state. In fact, I think that is the only reason it can “fail”. Note, depending on which function you use to create the Semaphore, it can be created in the empty or full state, so you need to be carefull about how you create it, or adjust that value right after making it.

@keyur_khatri is using xSemaphoreCreateBinary to create the semaphore, so the call to xSemaphoreGive should succeed. Does any of Task2 to Task3 uses this semaphore? Can you share the complete code?