Queue Task Crashing - Please Help

groger57 wrote on Thursday, October 22, 2015:

Hi,

My function and/or call to the queue task is crashing, and I cannot understand why. I am using the same, identical code in other applications and it works fine there. I checked the FreeRTOS config file, and all looks OK, including stack size. This is being compiled in Atollic TrueStudio. Please see below for how I am using the calls.
I have tried queueing messages from different tasks, but the same result. The task I am currently calling it from cycles every 225ms, and if there’s a new message to send, it places it (or tries to) on the queue. From the debug code, it never reaches there, as it appears to crash somewhere between the start of the scheduler and before queueing the message. I have also set the queue delay to some other value, i.e., ( TickType_t ) 10, but no luck there. It must be crashing before it even gets to the first call to queue, because it never prints the message just before it - see code.
FreeRTOS version 8.2.0

Can someone please advise as to where the issue might be? I really need this to work, and I can’t beleive that it is not functioning as it should - again, having used it in other applications I know it works very well.

Thank you!

//message task will wait forever for queue to clear
#define rxQueueRATE ( portMAX_DELAY )
static void vTxQueueFunction( void *pvParameters );
QueueHandle_t xQueueTask;

 //create transmit queue
 xQueueTask = xQueueCreate( 1, sizeof(uint8_t) );
 if(xQueueTask == 0) 
	 printf("Failed to create xQueueTask\n");
 if( xTaskCreate( vTxQueueFunction, "rxDataQueue", 80, NULL, 2, &xQueueTask ) != pdPASS )
	 printf("Cannot create queue task\n");
 printf("Scheduler starting, free heap size = %i\n", xPortGetFreeHeapSize());
 vTaskStartScheduler();
 while(1)
 {
     i++;
 }
 
 ///* queue function *////

static void vTxQueueFunction( void *pvParameters )
{
uint8_t rxValue;
portBASE_TYPE xStatus;
for(;:wink:
{
if( uxQueueMessagesWaiting( xQueueTask ) != 0 )
{
///printf(“Queue should be empty, but %i messages in queue\n”, uxQueueMessagesWaiting( xQueueTask ) );
}

    xStatus = xQueueReceive( xQueueTask, &rxValue, rxQueueRATE );

    if( xStatus == pdPASS )
    {
        printf("Rcvd in signal active queue: %i\n", rxValue);

        if( rxValue == 1 )
        {
        	//do this
        	printf("Received 1 in message queue\n");

        }
        else if(rxValue == 2)
        {
        	//do that
        	printf("Received 2 in message queue\n");
        }
        else
        {
        	printf("received junk\n");
        }
    }
    else
    {
        printf("Could not receive from queue\n");
    }


}

}

/* the call to queue a message */
uint8_t typeVal = 1;
printf(“Going to queue\n”);
if( xQueueSendToBack(xQueueTask, &typeVal, rxQueueRATE ) != pdPASS )
{
printf(“Failed to insert to queue\n”);
}
else
{
printf(“Queued\n”);
}

heinbali01 wrote on Thursday, October 22, 2015:

Hi Roger,

There is probably a typo in your code:

	//create transmit queue
	xQueueTask = xQueueCreate( 1, sizeof(uint8_t) );
	if(xQueueTask == 0)
	{
		printf("Failed to create xQueueTask\n");
	}
    if( xTaskCreate( vTxQueueFunction, "rxDataQueue", 80, NULL, 2, &xQueueTask ) != pdPASS )

You are passing the address of ‘xQueueTask’ to xTaskCreate(), which wlll fill that variable with the task handle. See tasks.h for the correct syntax of xTaskCreate()

Regards.

heinbali01 wrote on Thursday, October 22, 2015:

Hi Roger,

A bit more about your code fragment.

Here below the queue handle is passed as a task parameter to the vTxQueueFunction.
You can pass any data of the size of a pointer. In fact TaskHandle_t is also a pointer.

It also declares TaskHandle_t xTaskHandle. The address of this handle is passed to xTaskCreate which fills in the handle of the newly created task.

//message task will wait forever for queue to clear
#define rxQueueRATE      portMAX_DELAY

static void vTxQueueFunction( void *pvParameters );
TaskHandle_t xTaskHandle;

int main()
{
QueueHandle_t xQueueHandle;

    //create transmit queue
    xQueueHandle = xQueueCreate( 1, sizeof(uint8_t) );
    xTaskCreate( vTxQueueFunction, "rxDataQueue", 80,
        ( void * ) xQueueHandle, 2, &xTaskHandle );

    vTaskStartScheduler();

    while(1)
    {
        i++;
    }
}

 /* queue function */

static void vTxQueueFunction( void *pvParameters )
{
uint8_t rxValue;
portBASE_TYPE xStatus;
/* The queue handle has been passed as a task parameter. */
QueueHandle_t xQueueHandle = ( QueueHandle_t ) pvParameters;

    for(;;)
    {
        xStatus = xQueueReceive( xQueueHandle, &rxValue, rxQueueRATE );
        if( xStatus == pdPASS )
        {
        }
    }
}

Regards.

groger57 wrote on Friday, October 23, 2015:

Fantastic, thanks for your help. Stupid me…I was using the queue handle in the task, no wonder it was breaking!