Unused Event Group Bit Setting and Clearing

Hello all,

I’ve run into something strange and I was wondering if I have some misunderstanding regarding the use of event groups. Below I have copied the code for 3 tasks: UV, FR, and Confidence.

UV:

void UVTask(
    void *parameters            /* Task startup parameters */
)
{

    ( void ) parameters;
    static EventBits_t xTCBits;

    unsigned int ADCData = 50u;       /* Set to 50 for testing */

    EventBits_t UV_Control = ( UV_ONLY_MODE | OR_MODE );
    while(LOOP_FOREVER)
    {
        xTCBits = xEventGroupWaitBits(
                        xTaskControlBits,
                        UV_Control,
                        pdFALSE,
                        pdFALSE,
                        portMAX_DELAY );

        /* Added to give easier to read output in tracealyzer. */
        UVConfidenceQueueAdd(ADCData);
        xEventGroupSetBits( xNewDataSignalBits, TASK_BIT_UV_NEW_READING );
        vTaskDelay(35/portTICK_PERIOD_MS);

    }
}

FR:

void FRTask(
    void *parameters            /* Task startup parameters */
)
{

    ( void ) parameters;

    unsigned int ADCData = 50u;       /* Set to 50 for testing */

    static EventBits_t xTCBits;

    EventBits_t FR_Control =( FR_ONLY_MODE | OR_MODE );
    while(LOOP_FOREVER)
    {
        xTCBits = xEventGroupWaitBits(
                        xTaskControlBits,
                        FR_Control,
                        pdFALSE,
                        pdFALSE,
                        portMAX_DELAY );

        /* Added to give easier to read output in tracealyzer. */
        FRConfidenceQueueAdd(ADCData);
        xEventGroupSetBits( xNewDataSignalBits, TASK_BIT_FR_NEW_READING );

        vTaskDelay(25/portTICK_PERIOD_MS);
    }
}

Confidence:

void ConfidenceTask(
    void *parameters            /* Task startup parameters */
)
{

    ( void ) parameters;

    static EventBits_t xTCBits;

    EventBits_t CON_Control =( TASK_BIT_FR_NEW_READING | TASK_BIT_UV_NEW_READING );
    while(LOOP_FOREVER)
    {
        xTCBits = xEventGroupWaitBits(
                        xNewDataSignalBits,
                        CON_Control,
                        pdFALSE,
                        pdFALSE,
                        portMAX_DELAY );

        if((xEventGroupGetBits(xNewDataSignalBits) & TASK_BIT_FR_NEW_READING))
        {
            FRConfidenceAverage();
            xEventGroupClearBits(xNewDataSignalBits, TASK_BIT_FR_NEW_READING);

        }
        if((xEventGroupGetBits(xNewDataSignalBits) & TASK_BIT_UV_NEW_READING))
        {
            FRConfidenceAverage();
            xEventGroupClearBits(xNewDataSignalBits, TASK_BIT_UV_NEW_READING);
        }
        /* Added to give easier to read output in tracealyzer. */
    }
}

The UV and FR tasks send data into two circular queues periodically and after each new entry an event group flag will be raised which will allow the confidence task to run. As there is a lot of delay between tasks, it is expected that only on flag will be raised every time the confidence task is taken out of the blocked state. What I expect is the average function related to the task that set the flag to be called and then the flag will be lowered which would put the task back in the blocked state when it loops back around. I have attached two screenshots from Tracealyzer of what is actually happening:

After the UV task runs:
image

After the FR Task runs:
image

First of all, I’m not quite sure why it says its clearing bit 2 when the two event group bits I am currently using are defined as:
#define TASK_BIT_FR_NEW_READING ( 1UL << 0UL )
#define TASK_BIT_UV_NEW_READING ( 1UL << 1UL )

Second, I’m not sure why bit 0 is being cleared twice in both instances. I feel like I have a misunderstanding here, any help is greatly appreciated.

Thanks in advance.

That isn’t “Bit 0” being cleared, but a BIT PATTERN of 0 being clear (i.e. no bits). and the 2 isn’t “bit 2” but the bit pattern 2 = binary 0010 which is just bit 1

I suppose the code could have tested for the pattern being 0 and skipping the clear, but clearing no bits is fairly fast, and it is more common to clear the bits when received.

The typical pattern would be for the xEventGroupWaitBits to clear the bits after wake up, and then the following test uses the return values to see which bits it was woken up for, rather than asking the EventGroup for the current bit value.

Thanks for the response, that makes more sense.