#include "EventGroup.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
EventGroupHandle_t EventGroupHandle;
#define BIT_0 (1 << 0)
#define BIT_4 (1 << 4)
static void task_1(void *arg)
{
while (true)
{
printf("--------------------------------------------------------\n");
printf("Task_1 is waiting for BIT_0 or BIT_4 is set.\n");
xEventGroupWaitBits(
EventGroupHandle, /* 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. */
portMAX_DELAY); /* 无限时长等待。 */
printf("--------------------------------------------------------\n");
printf("Task_1: BIT_0 or BIT_4 is set.\n");
vTaskDelay(1000);
}
}
static void task_2(void *arg)
{
while (true)
{
printf("--------------------------------------------------------\n");
printf("Task_2 begin to set bits.\n");
xEventGroupSetBits(EventGroupHandle, BIT_0);
vTaskDelay(5000);
xEventGroupSetBits(EventGroupHandle, BIT_4);
vTaskDelay(pdMS_TO_TICKS(5000));
}
}
void EventGroup_Test(void)
{
vTaskDelay(1000);
EventGroupHandle = xEventGroupCreate();
if (EventGroupHandle == NULL)
{
printf("Error: Failed to create EventGroup\n");
}
else
printf("EventGroup Ok!\n");
vTaskSuspendAll(); //停止任务调度器,挂起所有任务
xTaskCreatePinnedToCore(task_1, "Task_1", 4096 * 2, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(task_2, "Task_2", 4096 * 2, NULL, 1, NULL, 1);
xTaskResumeAll(); //启用任务调度器
}
几个函数
EventGroupHandle_t xEventGroupCreate( void );//创建事件组
EventBits_t xEventGroupWaitBits( const EventGroupHandle_t xEventGroup,//事件组句柄
const EventBits_t uxBitsToWaitFor,//要等待检测的位
const BaseType_t xClearOnExit,//执行完后是否请吃位
const BaseType_t xWaitForAllBits,//取or或者and
TickType_t xTicksToWait );//等待时间
EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
const EventBits_t uxBitsToSet );//设置位的函数
一个官方的例子
#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 );
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. */
} }