cembeddedglobal-variablesfreertos

FreeRTOS event-task architecture to handle different flags around the code


I'm working on a FreeRTOS project in C/C++ that has about 10 tasks for gpios, displays, wifi, and so on.

These tasks are only triggered by events. For example, the user chooses an option on the menu "connect" and this will trigger an event to the respective task.

What I would like to know is, what is the best way to handle flags around the code?

For example I have some flags: "wifi_check_status", "mqtt_connect", "button_pressed" and so on.

Currently, I'm using a global struct that stores all these flags. Then, the respetive tasks just reads the flag it needs and updates its value.

I have a lot of flags, and 10 tasks that need to read them. Should I be using mutex everytime a task wants to read these flags? or is there an easier method I'm not seeing?

I don't have any problem with the current architecure, but I would like to know if this is the best approach.

Thanks a lot in advance

EDIT

I'm using direct-to-task notifications for communication between different tasks. That helped separate a lot of modules which is great. Nevertheless, I have those flags that are used by different modules and I'm trying to avoid using hundreds of mutex, is there any way?

For example, the wifi task has an event to check the wifi status and reconnect if needed. I trigger this event from a timer. Once the event is done, I update the "wifi_check_status" flag to false inside the task. What other patterns can I use to do this? Similarly, there's another task that reads this flag to decide if it needs to do anything

Another example, is the encoder tasks update a current_menu_option variable based on the number of options in a menu. If the menu has 3 options, the current_menu_option will move between 0 and 2. When the user presses the button, it will trigger the UI task. The UI task prints the menu in the display and resets current_menu_option to 0, because the user starts scrolling always at position 0. Thats why I'm using a global variable in this case, it's very convenient.

How could I do that without a global variable?


Solution

  • I don't have any problem with the current architecure, but I would like to know if this is the best approach.

    It is an example of how to not program using freeRTOS. It is a terrible practice. Use queues, direct to task notifications or event groups (the very worst option) to communicate between tasks

    Using flags and task polling their values indicates that you need to learn IPC (inter process communication) before you start developing freeRTOS applications

    These tasks are only triggered by events. For example, the user chooses an option on the menu "connect" and this will trigger an event to the respective task.

    It has nothing in common with events and your app is not event-driven.

    What I would like to know is, what is the best way to handle flags around the code?

    The best way is to remove them and redesign the application.

    How could I do that without a global variable?

    Uses queues