FreeRTOS task notifications using the notification task for the event flag groups

table of Contents

characteristic

Application restrictions

Task notification as the event flag groups

As a task notification advantages event flag groups

Task notification as a drawback event flag groups

Part of the API

test program


characteristic

Each task has a 32-bit value of the task assignment notification, task notifications are sent directly to the mission of the event, to unblock the receiving task.

Task notification can update the notification task value by the following four ways:

Set value of the received notification tasks (not overwrite the previous value);

Notification setting value (overwrites the previous value) of the received job;

Set up one or more receive job notifications value;

Increase the value of the received notification tasks.

Due to the flexibility of the notification task may require the use of single queue, the amount of the binary signal, counting semaphores, event flags set when it is used instead. Use task notification task to unlock faster way than above 45%, and requires less of ram. 

Application restrictions

  1. Task notification can only be used if the task is only one receiving the event;
  2. Notification task queue may be used instead of: receiving a task can be notified by blocking state of waiting tasks, the task should not block the transmission state, even if not sent.

Task notification as the event flag groups

xTaskNotifyWait ()             is used in place of xEventGroupWaitBits ()

xTaskNotify () is used in place of xEventGroupSetBits ()

xTaskNotifyFromISR () is used in place of xEventGroupSetBitsFromISR ()

As a task notification advantages event flag groups

Compared with xEventGroupSetBitsFromISR (), xTaskNotifyFromISR () has a significant performance advantage, because xTaskNotifyFromISR () fully implemented in the ISR, and xEventGroupSetBitsFromISR () must be delayed to some processing daemon RTOS task.

Task notification as a drawback event flag groups

Task notification can not be combined position to judge, when there is a matched, leaving the blocking state, if the bit is a combination of judgment, require additional judge.

Part of the API

xTaskNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction );
//发送任务通知,参数xTaskToNotify是发向的任务句柄,ulValue是发送的任务通知值,eAction 是发送的形式
xTaskNotifyFromISR( TaskHandle_t xTaskToNotify, 
uint32_t ulValue, 
eNotifyAction eAction, 
BaseType_t *pxHigherPriorityTaskWoken );//同上,用于中断函数中

xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, 
uint32_t ulBitsToClearOnExit, 
uint32_t *pulNotificationValue, 
TickType_t xTicksToWait );
//接收任务通知,参数ulBitsToClearOnEntry表示读取之前是否清除,设置为0xffffffff将会清除
//参数ulBitsToClearOnExit表示读取之后是否清楚,设置为0xffffffff将会清除
//参数pulNotificationValue用于获取任务通知值
//参数xTicksToWait表示阻塞时间 

Task notification form sent eNotifyAction

/* Actions that can be performed when vTaskNotify() is called. */
typedef enum
{
    eNoAction = 0,/* Notify the task without updating its notify value. */
    eSetBits,	                //设置位
    eIncrement,	                //增加任务通知值
    eSetValueWithOverwrite,     //覆盖的方式设置任务通知值
    eSetValueWithoutOverwrite	//不覆盖的方式设置任务通知值
} eNotifyAction;

test program

The overall design: two tasks, an interrupt

counttask: counted and, counting the time task 50 transmits a notification

serialtask:获取任务通知,并判断是何种事件

串口中断:收到数据之后,发送任务通知

创建任务

#define COUNT_TASK_PRIO			    1	 //任务优先级
#define COUNT_TASK_STK_SIZE 		    80   //任务堆栈大小
TaskHandle_t CountTaskHandler;                   //任务句柄
void CountTaskFunc(void *pvParameters);          //任务函数

#define SERIAL_TASK_PRIO		    4	 //任务优先级
#define SERIAL_TASK_STK_SIZE 		    80   //任务堆栈大小
TaskHandle_t SerialTaskHandler;                  //任务句柄
void SerialTaskFunc(void *pvParameters);         //任务函数

#define BIT_0   1<<0
#define BIT_1   1<<1
#define BIT_2   1<<2

void OtherTest(void )
{
    BaseType_t ret;
	
    BoardInitMcu();
    BoardInitPeriph();
		
    ret=xTaskCreate( (TaskFunction_t )CountTaskFunc,     	
		     (const char*    )"counttask",   	
		     (uint16_t       )COUNT_TASK_STK_SIZE, 
		     (void*          )NULL,				
		     (UBaseType_t    )COUNT_TASK_PRIO,	
		     (TaskHandle_t*  )&CountTaskHandler); 
	
    ret=xTaskCreate((TaskFunction_t )SerialTaskFunc,     
		    (const char*    )"serialtask",   
		    (uint16_t       )SERIAL_TASK_STK_SIZE, 
		    (void*          )NULL,
		    (UBaseType_t    )SERIAL_TASK_PRIO,
		    (TaskHandle_t*  )&SerialTaskHandler); 
				
    vTaskStartScheduler(); 
}

各个任务函数

void  CountTaskFunc(void *pvParameters)
{
    static uint8_t i;
	
    for(;;)
    {
	i++;
		
	printf("count =%d\r\n",i);

	if(i==50)
	{
	    i=0;

	    xTaskNotify( SerialTaskHandler, BIT_1, eSetBits );
	}
						
        vTaskDelay(500);                     //延时500ms,也就是500个时钟节拍	
    }
}

void SerialTaskFunc(void *pvParameters)
{
    BaseType_t ret;
    uint32_t pulNotificationValue;
	
    for(;;)
    {
	ret= xTaskNotifyWait(0,   //读取之前,设置相应的位,0:不设置,ffffffffff:设置所有的位                  
			0xffffffff,     //读取之后,清除相应的位
	     &pulNotificationValue, 	//读取到的值
	                        10);  //阻塞时间
	
        if(ret==pdPASS)
        {
	    if((pulNotificationValue&BIT_0)==BIT_0)
	    {
	        printf("serial \r\n");
	    }
	    else
	    {
	        if((pulNotificationValue&BIT_1)==BIT_1)
	        {
		    printf("count \r\n");
	        }
	        else
	        {
		    printf("default");
	        }
	    }
        }
        else
        {
	    vTaskDelay(10);
        }		
    }
}

中断函数

void USART1_IRQHandler( void )
{
    BaseType_t pxHigherPriorityTaskWoken=pdFALSE;
	
    //省略部分代码
    
    if(RESET != __HAL_UART_GET_FLAG(&UartHandle,UART_FLAG_IDLE))
    {                   
	__HAL_UART_CLEAR_IDLEFLAG(&UartHandle);
 	
	/*任务通知用作事件标志组*/
	xTaskNotifyFromISR( SerialTaskHandler, 
                                       (1<<0), 
                                     eSetBits,     
                   &pxHigherPriorityTaskWoken);
	portYIELD_FROM_ISR(pxHigherPriorityTaskWoken);
    }
}

运行结果

与使用事件标志组是一样的。

 

发布了35 篇原创文章 · 获赞 25 · 访问量 1万+

Guess you like

Origin blog.csdn.net/freemote/article/details/104413285