Defining bits in Freertos

I run the simple FreeRTOS code, in which program wait for five seconds for MYTASK_Bit to clear, and then ‎create a task “vMyTask”, which execute printf repeatedly with delay of one seconds.

The program runs fine ‎and do what it is supposed to do.‎

I have two questions:‎

‎1.‎ I have set MYTASK_Bit, which is first bit, ‎to 1 (‎#define MYTASK_Bit (1 << 0)‎). Does’nt this means ‎that the program should find it as “SET” (as its value is 1) and not wait for five seconds? ‎

‎2.‎ If I set MYTASK_Bit to 0 (‎#define MYTASK_Bit (0 << 0)), I get the error :‎ ‎

assert failed: xEventGroupWaitBits event_groups.c:371 (uxBitsToWaitFor != 0)‎.‎ What does it means?‎ My code is as below:‎

‎#define MYTASK_Bit (1 << 0)‎
    EventGroupHandle_t  xMyEventGroup; ‎

void vMyTask()‎
‎{‎
‎    for (;;){‎
‎        printf("This is in vMyTask.\n”);‎
‎       vTaskDelay(1000 / portTICK_PERIOD_MS);‎
       ‎}‎
‎}
‎
void app_main(void)‎
‎{‎
‎    xMyEventGroup = xEventGroupCreate();‎
‎    xEventGroupWaitBits(xMyEventGroup, MYTASK_Bit, pdTRUE, pdFALSE, 5000 / portTICK_PERIOD_MS);‎
‎    printf("Starting vMyTask\n");‎
‎    xTaskCreate(&vMyTask, "vMyTask", 4096, NULL, 1, NULL);‎}‎
‎}‎

I am using ESP32 board with esp_idf.

You don’t set a bit here :wink: Shifting a 0 by 0 bit positions gives a … 0.
In case you intended to set a bit to 1 as you’ve to because it’ s required by xEventGroupWaitBits as documented.

I can’t see anything in your code that is setting the bit - only waiting for it to be set. See the Event Groups API documentation and examples: FreeRTOS event groups and event bits API functions

This is because you are passing zero, i.e. no bits set, as the bit you want to wait for.

In other words, you need to call xEventGroupSetBits to set bit(s).

@aggarg wrote:

And beside that, you can not call xEventGroupWaitBits() with uxBitsToWaitFor equal to zero.

If you wait for a single bit, you could use any out of:

#define BIT_0   ( 1U << 0 )  /* or 0x0001 */
#define BIT_1   ( 1U << 1 )  /* or 0x0002 */
#define BIT_2   ( 1U << 2 )  /* or 0x0004 */
#define BIT_3   ( 1U << 3 )  /* or 0x0008 */
#define BIT_4   ( 1U << 4 )  /* or 0x0010 */
etc

And you can combine them if you wait for 2 bits as e.g. BIT_0|BIT_1.
Note that the highest 8 bits are reserved for the API’s. Which 8 bits these are depends on the size of TickType_t:

#if configUSE_16_BIT_TICKS == 1
    #define eventEVENT_BITS_CONTROL_BYTES    0xff00U
#else
    #define eventEVENT_BITS_CONTROL_BYTES    0xff000000UL
#endif

So an event can hold at most 24 bits.

I would recommend reading more tutorials about FreeRTOS and look at the examples given. Good luck

Thanks a lot to all of you for answering. So now my understanding is that when I say:

‎#define MYTASK_Bit (1 << 0)

It means that MYTASK_Bit ‎ is the first bit (from right, at index 0) of any event group I create.

Also as a bit value can only be 0 or 1, so when I define it, it holds the value of 0 by default.

Is my assumption right?

MYTASK_Bit is just a constant which does not have to do anything with event group. When you call xEventGroupSetBits( ev, MYTASK_Bit), first bit of the event group will be set.

Yes, a bit can only be 0 or 1.

You probably want to say that when you create an event group, all the bits are zero unless you explicitly set a bit by calling xEventGroupSetBits. If so, yes you are right.

Thanks Gaurav, this pretty much clear my concept about defining a bit in freetos.