首页 > 其他分享 >EventGroup

EventGroup

时间:2024-07-18 16:40:28浏览次数:6  
标签:group created EventBits EventGroupHandle EventGroup 事件 event

  事件组也是一种异步处理方式,,将事件监听和事件处理分离开。

  可以记录事件、清除事件标志位。

使用流程:

  创建事件组

  

  事件监听task                                       事件处理task

  事件发生,设置事件位                        等待事件位,清除相应事件位

                                                              处理事件

 

事件组创建:

//创建API
EventGroupHandle_t xEventGroupCreate( void );
EventGroupHandle_t xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer );



//eg:
/* Declare a variable to hold the created event group. */
EventGroupHandle_t xCreatedEventGroup;
/* Attempt to create the event group. */
xCreatedEventGroup = xEventGroupCreate();
/* Was the event group created successfully? */
if( xCreatedEventGroup == NULL )
{
 /* The event group was not created because there was insufficient
 FreeRTOS heap available. */
}
else
{
 /* The event group was created. */
}


/* Declare a variable to hold the handle of the created event group. */
EventGroupHandle_t xEventGroupHandle;
/* Declare a variable to hold the data associated with the created event group. */
StaticEventGroup_t xCreatedEventGroup;
void vAFunction( void )
{
 /* Attempt to create the event group. */
 xEventGroupHandle = xEventGroupCreate( &xCreatedEventGroup );
 /* pxEventGroupBuffer was not null so expect the event group to have been 
 created. */
 configASSERT( xEventGroupHandle );
}



//事件删除
void vEventGroupDelete( EventGroupHandle_t xEventGroup );

 

 

事件位操作

//获取当前事件位API 
EventBits_t xEventGroupGetBits( EventGroupHandle_t xEventGroup );
EventBits_t xEventGroupGetBitsFromISR( EventGroupHandle_t xEventGroup );


//设置事件位
EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
 const EventBits_t uxBitsToSet );

/*
    Setting bits in an event group will automatically unblock any tasks that were blocked waiting 
for the bits to be set. 解锁当前事件task
*/

//eg:
#define BIT_0 ( 1 << 0 )
#define BIT_4 ( 1 << 4 )
void aFunction( EventGroupHandle_t xEventGroup )
{
EventBits_t uxBits;
 /* Set bit 0 and bit 4 in xEventGroup. */
 uxBits = xEventGroupSetBits(
 xEventGroup, /* The event group being updated. */
 BIT_0 | BIT_4 );/* The bits being set. */
 if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) )
 {
 /* Both bit 0 and bit 4 remained set when the function returned. */
 }
 else if( ( uxBits & BIT_0 ) != 0 )
 {
 /* Bit 0 remained set when the function returned, but bit 4 was
 cleared. It might be that bit 4 was cleared automatically as a
 task that was waiting for bit 4 was removed from the Blocked
 state. */
 }
 else if( ( uxBits & BIT_4 ) != 0 )
 {
 /* Bit 4 remained set when the function returned, but bit 0 was
 cleared. It might be that bit 0 was cleared automatically as a
 task that was waiting for bit 0 was removed from the Blocked
 state. */
 }
 else
 {
 /* Neither bit 0 nor bit 4 remained set. It might be that a task
 was waiting for both of the bits to be set, and the bits were cleared
 as the task left the Blocked state. */
 }
}



//中断设置API 
BaseType_t xEventGroupSetBitsFromISR( EventGroupHandle_t xEventGroup,
 const EventBits_t uxBitsToSet,
 BaseType_t *pxHigherPriorityTaskWoken );



EventBits_t xEventGroupSync( EventGroupHandle_t xEventGroup,
 const EventBits_t uxBitsToSet,
 const EventBits_t uxBitsToWaitFor,
 TickType_t xTicksToWait );

 

等待事件:

EventBits_t xEventGroupWaitBits( const EventGroupHandle_t xEventGroup,    //等待事件组
 const EventBits_t uxBitsToWaitFor,  //等待事件位
 const BaseType_t xClearOnExit,      //在函数返回时是否清除事件位 pdTRUE 是
 const BaseType_t xWaitForAllBits,   //所有事件发生返回还是一有事件发生就返回
 TickType_t xTicksToWait );          //超时事件

/*
    read 事件位,阻塞等待,超时返回  不能在中断中调用
*/

//eg: #define BIT_0 ( 1 << 0 ) #define BIT_4 ( 1 << 4 ) void aFunction( EventGroupHandle_t xEventGroup ) { EventBits_t uxBits; const TickType_t xTicksToWait = pdMS_TO_TICKS( 100 ); /* Wait a maximum of 100ms for either bit 0 or bit 4 to be set within the event group. Clear the bits before exiting. */ uxBits = xEventGroupWaitBits( xEventGroup, /* The event group being tested. */ BIT_0 | BIT_4, /* The bits within the event group to wait for. */ pdTRUE, /* BIT_0 and BIT_4 should be cleared before returning. */ pdFALSE, /* Don't wait for both bits, either bit will do. */ xTicksToWait );/* Wait a maximum of 100ms for either bit to be set. */ if( ( uxBits & ( BIT_0 | BIT_4 ) ) == ( BIT_0 | BIT_4 ) ) { /* xEventGroupWaitBits() returned because both bits were set. */ } else if( ( uxBits & BIT_0 ) != 0 ) { /* xEventGroupWaitBits() returned because just BIT_0 was set. */ } else if( ( uxBits & BIT_4 ) != 0 ) { /* xEventGroupWaitBits() returned because just BIT_4 was set. */ } else { /* xEventGroupWaitBits() returned because xTicksToWait ticks passed without either BIT_0 or BIT_4 becoming set. */ } }

 

标签:group,created,EventBits,EventGroupHandle,EventGroup,事件,event
From: https://www.cnblogs.com/zypprocess/p/18309856

相关文章