cancel
Showing results for 
Search instead for 
Did you mean: 

I want to go with a state machine pattern. Is using freeRTOS Queues to send event messages between tasks?

LMorr.3
Senior II

For example, if I have 3 freeRTOS tasks running, each watching a common stateQueue. Tasks take action when they get a new stateQueue item, and potentially trigger state transitions by adding to the stateQueue, but a tasks only takes action if the stateQueue item is 'addressed' to it. Tasks ignore stateQueue items not addressed to them. Once a tasks takes action, it removes the item from the stateQueue. Is there a better way to implement this pattern?

3 REPLIES 3
Artur IWANICKI
ST Employee

Hello. Once using common queue for few tasks and all of those tasks are blocked waiting for data in the queue we should remember that an access to the queue will be given to the task with highest priority and in case of equal priority the length of waiting time is used. In such a case it would be quite difficult to have fully deterministic system.

Please consider ThreadFlags (CMSIS_OS naming convention) to organize direct task2task communication (in FreeRTOS API it is xTaskNotify set of function).

Another option could be usage of Event Flags (CMSIS_OS v2 notification, Event Groups in FreeRTOS API). This option requires creation of OS object which can be shared among tasks. You can have a look at our "FreeRTOS on STM32" session for more info (especially parts 13, 15 and 16).

https://www.st.com/content/st_com/en/support/learning/stm32-education/stm32-moocs/freertos-common-microcontroller-software-interface-standard-osv2.html

LMorr.3
Senior II

Thank you for steering me in the right direction, as well as the online course link. That will be helpfull.

Best,

Piranha
Chief II

Indeed flags/notifications are more preferable, but if you still need a queues, consider a separate queue for each task, not a single one for all of them.