xEventGroupWaitBits returns zero


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:

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:

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);

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!

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.

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.

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

Just commented console_printf out and replaced to LED switched on if uxBits nonzero. Same result :frowning:

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:

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

  1. Define a task that acts like the interrupt executing:
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.
    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 );
    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:

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

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!

Thank you for reporting back.