Post queue by copy, not reference (aka xQueueSend in freertos)

Discussion to talk about software related topics only.
Post Reply
zealott
Posts: 32
Joined: Thu Oct 30, 2008 1:15 am

Post queue by copy, not reference (aka xQueueSend in freertos)

Post by zealott » Tue Nov 23, 2021 3:11 am

Hi,

Any thoughts on the best way to provide a thread-safe way to mimic xQueueSend / xQueueReceive in FreeRTOS?
It copies the message itself, not just a reference as NB Queue functions.

Thanks

User avatar
pbreed
Posts: 1011
Joined: Thu Apr 24, 2008 3:58 pm

Re: Post queue by copy, not reference (aka xQueueSend in freertos)

Post by pbreed » Tue Nov 23, 2021 9:36 am

Use the fifo class, allocate a poolbuffer, copy the data into post to the fifo...
free it on the other end...


#include <buffers.h>
#include <nbrtos.h>

OS_FIFO TheFifo;

//Sending....
PoolPtr pp=GetBuffer();
if((pp) && (messag_len<ETHER_BUFFER_SIZE))
{
memcpy(pp->pData,message,message_len);
pp->usedsize=message_len;
TheFifo.Post(pp);
pp=0; //Make it null so it wont get freed
}
if (pp) FreeBuffeR(pp);


//Receiving:
PoolPtr pp=(PoolPtr) TheFifo.Pend();
if(pp)
{
int len=pp->usedsize;
//Copy out if you want.. or just use the buffer...
//Remember to free it when done
memcpy(destination,pp->pData,len);
FreeBuffer(pp);
}


Something like that...
This code was just typed in, it was not compiled ot tested, could have errors...

zealott
Posts: 32
Joined: Thu Oct 30, 2008 1:15 am

Re: Post queue by copy, not reference (aka xQueueSend in freertos)

Post by zealott » Wed Nov 24, 2021 12:03 am

Brilliant! thanks Paul

Post Reply