Best way to use FreeRTOS message system

grantkbrown wrote on Wednesday, October 22, 2008:

Hi List

I have the following function that is designed to send a message via the FreeRTOS message system to my I2C module.

It then waits until the message has been processed and then retrieves the results.

My question is,

Is this the best way in which to do this or is there a better more efficient way in which to achieve the above function.

void Load_Current_I2C_GPO_Register(void)
{
I2C_CmdMsg TheMsgStruct;  
I2C_CmdMsg *TheMsgPtr;

TheMsgStruct.I2C_Status = I2C_DoCmd;  // set the command status

TheMsgPtr = &TheMsgStruct; // create a pointer to the message structure

    if (xQueueSendToBack( TheI2C_Queue, ( void * ) TheMsgPtr, ( portTickType ) 10 )!= pdPASS)
    SPI_DtaMessage->I2C_Status = I2C_CmdError;// message did not get posted
    else
    {    // the message was posted successfully
    // wait until the I2C command has been processed
    while(TheMsgStruct.I2C_Status == I2C_DoCmd);
    // Load the new GPO register data from the message structure
    GPO_0_Byte = TheMsgStruct.Byte_1;
    GPO_1_Byte = TheMsgStruct.Byte_1;
    SPI_DtaMessage->I2C_Status = I2C_CmdDone;  
    }
}

Kind Regards
Grant Brown

davedoors wrote on Wednesday, October 22, 2008:

Without examining too much a couple of points.

Be careful passing a pointer to a stack variable. In this case it is fine as the variable remains valid until after you have waited for the result.

You are polling for the result which will only work in the task processing the message has a higher priority than the task running this code. Consider using a blocking mechanism like a semaphore, that will put task to sleep until there is actually some processing to do. That would be much more efficient.