esp32 freertos事件组等待

这个示例展示了如何在FreeRTOS中使用事件组进行任务间的通信。task_1等待BIT_0或BIT_4被设置,而task_2则周期性地设置这两个位。xEventGroupWaitBits函数用于等待特定位被设置,xEventGroupSetBits函数用于设置位。
摘要由CSDN通过智能技术生成
#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. */
 } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值