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


Data corrupted in queues between ISR and FreeRTOS task

Posted by jiricharvat on June 15, 2015

Hello, I would like to use FreeRTOS queues for sending messages between USB Isr() and Rtos Task. But the queues do not seem to work - at higher speeds/volumes the data seems to be inconsistent or corrupted. Namely assert configASSERT(cmd->mtype == UNDEFCMD) fails.

Interrupt routine is using GetFreeCommandFromISR() and HandleCommandFromISR(): ~~~~~~ struct sCmd* GetFreeCommandFromISR() { struct sCmd* cmd; BaseTypet res = xQueueReceiveFromISR( coreFreeCmd, &cmd, NULL ); configASSERT(res == pdPASS); configASSERT(cmd->mtype == UNDEFCMD); return cmd; } //Now the interrupt routine fills data received via USB into sCmd structure and //enqueues it into coreQueue for processing in Rtos task void HandleCommandFromISR(struct sCmd* c) { BaseTypet res = xQueueSendToBackFromISR( coreQueue, &c, 0U ); configASSERT(res == pdPASS); }

The RTOS task is using this main loop: for( ;; ) { /* Wait until something arrives in the queue - this task will block / while(pdPASS == xQueueReceive( coreQueue, &cmd, portMAX_DELAY ) ) { / ProcessCoreTaskCommand */ ProcessCoreCommand(cmd);

    BaseType_t res;
    do {
        struct sCmd\* cmd2 = cmd;
        cmd2->m_type = UNDEF_CMD;
        res = xQueueSend( coreFreeCmd, &cmd2, 0U );
    } while(res != pdPASS);

} ~~~~~~

Hardware: SmartFusion2 StarterKit, SoftConsoleIDE 3.4 FreeRTOS: 8.2.1 (having the same with 8.1.2) I have a full demo of this using FreeRTOSV8.2.1FreeRTOSDemoCORTEXSmartFusion2M2S050_SoftConsole (is it possible to attach it to the forum?) Because of the issue I am considering to use the new Task notifications instead. Thanks for any comment.

Data corrupted in queues between ISR and FreeRTOS task

Posted by richard_damon on June 15, 2015

Queue are not best for high volume as they copy the data into and out of the queue, but that shouldn't cause corruption. I think that the code show is a bit corrupted as the code seems written as if cmd is a pointer to a struct sCmd, but the code reads as a struct sCmd.

I do note that in GetFreeCommandFromISR has the asserts backwards, if the QueueReceive fails then you first test for the cmd being right before checking that you GOT a cmd. The issue could well be that you are running out of buffers.

I note that you aren't using a "higher task was woken" flag in your queue operations, which says that likely your ISR isn't triggering the scheduler, which says that the receive task won't wake up until the next timer tick, which might cause buffer exhaustion issues at higher data rates.

Data corrupted in queues between ISR and FreeRTOS task

Posted by jiricharvat on June 16, 2015

Thanks a lot for the comments >> the code seems written as if cmd is a pointer to a struct sCmd, but the code reads as a struct sCmd ..The star * was missing in the displayed code, so it is now corrected. The queues are copying only pointers to sCmd (the sCmd has a buffer of 512 bytes internally).

>> GetFreeCommandFromISR has the asserts backwards Ok, thanks. In actual code there was first assert for res == pdPASS, and then cmd->mtype == UNDEFCMD. Again corrected in sample above.

>> "higher task was woken" Great, I will learn how to use it.

Most likely I will have to dig inside the queues with debugger or printouts to understand what is going on... But first I will try to use the Task notifications instead to have something working.

[ 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