Quality RTOS & Embedded Software

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




Loading

Reading from message queue crashes

Posted by specialk on September 22, 2009
I'm trying to set up a task that will regularly check the rx message queue on uart, and I have the ISR working properly, but whenever I try to read the contents of the rx message queue I receive the character, but the mcu crashes immediately afterwards. Also, if no character is input before xQueuePeek times out (500 ticks in this case) it will also crash. If I remove the code to check the rx queue, it works fine and happily writes to the uart every 100 ticks as expected.

I just ordered the freertos book (hopefully I'll get it today), but if there something that I'm doing in this task code that I obviously shouldn't be? (All I'm trying to do it send the char back out to the UART just to see if it works or not.)

static portTASK_FUNCTION(vUART1Task, pvParameters __attribute__((unused)))
{
static xQueueHandle rxQueue = NULL;
uart1GetRxQueue (&rxQueue);
signed portCHAR rxChar;

for (;;)
{
/* Check if anything is available in the rx queue */
if (rxQueue)
{
if (xQueuePeek(rxQueue, &rxChar, (portTickType)500))
{
/* Print the result on the uart port */
char *feedback;
sprintf(feedback, "Received: %c\r\n", rxChar);
uart1PutString(feedback, 10);
}
}

// Transmit current tick on UART1
char *output;
sprintf(output, "Ticks: %d\r\n", xTaskGetTickCount());
uart1PutString(output, 10);
vTaskDelay(100);
}
}

RE: Reading from message queue crashes

Posted by specialk on September 22, 2009
It seems that it isn't actually the queues, but the sprintf that's causing the problems. The following code seems to work as expected:

/* Check if anything is available in the rx queue */
if (rxQueue)
{
if (xQueueReceive(rxQueue, &rxChar, (portTickType)500)) // portMAX_DELAY))
{
/* Print the result on the uart port */
uart1PutString("Received: ", portMAX_DELAY);
uart1PutChar(rxChar, portMAX_DELAY);
uart1PutString("\r\n", portMAX_DELAY);
}
}


RE: Reading from message queue crashes

Posted by woops_ on September 22, 2009
char *feedback;
With this line you allocate a character pointer, but no memory to hold the string to which it points. The pointer is uninitialized.

sprintf(feedback, "Received: %c\r\n", rxChar);
With this line you are writing a dozen characters into an address pointed to by an uninitialized pointer, so it will more than likely crash.

RE: Reading from message queue crashes

Posted by specialk on September 22, 2009
Ooops ... so busy looking at the queues that I don't pay attention to what's right in front of my eyes :)

/* Print the result on the uart port */
char feedback[80];
sprintf(feedback, "Received: %c\r\n", rxChar);
uart1PutString(feedback, 10);

RE: Reading from message queue crashes

Posted by woops_ on September 22, 2009
that should be better, but make sure you have enough stack space to hold the 80 character array.


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




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

Latest News

FreeRTOS kernel V10 is available for immediate download. Now MIT licensed.


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

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