Quality RTOS & Embedded Software

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




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) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.


Free TCP/IP and file system demos for the RTOS


Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑


FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists