Complete the code to create a binary semaphore in FreeRTOS.
SemaphoreHandle_t xSemaphore = xSemaphoreCreate[1]();In FreeRTOS, xSemaphoreCreateBinary() creates a binary semaphore used for simple signaling.
Complete the code to take a semaphore with a block time of 100 ticks.
if(xSemaphoreTake(xSemaphore, [1]) == pdTRUE) { // Critical section }
The second parameter of xSemaphoreTake() is the block time in ticks. Here, 100 ticks means the task waits up to 100 ticks to take the semaphore.
Fix the error in the code to give the semaphore from an ISR.
void vISR_Handler(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(xSemaphore, [1]);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}When giving a semaphore from an ISR, you must pass the address of xHigherPriorityTaskWoken to xSemaphoreGiveFromISR() so FreeRTOS can manage task switching properly.
Fill both blanks to create a counting semaphore with a max count of 5 and initial count of 3.
xSemaphore = xSemaphoreCreateCounting([1], [2]);
xSemaphoreCreateCounting() takes two parameters: the maximum count and the initial count of the semaphore.
Fill all three blanks to check if a semaphore is available and then take it without blocking.
if(xSemaphore[1](xSemaphore, [2]) == [3]) { // Semaphore taken }
xSemaphoreTake() tries to take the semaphore. Passing 0 as block time means no waiting. It returns pdTRUE if successful.
