As I recall (can’t check just now as on my phone) xSemaphoreCreateBinary() creates the semaphore with vale 0, whereas the older vSemaphoreCreateBinary() creates it with value 1.
Do we need to compile FreeRTOS with config SUPPORT_DYNAMIC_ALLOCATION as 1 - vSemaphoreCreateBinary requires this flag to set as 1? Is there an alternative to create statically i.e. not using heap?
Suppose we want to reset the value of semaphore created to value a ‘startValue’ which can be either 0 or 1 - will the below code work:
BaseType_t status = xQueueReset((QueueHandle_t)SemaphoreHandle); //Reset to empty state
assert(status == pdTRUE);
U32 index;
for(index = 0; index <= startValue; index++)
{
status = xSemaphoreGive(SemaphoreHandle);
assert(status == pdTRUE);
}
In above code whether we have created Semaphore using xSemaphoreCreateBinary or vSemaphoreCreateBinary we desire to reset its value and then make its curret value to as startValue?
/*
* This old vSemaphoreCreateBinary() macro is now deprecated in
* favour of the xSemaphoreCreateBinary() function. Note that
* binary semaphores created using the vSemaphoreCreateBinary()
* macro are created in a state such that the first call to 'take'
* the semaphore would pass, whereas binary semaphores created
* using xSemaphoreCreateBinary() are created in a state such
* that the the semaphore must first be 'given' before it can
* be 'taken'.
*/
Although a semaphore “actually” is a Queue, I would not use any direct Queue API to change something in a semaphore or mutex. It might work, it might not.
These 2 functions will clear and set the semaphore:
xSemaphoreTake( xSem, 0U ); // Clear the semaphore, non-blocking
xSemaphoreGive( xSem); // Set (release) the semaphore
In general, ALL the “Create” functions need SUPPORT_DYNAMIC_ALLOCATION to be 1, and there will be an equivalent “CreateStatic” where you supply the memory to be used instead which requires the SUPPORT_STATIC_ALLOCATION to be 1.