Queue reports full : How to implement FreeRTOS queues for CAN messages

Hi ,

I am trying to use FreeRTOs queues to send messages between two task . I have main task which pushes the CAN ID to queue and can monitor task which pops this ID and forms the packet and sends it to the CAN driver .

I have push these messages from Main task and pop these from CAN task and form the packet

#define MAX_MESSAGES (0x16)
typedef enum {
    MSG_ID_PACKET1 = 0x200,
   MSG_ID_PACKET2,
    MSG_ID_PACKET3,
    MSG_ID_MAX,
    }msg_id;

So I have created queue which is of
queue_init(sizeof(msg_id), (sizeof(msg_id) * MAX_MESSAGES));
(The above function internally uses FreeRTOS xQueueCreate.

The problem I am facing is after it reaches ((sizeof(msg_id) * MAX_MESSAGES)) the queue is returning full . Do I have to clear the queue to continously send the messages or how do I do it?

Also my task priorities are as below
Main Thread 34
CAN thread 24
Task A 24
Task B 25
I am using stm32 and CMSIS API

If the CAN task really pops the messages (i.e. xQueueReceive messages) and is able to cope with the message rate and of course gets a sufficient share of the CPU to process the messages, the queue doesn’t and shouldn’t get full.
Otherwise you probably have an overall design problem because you’ll loose messages.
Do the other tasks also block on/wait for something (to give up the CPU) ?
Especially the higher or equal prio tasks.

The other task of equal priority does not wait or block. Do you think the main thread should wait until CAN thread pops the message out from queue?

In practice, all tasks need some share of the CPU if they are to perform any work.
If the Main thread is putting in messages faster than the CAN thread can process it then ,as Hartmut rightly pointed out, you’ll loose messages.
This will be exacerbated by the fact that the Main thread has higher priority than the CAN thread.

To answer your question: if you do not want to loose messages, then your main thread should give some leeway to the CAN thread. If the Main thread can also block on the queue.

Thanks,
Aniruddha

At least the tasks Main and B should also wait for something to allow the tasks CAN and A to run at all because they have a lower prio and wouldn’t get any CPU time otherwise.
How does the Main task get the MSG_IDs to forward to the CAN task ? It should NOT poll something to do so but wait for an event to retrieve it as Aniruddha already pointed out (e.g. a task notification, a semaphore or also a queue).

Basic Rule of Thumb, ALL Tasks, (except possibly Idle priority tasks) need to Block or wait on something at least occasionally.

If not, then lower-priority tasks are starved.