AnsweredAssumed Answered

FreeRtos memory management in interrupt routine

Question asked by Lukasz Przenioslo on Jul 28, 2015
Latest reply on Jul 30, 2015 by Lukasz Przenioslo
Hello there,
I have a question regaring HAL interrupt routine and freeRtos ST layer. Here is my CAN RX routine.

void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* CanHandle)
{
    osMessagePut(g_canRxMessageQueue, CanHandle->pRxMsg, 0);

    /* Receive */
    if(HAL_CAN_Receive_IT(CanHandle, CAN_FIFO0) != HAL_OK)
    {
        /* Reception Error */
        return;
    }
}


I insert a pointer to a to a previously defined CanRxMsgTypeDef struct. I then retrieve it in another task:

void task_CanRxQueue(void const * argument)
{
    osEvent status;

    while(1)
    {
        status = osMessageGet(g_canRxMessageQueue, osWaitForever);
        CanRxMsgTypeDef* msgRcv = (CanRxMsgTypeDef*)status.value.p;
    }
}


But this way is not good, as my queue is then really 1 item lenght and new frames comming will overwrite it (all items in queue will point to the same object).

My question is, is it safe and efficient to:
use malloc in the interrupt routine to create a new CanRxMsgTypeDef object,
copy content to it,
put to queue,
deque in another task,
free.

Is there a better way of achieving CAN msg queueing?

Outcomes