Passing a function pointer in a message queue

moto95 wrote on Thursday, April 03, 2014:

I am trying to pass the pointer to a function via a message queue. The send/receive mechanism is working, i.e. the task waits on a message being received and unblocks when something is in the queue. However, I struggle to then execute the function that the queue contents points to.

Here is my send statement:
xQueueSend( xMsgQueue, ( void * ) &MsgFunction, 0 );

Here is my receive statement (inside the FreeRTOS task):
xQueueReceive( xMsgQueue, &(MsgWaiting), portMAX_DELAY );

And here is the dereferencing of the function pointer to actually call the function:
(*MsgWaiting)();

MsgWaiting is declared as:
void (*MsgWaiting) (void);

MsgFunction is declared as:
void MsgFunction (void);

However, the function never gets called. I’m sure it has something to do with the usage of pointers, or rather incorrect usage of pointers. Anyone mind to point out my mistake?

Many thanks.

dumarjo wrote on Thursday, April 03, 2014:

Hi,

How the queue is created ?

Jonathan
Le 2014-04-03 06:02, Felix Hirzel a écrit :

I am trying to pass the pointer to a function via a message queue. The
send/receive mechanism is working, i.e. the task waits on a message
being received and unblocks when something is in the queue. However, I
struggle to then execute the function that the queue contents points to.

Here is my send statement:
xQueueSend( xMsgQueue, ( void * ) &MsgFunction, 0 );

Here is my receive statement (inside the FreeRTOS task):
xQueueReceive( xMsgQueue, &(MsgWaiting), portMAX_DELAY );

And here is the dereferencing of the function pointer to actually call
the function:
(*MsgWaiting)();

MsgWaiting is declared as:
void (*MsgWaiting) (void);

MsgFunction is declared as:
void MsgFunction (void);

However, the function never gets called. I’m sure it has something to
do with the usage of pointers, or rather incorrect usage of pointers.
Anyone mind to point out my mistake?

Many thanks.


Passing a function pointer in a message queue
https://sourceforge.net/p/freertos/discussion/382005/thread/bc70730a/?limit=25#5400


Sent from sourceforge.net because you indicated interest in
SourceForge.net: Log In to SourceForge.net

To unsubscribe from further messages, please visit
SourceForge.net: Log In to SourceForge.net


This message has been scanned for viruses and
dangerous content by MailScanner http://www.mailscanner.info/, and is
believed to be clean.


Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce que la protection avast! Antivirus est active.

richard_damon wrote on Thursday, April 03, 2014:

The big problem would be you need to pass to xQueueSend the address that contains a buffer with the data to send, NOT the value it self.

Thus you need something like

void (*MsgWaiting)();

MsgWaiting = &MsgFunction;
xQueueSend(xMsgQueue, MsgWaiting);

moto95 wrote on Thursday, April 03, 2014:

This is how I’ve attempted it. Using a PIC32, I assumed a function pointer would be 4 bytes long.

int QUEUELEN = 20;
int MsgIDBytes = 4;
xMsgQueue = xQueueCreate( QUEUELEN, MsgIDBytes );

moto95 wrote on Thursday, April 03, 2014:

Many thanks to Jonathan. I initially created the queue with only 2 bytes for the item size. Once correct to 4 bytes as per my post just above, it worked perfectly! So, it seems all my pointer handling was correct. Even the simpler de-referencing of the function pointer as follows worked equally well:
MsgWaiting();