下载 FreeRTOS
 

出色的 RTOS & 嵌入式软件

内核
最新资讯
简化任何设备的身份验证云连接。
利用 CoAP 设计节能型云连接 IoT 解决方案。
11.0.0 版 FreeRTOS 内核简介:
FreeRTOS 路线图和代码贡献流程。
使用 FreeRTOS 实现 OPC-UA over TSN。

xEventGroupSync()
[事件组 API]



event_groups.h

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

以原子方式设置RTOS 事件组中的位(标志),然后等待 在同一事件组中设置位的组合。 此功能通常 用于同步多个任务(通常称为任务集合),其中每个 任务必须等待其他任务到达同步点 才能继续。

不能从中断使用此函数。

如果设置了 uxBitsToWait 参数指定的位, 或者在该时间内设置了这些位,则该函数将在其阻塞时间到期之前返回。 在 这种情况下,uxBitsToWait 指定的所有位将在 函数返回之前自动清除。

RTOS源文件 FreeRTOS/source/event_groups.c 必须 包含在构建中,确保 xEventGroupSync()函数可用。

参数:
xEventGroup   设置和测试位的事件组。 必须 必须已通过 通过调用 xEventGroupCreate() 创建。
uxBitsToSet   在确定(且可能等待)uxBitsToWait参数指定的所有位 设置完成之前, 要设置事件组中的一个或多个位。 例如,将uxBitsToSet设置为0x04 即可设置事件组中的位2。
uxBitsToWaitFor   指定事件组中要测试的一个或多个事件位 的按位值。 例如,如果要等待 位0和位2被设置,则将uxBitsToWaitFor设置为0x05。 如果要等待 位0、位1和位2被设置,则将uxBitsToWaitFor设置为0x07 等等。
xTicksToWait   等待uxBitsToWaitFor参数值指定的 所有位设置完成的最长时间 (以“ticks”表示)。
返回:
等待置位时或阻塞到期时 事件组的值。 测试返回值 以便了解设置了哪些位。

如果xEventGroupSync()由于超时而返回, 则不会设置所有等待位。

如果 xEventGroupSync()因其所等待的所有位都被设置而返回, 那么返回值是自动清除任何位之前的 事件组值。

用法示例:
/* Bits used by the three tasks. */
#define TASK_0_BIT        ( 1 << 0 )
#define TASK_1_BIT        ( 1 << 1 )
#define TASK_2_BIT        ( 1 << 2 )

#define ALL_SYNC_BITS ( TASK_0_BIT | TASK_1_BIT | TASK_2_BIT )

/* Use an event group to synchronise three tasks.  It is assumed this event
group has already been created elsewhere. */
EventGroupHandle_t xEventBits;

void vTask0( void *pvParameters )
{
EventBits_t uxReturn;
TickType_t xTicksToWait = 100 / portTICK_PERIOD_MS;

    for( ;; )
    {
        /* Perform task functionality here. */
        . . .

        /* Set bit 0 in the event group to note this task has reached the
        sync point.  The other two tasks will set the other two bits defined
        by ALL_SYNC_BITS.  All three tasks have reached the synchronisation
        point when all the ALL_SYNC_BITS are set.  Wait a maximum of 100ms
        for this to happen. */
        uxReturn = xEventGroupSync( xEventBits,
                                    TASK_0_BIT,
                                    ALL_SYNC_BITS,
                                    xTicksToWait );

        if( ( uxReturn & ALL_SYNC_BITS ) == ALL_SYNC_BITS )
        {
            /* All three tasks reached the synchronisation point before the call
            to xEventGroupSync() timed out. */
        }
    }
}

void vTask1( void *pvParameters )
{
    for( ;; )
    {
        /* Perform task functionality here. */
        . . .

        /* Set bit 1 in the event group to note this task has reached the
        synchronisation point.  The other two tasks will set the other two
        bits defined by ALL_SYNC_BITS.  All three tasks have reached the
        synchronisation point when all the ALL_SYNC_BITS are set.  Wait
        indefinitely for this to happen. */
        xEventGroupSync( xEventBits, TASK_1_BIT, ALL_SYNC_BITS, portMAX_DELAY );

        /* xEventGroupSync() was called with an indefinite block time, so
        this task will only reach here if the syncrhonisation was made by all
        three tasks, so there is no need to test the return value. */
    }
}

void vTask2( void *pvParameters )
{
    for( ;; )
    {
        /* Perform task functionality here. */
        . . .

        /* Set bit 2 in the event group to note this task has reached the
        synchronisation point.  The other two tasks will set the other two
        bits defined by ALL_SYNC_BITS.  All three tasks have reached the
        synchronisation point when all the ALL_SYNC_BITS are set.  Wait
        indefinitely for this to happen. */
        xEventGroupSync( xEventBits, TASK_2_BIT, ALL_SYNC_BITS, portMAX_DELAY );

        /* xEventGroupSync() was called with an indefinite block time, so
        this task will only reach here if the syncrhonisation was made by all
        three tasks, so there is no need to test the return value. */
    }
}





Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.