Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Loading

Passing a function pointer in a message queue

Posted by moto95 on April 3, 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.


Passing a function pointer in a message queue

Posted by dumarjo on April 3, 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 > https://sourceforge.net/p/freertos/discussion/382005/ > > To unsubscribe from further messages, please visit > https://sourceforge.net/auth/subscriptions/ > > > -- > 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. http://www.avast.com


Passing a function pointer in a message queue

Posted by richard_damon on April 3, 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);


Passing a function pointer in a message queue

Posted by moto95 on April 3, 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 );

Passing a function pointer in a message queue

Posted by moto95 on April 3, 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();


[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]


Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


Careers

FreeRTOS and other embedded software careers at AWS.



FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Renesas

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner