FreeRTOS operating system -- counting semaphores

FreeRTOS operating system

preface

Counting semaphores are called numerical semaphores. Binary semaphores are equivalent to queues with a length of 1, so counting semaphores are queues with a length greater than 1. Like binary semaphores, users do not need to care about what data is stored in the queue, but only whether the queue is empty.
Counting semaphores are usually used in the following two occasions:

   1,Event count
   2,resource management

1. Event count
In this case, each time an event occurs, the semaphore is released in the event handler function (increase the semaphore count)
Value), other tasks will obtain semaphores (the semaphore count value minus one, and the semaphore value is the member variable of the queue structure
Uxmessages waiting) to handle events. In this case, the initial count value of the count semaphore created is 0.

2. Resource management in this case, the semaphore value represents the available quantity of current resources, such as the number of remaining parking spaces in the parking lot. If a task wants to obtain the right to use resources, it must first obtain the semaphore. After the semaphore is successfully obtained, the semaphore value will be reduced by one. When the semaphore value is 0, there are no resources. When a task uses up resources, it must release semaphores. After releasing semaphores, the semaphore value will be increased by one. In this case, the initial value of the count semaphore created should be the number of resources. For example, there are 100 parking spaces in the parking lot. When creating a semaphore, the semaphore value should be initialized to 100.

1, API function


1. Function xsemaphotorecreatecounting()
This function is used to create a counting semaphore, and the required memory is allocated through dynamic memory management method.

SemaphoreHandle_t xSemaphoreCreateCounting(UBaseType_t uxMaxCount, 
UBaseType_t uxInitialCount )

Parameters:
uxMaxCount: counts the maximum count value of the semaphore. When the semaphore value is equal to this value, releasing the semaphore will fail.
uxInitialCount: counts the initial value of the semaphore.
Return value:
NULL: count semaphore creation failed.
Other values: the count semaphore is created successfully, and the count semaphore handle is returned.

Static creation is usually not used. Here is too much introduction



Releasing counting semaphores and obtaining counting semaphores are the same as binary semaphores. Please refer to the previous blog

2, Counting semaphore experiment


//ÊͷżÆÊýÐÍÐźÅÁ¿ÈÎÎñº¯Êý
void SemapGive_task(void *pvParameters)
{
	u8 key,i=0;
    u8 semavalue;
	BaseType_t err;
	while(1)
	{
		key=KEY_Scan(0);           	//ɨÃè°´¼ü
        if(CountSemaphore!=NULL)  	//¼ÆÊýÐÍÐźÅÁ¿´´½¨³É¹¦
        {
            switch(key)
            {
                case WKUP_PRES:
                    err=xSemaphoreGive(CountSemaphore);//ÊͷżÆÊýÐÍÐźÅÁ¿   err·µ»ØÖµ   º¯Êý£¨¾ä±ú£©
					if(err==pdFALSE)
					{
						printf("ÐźÅÁ¿ÊÍ·Åʧ°Ü!!!\r\n");
					}
                    semavalue=uxSemaphoreGetCount(CountSemaphore);	//»ñÈ¡¼ÆÊýÐÍÐźÅÁ¿Öµ   ¼ÆÊýÁ¿µÄÖµ = º¯Êý(¾ä±ú)
					        //ÕâÀïÒªºÍ¶þÖµÐźÅÁ¿Çø·Ö  ÕâÀïµÄ·µ»ØÖµÊÇ»ñÈ¡ÐźÅÁ¿µÄÖµ  ʹÓà uxSemaphoreGetCount
                    LCD_ShowxNum(155,111,semavalue,3,16,0);	    	//ÏÔʾÐźÅÁ¿Öµ
                    break;
            }
        }
        i++;
        if(i==50)
        {
            i=0;
            LED0=!LED0;
        }
        vTaskDelay(10);     //ÑÓʱ10ms£¬Ò²¾ÍÊÇ10¸öʱÖÓ½ÚÅÄ	
	}
}

//»ñÈ¡¼ÆÊýÐÍÐźÅÁ¿ÈÎÎñº¯Êý
void SemapTake_task(void *pvParameters)
{
    u8 num;
    u8 semavalue;
	while(1)
	{
        xSemaphoreTake(CountSemaphore,portMAX_DELAY); 	//µÈ´ýÊýÖµÐźÅÁ¿   º¯Êý(¾ä±ú£¬×èÈûʱ¼ä)
        num++;
        semavalue=uxSemaphoreGetCount(CountSemaphore); 	//»ñÈ¡ÊýÖµÐźÅÁ¿Öµ
        LCD_ShowxNum(155,111,semavalue,3,16,0);         //ÏÔʾÐźÅÁ¿Öµ
		LCD_Fill(6,131,233,313,lcd_discolor[num%14]);   //Ë¢ÆÁ
		LED1=!LED1;
        vTaskDelay(1000);                               //ÑÓʱ1s£¬Ò²¾ÍÊÇ1000¸öʱÖÓ½ÚÅÄ	
	}
}

summary

The counting semaphore is similar to the binary semaphore. The return value of the binary semaphore is similar to 0 and 1. The counting return value is a specific acquired value. Master the use of these functions.

Keywords: Single-Chip Microcomputer stm32

Added by estero2002 on Fri, 21 Jan 2022 02:13:05 +0200