I started working recently with FreeRTOS, and while trying to implement a binary semaphore, I was taking a look at your website for an explanation on how to use it.
In the webpage http://www.freertos.org/xSemaphoreCreateBinary.html it is said that “The semaphore is created in the 'empty' state, meaning the semaphore must first be given before it can be taken (obtained) using the xSemaphoreTake() function.“
I wonder if this function differs from the older “vSemaphoreCreateBinary”, since I noticed that what is written in the website is different than what really happens when creating a binary semaphore.
I’m using an older version of FreeRTOS (v7.5.2) with the function “vSemaphoreCreateBinary” and in fact I must first take the semaphore (and not “give”, as it is said in the website) for it to work in the same way.
For example, as in a version written for Arduino, by William Greiman ( https://github.com/greiman/FreeRTOS-Arduino ), in example 12 from the book, he included the following comment in the code:
static void vHandlerTask( void pvParameters )
/ Note that when you create a binary semaphore in FreeRTOS, it is ready
to be taken, so you may want to take the semaphore after you create it
so that the task waiting on this semaphore will block until given by
another task. */
xSemaphoreTake( xBinarySemaphore, 0);
for( ;; )
The same happens with me and I’m not sure if it’s due to the version I’m using (and his version as well).
I checked the examples provided in the FreeRTOS website (standard edition examples) and although there is version v5.0.4 in the description, I noticed that this comment doesn’t exist as well as the call to the function “xSemaphoreTake” before the start of the “for” cycle.
so... in the end, my question is: the behaviour of the semaphores depends on the version/function used, or are the examples and explanations provided in the official website wrong?
I hope my explanation was enough to understand my problem. Could you please clarify this situation?
vSemaphoreCreateBinary() is the old way of creating a binary semaphore and will leave the semaphore in the opposite state to xSemaphoreCreateBinary(). I suspect the API documentation pages for the two functions will tell you how they are used, and the question has been asked a couple of times on the forum already so try searching the archive on the FreeRTOS website.
Thanks for the clarification! It justifies the problem I was having.
I would suggest to update the contents on the API page of vSemaphoreCreateBinary(), as there's no reference to the initial state of the semaphore. I'm pretty sure more people will run into the same problem when using this function, although it should not be used in the recent versions.