dciliske wrote:I'm 90% certain that you're either not passing a structure containing the function pointer or you're allocating the structure on the stack in the posting context. Either of these will cause you to get a corrupted memory address for the resulting function pointer.
Take a look at the OSFifo example in the RTOS examples for how to do this correctly.
-Dan
It seems you are right, but I can't seem to figure out how to pass the function pointer without it being allocated on the stack. I've tried passing it with QueueFunction(&Function) and that had no effect. I also tried casting it to an OS_FIFO_EL before passing it and that didn't work either. Maybe I'm misunderstanding something fundamental about passing function pointers. Any ideas?
Code: Select all
void Function()
{
iprintf("print me");
}
void main()
{
QueueFunction(Function);
}
int QueueFunction(void ( *function )())
{
// function(); //function can be called from here successfully
if(!taskInitialized)
{
return NOT_INITIALIZED;
}
OSFifoPost(&functionQueue,(OS_FIFO_EL *) function);
// function(); //function cannot be called from here
return NO_ERROR;
}
I also tried to copy everything exactly from the OSFifo example and it didn't work with functions. Maybe what I'm trying to do isn't possible?
Code: Select all
OSFifoPost(&functionQueue,(OS_FIFO_EL *) &Function);
void ( *myFunction )() = (void (*)()) OSFifoPend(&functionQueue,0);
myFunction();//failed
Update:
I've figured out that the call to OSFifoPost is causing some kind of change in the function pointer. Is this what you would expect to happen? I would have thought that it shouldn't change anything.
Code: Select all
int QueueFunction(void ( *function )())
{
if(!taskInitialized)
{
return NOT_INITIALIZED;
}
// OSFifoPost(&functionQueue,(OS_FIFO_EL *) function);
OSFifoPost(&functionQueue,(OS_FIFO_EL *) &taskInitialized); //post something other than the function pointer
function(); //executes properly now that it isn't being posted to the queue, previously it would fail here
return NO_ERROR;
}