[FreeRTOS learning - counting semaphore]

Follow the learning records of Wei Dongshan's FreeRTOS teaching materials

FreeRTOS all project code links (updating)

https://gitee.com/chenshao777/free-rtos_-study


counting semaphore

Create a counting semaphore

QueueHandle_t xSemaphoreCreateCounting( const UBaseType_t uxMaxCount, 
										const UBaseType_t uxInitialCount )

uxMaxCount: The maximum counting semaphore uxInitialCount: The total counting times of the initial counting semaphore = uxMaxCount - uxInitialCount

Example usage:

QueueHandle_t vSemaphoreCount;  //初始化计数信号量句柄

//......
//......

int main()
{
    
    
	//......
	//......
	
	/* 初始化计数信号量 : 要将 configUSE_COUNTING_SEMAPHORES 宏置1 */
	vSemaphoreCount = xSemaphoreCreateCounting(3, 0);    // 一共计数3次
}

Acquire and release counting semaphores (same as binary semaphore operations)

/* 获取信号量结果变量 */
BaseType_t result;  

//......
//......

/* 获取计数信号量 */
result = xSemaphoreTake(vSemaphoreCount, portMAX_DELAY);  
if(result == pdPASS)
{
    
    
	printf("读取到二值信号量\r\n");
}

/* 释放计数信号量 */
result = xSemaphoreGive(vSemaphoreCount);   
if(result == pdPASS)
	printf("释放计数信号量:%d\r\n",i);
else
	printf("释放失败\r\n");

The difference between counting semaphore & binary semaphore

The counting semaphore can be used as a binary semaphore, only need to make the maximum counting semaphore = initial counting semaphore + 1

Guess you like

Origin blog.csdn.net/HuangChen666/article/details/129984857