# xEventGroupWaitBits returns zero

**URL:** <https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850>\
**Category:** Kernel\
**Created:** [February 27, 2023, 5:21pm UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850 "2023-02-27T17:21:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sgergo](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sgergo](https://forums.freertos.org/u/sgergo)\
**Post date:** [February 27, 2023, 5:21pm UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/1 "2023-02-27T17:21:32Z")

</div>

Hi

I’ve a small test application using eventgroup bits and I’m not getting anything from xEventGroupWaitBits but zero. The function where I set a bit is here:

```auto
void main_set_event_bits(uint32_t bits) {
  BaseType_t xHigherPriorityTaskWoken, xResult;
  EventBits_t getbits;

  xResult = xEventGroupSetBitsFromISR(xCreatedEventGroup, bits, &xHigherPriorityTaskWoken);
  if( xResult != pdFAIL ) {
    /* If xHigherPriorityTaskWoken is now set to pdTRUE then a context
    switch should be requested. The macro used is port specific and will
    be either portYIELD_FROM_ISR() or portEND_SWITCHING_ISR() - refer to
    the documentation page for the port being used. */
    getbits = xEventGroupGetBitsFromISR(xCreatedEventGroup);
    console_printf("Eventgroup bits are %08x\n", bits);
    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  }
}

```

The task waiting for the bit to be set:

```auto
static void nucleo_led_task(void *param) {
  EventBits_t uxBits;
  const TickType_t xTicksToWait = pdMS_TO_TICKS(5000);

  for (;;) {
    uxBits = xEventGroupWaitBits( xCreatedEventGroup,
      0xff, /* The bits within the event group to wait for. */
      pdTRUE,/* Bits 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. */

      console_printf("Eventgroup bits set: %08x\n", uxBits);
      board_toggle_NUCLEO_LED();
  }
}

```

This task prints zero in every 5 seconds no matter which bit is set by the main\_set\_event\_bits function which is called from an irq. In the main\_set\_event\_bits fn the xEventGroupGetBitsFromISR prints the bits set correctly so I assume that xEventGroupSetBitsFromISR is working well. The eventgroup is global and it is created successfully. Any help would be great I’m pretty clueless what can be the problem! Thank you!

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [February 27, 2023, 5:53pm UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/2 "2023-02-27T17:53:09Z")

</div>

Unrelated but you need to initialize `BaseType_t xHigherPriorityTaskWoken = pdFALSE;`  
Also very often `printf`-family functions can’t be used in ISRs. Are you sure it’s ok to call `console_printf` in the ISR ? Maybe just comment it and retry.  
I guess the bits you set are covered by the 0xFF mask i.e. you use b7:0.

---

<div class="post-metadata">

**Author:** ![sgergo](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sgergo](https://forums.freertos.org/u/sgergo)\
**Post date:** [February 27, 2023, 6:11pm UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/3 "2023-02-27T18:11:14Z")

</div>

Hi Hartmut

good observation on the initialization, thank you. The console\_printf fn is rolled by me and had been tested many many times so I don’t think that is the problem.

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [February 27, 2023, 6:15pm UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/4 "2023-02-27T18:15:20Z")

</div>

Hmm … it’s a pitty that `console_printf` works (and the ISR stack is large enough ?) 😉  
Because your code looks fine so far and that was the only potential issue … strange.

---

<div class="post-metadata">

**Author:** ![sgergo](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sgergo](https://forums.freertos.org/u/sgergo)\
**Post date:** [February 27, 2023, 6:22pm UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/5 "2023-02-27T18:22:01Z")

</div>

Just commented console\_printf out and replaced to LED switched on if uxBits nonzero. Same result ☹

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [February 28, 2023, 2:30am UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/6 "2023-02-28T02:30:13Z")

</div>

I just tried the following.

1. Modify main\_set\_event\_bits() so it doesn’t print out - never a good idea in an interrupt unless printing is just writing to RAM for later output, even then it can be slow and take a lot of stack.

2. Modify nucleo\_led\_task() so it prints out the received event bits, and the event bits after the call to xEventGroupWaitBits(), thus:

```auto
printf( "Eventgroup bits received: %08x and now %08x\n", 
            uxBits, 
            xEventGroupGetBitsFromISR( xCreatedEventGroup ) );

```

1. Define a task that acts like the interrupt executing:

```auto
static void pretend_interrupt( void * param )
{
    for( ;; )
    {
        /* NOTE: Uses 0xa5 as the bits to set. */
        main_set_event_bits( 0xa5 );
        vTaskDelay( 2000 );
    }
}

```

1. Create both tasks.

```auto
{
    xCreatedEventGroup = xEventGroupCreate();
    configASSERT( xCreatedEventGroup );

    xTaskCreate( nucleo_led_task, "task", configMINIMAL_STACK_SIZE, NULL, 1, NULL );
    xTaskCreate( pretend_interrupt, "int", configMINIMAL_STACK_SIZE, NULL, 2, NULL );
    vTaskStartScheduler();
    for( ;; );
}

```

The output I got was as expected. One tasks calls the interrupt function, setting the bits to 0xa5, the other task reads 0xa5 from the event group, clearing the bits to zero as it reads:

```auto
Eventgroup bits received: 000000a5 and now 00000000
Eventgroup bits received: 000000a5 and now 00000000
Eventgroup bits received: 000000a5 and now 00000000

```

---

<div class="post-metadata">

**Author:** ![sgergo](https://avatars.discourse-cdn.com/v4/letter/s/e0b2c6/32.png) [@sgergo](https://forums.freertos.org/u/sgergo)\
**Post date:** [February 28, 2023, 11:00am UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/7 "2023-02-28T11:00:05Z")

</div>

Hi Richard

thank you for your help. I modified my code as per you recommended but still received zeroes. Then I created another fn to set event bits but with the non-isr version (xEventGroupSetBits) and with that I got it working. So I started to inspect why the ISR one did not work and found a forgotten task with higher priority which had a busy loop inside running. That task caused the problem, now everything is working as expected. Thank you for the hint!

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [March 1, 2023, 10:52am UTC](https://forums.freertos.org/t/xeventgroupwaitbits-returns-zero/16850/8 "2023-03-01T10:52:03Z")

</div>

Thank you for reporting back.
