Electronic – Basic RTOS concept. Tasks communication

freertosrtos

I am learning to use a RTOS, CMSIS RTOS in this case, so my question is basic. I want to know what is the best way to share information between two tasks.

What I am trying to do is basically a controller with some external communication. This system has two tasks.

Task 1. Controller task:

  • It is executed at fixed intervals, each X ms, as fast as possible
  • It has a setpoint
  • It reads several sensors
  • It calculates and sets several outputs

Task 2. Communication task:

  • It’s a slave in a master/slave communication (Modbus). So, it only communicates when it receives a request from master
  • It must be able to set the setpoint
  • It must be able to read the value/status of sensors and outputs

How should these two tasks communicate? If I was not using an RTOS I will use private global variables, wrapped with some public get/set functions. But as far as I understand that is not the way to do it in a RTOS.

Should I use a queue where the communication task writes a request message, that the controller task will check every iteration? The problem I see with this solution is that the controller task must execute as fast as possible, so either the controller task only replies to one request each iteration making the communication task less responsive or, it responses to all the request making its execution time longer.

Best Answer

This is a good way to pass data between tasks, yes. If it's anything like FreeRTOS, you'll also be able to block the receiving task until something is in the queue (if you wish). Or, you can check and drop through or wait for a specified time before dropping through to the next instruction in the task. This blocking, to my mind, is one of the real beauties of running an RTOS. To be able to have one task waiting for another task to pass it data can make your life a lot easier and queues enable this in a thread safe manner.

You also have the facilities to check whether a queue is full before writing to it and, if it is full, the possibility of waiting until there is space before writing to it.

You will have the option, I assume, to write lots of different types of data to the queues, including structures. Queues are very useful and a safe way of passing data.