Quality RTOS & Embedded Software

NOTE:This is a read only archive of threads posted to the FreeRTOS support forum. Use these archive pages to search previous posts. New forum support threads can be started at the FreeRTOS forums.

FreeRTOS Support Archive

The FreeRTOS support forum can be used for active support both from Amazon Web Services and the community. In return for using our software for free, we request you play fair and do your bit to help others! Sign up for an account and receive notifications of new support topics then help where you can.

This is a read only archive of threads posted to the FreeRTOS support forum. Use these archive pages to search previous posts. New forum support threads can be started at the FreeRTOS forums.

Send a struct through Queue

Hi there
I am new to this forum and I am already bring some problems that I have been facing while exploring FreeRTOS on my STM32F4:
I want to pass a simple structure from one task to anoder. Actually am I followyig by the book (I think….) what is told on FreeRTOS API suport:
This is shortest example fo what I am doing:

It seems I am saving the data on a Global variable (structure) and not sending a pointer to a strcuture – one of the 10 that was created when I called xQueueCreat.
I tryed not using pointer on the TASK_B but it receives trash.
Any help will be highly appreciated
Thanks

Send a struct through Queue

In your code you are only creating one structure, then sending pointers to that one structure through the queue. All the pointers will be pointing to the same structure, and reading anything out from the structure will result in whatever value was last written to the structure by any task (all tasks are accessing the same one). Is this correct?
If the structure is small you might be better of sending the structure itself, so each time you send the structure to the queue you send a copy of a strcture, rather than a pointer to a single structure. So the queue would be created as:
// The * has been removed
xQueueENCODERReadings = xQueueCreate( 10 , sizeof( struct ENCODERMotion) );
Then Task a can do this to send a copy of its local variable, rather than a pointer to it, to the queue

I think if you wanted to queue pointers, maybe for efficiency reasons, then you would have to have a set of structures available that could be pointed to, so no two tasks point to the same structure at the same time – or perhaps dynamically allocate a structure then send a pointer to the dynamically allocated structure, and have the receiving task free the dynamically allocated structure again after it has received it.
Regards.

Send a struct through Queue

Thank you so much for your reply.
Your solution worked perfectly! Thank you again.
Indeed I was intending to use pointers due to efficiency issues. Actually the example above was the smallest and maybe the simpliest situation.
This is the first time I saw this kind of buggy behaviour – accessing globaly to the same structure through those pointers which were indeed pointing precisely to the same strucutre.
In my aplication I need to pass arge structures from TASKA, TASKB and TASKC to TASKD and this last one will gather all information and process it.
I was trying to use a single Queue, common to all tasks A, B and C, which will be used to send the data to task D.
The strucutre has a sort of task ID to identify from where that information came.
So please confirm if I undertood it right: it is recomended to create several structures associated to each task A, B and C. Then send each pointer through the same queue to task D.
Thank you once again for your important support
Regards

}
To send a pointer to a structure, call get_struct to get a struct from the pool, fill the struct with data, then send a pointer to the struct to the queue.
At the other end of the queue, read out the data from the struct, then call free_struct() to return the stuct to the pool.
This implementation is very crude but ok if MAX_STRUCTS is small.

Send a struct through Queue

If each task is sending the same sort of data, there is no need for multiple queues, and in fact multiple queues can make you life harder. My general rules as each source of asyncronous data should have a single task dedicated to it.
Whether to send a structure or a pointer is largely dependent on how much data is in the structure. Two floats isn’t bad. Sending structures is simpler in that you normally don’t need to deal with life management of the data structures. Passing pointers may be quicker if there is much data in the structure, OR if the structure deals with pointers to itself or other data buffers.
(And when dealing wth pointers to buffers, I find the simplest way to keep track of the free buffers is to put the pointers into another queue, that you take from to get a buffer and put to in order to free a buffer)