EventGroupWaitBits disfunction on tasks with same priority

I am trying to learn how to use FreeRTOS, and I have a disfunctionning when I use the EventGroup. My exercise projet is to create a counter using seconds, minutes, hours. I used different tasks for each RaZ (dont say me you could use only one task cause the goal was to learn). My code is as following, the eventgroup has been put as a global variable : In main() : ~~~ /* Create tasks / xTaskCreate( IncVar_Timer, / Function pointer / “Task1”, / Task name – for debugging only/ configMINIMAL_STACK_SIZE, / Stack depth in words / (void) NULL, /* Pointer to tasks arguments (parameter) / 1, / Task priority/ NULL / Task handle */ );
xTaskCreate(
        DetectLimitReach,
        "Task2",
        configMINIMAL_STACK_SIZE,
        (void*) NULL,
        1,
        NULL);

xTaskCreate(
        RaZsec_IPC,
        "Task3",
        configMINIMAL_STACK_SIZE,
        (void*) NULL,
        1,
        &Task_sec);

xTaskCreate(
        RaZmin_IPC,
        "Task4",
        configMINIMAL_STACK_SIZE,
        (void*) NULL,
        1,
        &Task_min);

xTaskCreate(
        RaZheure_IPC,
        "Task5",
        configMINIMAL_STACK_SIZE,
        (void*) NULL,
        1,
        &Task_heure);

/* Start the RTOS Scheduler */
vTaskStartScheduler();

/* HALT */
while(1);

Tasks : (their prototype has been written before the main)
void IncVar_Timer(void *pvParameters){
while (1) {

    vTaskDelay(1000 / portTICK_RATE_MS);
    secondes += 1;

}
vTaskDelete(NULL);
} void DetectLimitReach(void *pvParameters){
ComptFin_Group = xEventGroupCreate(); //nom du EventGroup
if (ComptFin_Group == NULL)
{   while (1);  }//fatal error



while (1) {

    if(secondes>=3)
    {
        ComptFin = xEventGroupSetBits(
                ComptFin_Group,
                ( 1 << 0 ));
    }
    if(minutes>=2)
    {
        ComptFin = xEventGroupSetBits(
                ComptFin_Group,
                ( 1 << 1 ));
    }
    if(heures>=2)
    {
        ComptFin = xEventGroupSetBits(
                ComptFin_Group,
                ( 1 << 2 ));
    }
vTaskDelay(400 / portTICKRATEMS); } vTaskDelete(NULL); } void RaZsec_IPC(void *pvParameters) {
EventBits_t ComptFin;

while (1) {

    ComptFin = xEventGroupWaitBits(
            ComptFin_Group,
            ( 1 << 0 ),
            pdTRUE,
            pdTRUE,
            portMAX_DELAY );

    secondes = 0;
    minutes += 1;
}
vTaskDelete(NULL);
} void RaZmin_IPC(void *pvParameters) {
EventBits_t ComptFin;

while (1) {

    ComptFin = xEventGroupWaitBits(
            ComptFin_Group,
            ( 1 << 1 ),
            pdTRUE,
            pdTRUE,
            portMAX_DELAY );

    minutes = 0;
    heures += 1;
}
vTaskDelete(NULL);
} void RaZheure_IPC(void *pvParameters) {
EventBits_t ComptFinn;

while (1) {
    ComptFin = xEventGroupWaitBits(
            ComptFin_Group,
            ( 1 << 2 ),
            pdTRUE,
            pdTRUE,
            portMAX_DELAY );

    heures = 0;
}
vTaskDelete(NULL);
} ~~~ Actualy my problem is as following : the flag needed for RaZheureIPC is up, but the tasks never goes out from the blocking state. I know how to fix the problem : create the task IncVarTimer in last or put her at a higher priority. To prevent any problem from that cause in future projetcs, I would like to find what causes it, but actualy I don’t get why this fix works nor why I have this problem. Any idea ? [RB edited for formatting]

EventGroupWaitBits disfunction on tasks with same priority

Can you please post your main.c and FreeRTOSConfig.h, then I can try it here.

EventGroupWaitBits disfunction on tasks with same priority

By which I mean attached the files to the post, not cut and paste them into a post – thanks. Also – which version of FreeRTOS are you using, and which port (which chip and which compiler).

EventGroupWaitBits disfunction on tasks with same priority

The named old is my main.c. Say me if you find out something.

EventGroupWaitBits disfunction on tasks with same priority

I am using the v10.2.1 of FreeRTOS on STM32f4 with GCC.

EventGroupWaitBits disfunction on tasks with same priority

Not run the code yet, but a couple of things for you to try upfront. 1) Set configCHECKFORSTACK_OVERFLOW to 2 (you have it set to 1). 2) Define configASSERT() https://www.freertos.org/a00110.html#configASSERT – this is critical while developing, especially if you are learning. Simplest thing to do, assuming you have a debugger can will be able to break a debug session to see that the assert has been hit, is to define it as: ~~~

define configASSERT( ( x ) ) if( ( x ) == 0 ) {

taskDISABLE_INTERRUPTS(); for( ;; ); } ~~~

EventGroupWaitBits disfunction on tasks with same priority

Also, two of your tasks access local variables called ComptFin, and two access a global variable called ComptFin – that may be confusing things. In your original post you said the event bit was getting set, but the task was not running – how did you determine that was the case?

EventGroupWaitBits disfunction on tasks with same priority

The problem with your code is that you create ComptFin_Group in the task DetectLimitReach and all three Raz tasks use that. Since all of these tasks are of same priority, there is no guarantee which one of them will run first.
  • If DetectLimitReach runs first, it will create the event group first and everything will work fine.
  • If any of the Raz task runs first, it will call xEventGroupWaitBits Or xEventGroupSetBits with NULL and that will result in a crash.
You need to move the creation of the event group before starting the scheduler. I made this change and then I am able to successfully run your code (on a different STM32 MCU though). ~~~ ComptFinGroup = xEventGroupCreate(); //nom du EventGroup if (ComptFinGroup == NULL) { while (1); }//fatal error
/* Start the RTOS Scheduler */
vTaskStartScheduler();
~~~ Some other suggestions:
  • Defining configASSERT will help you detect problems faster. In this specific case you will hit assert in the xEventGroupWaitBits function when you pass NULL for the EventGroupHandle_t parameter.
  • Although you are not using ComptFin, it is generally better to not have same name for local and global variables.
Thanks.

EventGroupWaitBits disfunction on tasks with same priority

Thx for the answers. First, configASSERT is already defines in the freeRTOS.h by default, I just looked at it. Second, I see the problem of my EventGroup definition, but I still don’t get why I reached my result : 2 out of 3 RaZ functions are working. And, modifying the IncVar’s priority fix the problem, I don’t understand what is the link with the EventGroup created in the task DetectLimit.

EventGroupWaitBits disfunction on tasks with same priority

Well, your change fix the problem too. Then what’s the link with IncVar and EventGroup since those 2 changes can fix the problem ? You spoke about if any RaZ worked before the Detect task it would not work, but then why does the IncVar priority can solve it ?

EventGroupWaitBits disfunction on tasks with same priority

If you are using unmodified kernel code then configASSERT() is not defined unless you define it yourself in FreeRTOSConfig.h. If it is left undefined then the kernel code simply defines it to nothing.