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

Queue help

Posted by tomirtos on October 28, 2014

Hi all!

I am trying to use a qeueu to get data from an interrupt to a task. The interrupt is trigged by the UART. I receive a character there and send it with a queue to a task. The task should evaluate what character it received and according to that should light a led.

In the same task I also have a function that will send back the character that the microcontroller received just to test if the queue was working. I receive the character on my serial monitor no problem. The problem is I start receiving the same character over and over again even if I don't send any new characters. I don't really understand why this is happening, because when I used queues before the same way this never happened. Can anyone please explain this behaviour to me?

this is my task:

~~~~~~~~~~~~~~~~~~~

xQueueHandle UART_queue=0; /******************************************************************************/

void examinertask() { while (1) { char stringreceived;

    if(xQueueReceive( UART_queue , &string_received , 1000))
    {
        if( string_received=='s' )
        {
            LATAbits.LATA0=1;
        }

        else LATAbits.LATA0=0;
    }

    UART1PutChar(string_received);
}

}

~~~~~~~~~~~~~~~~

this is the interrupt handler:

~~~~~~~~~~~~~~~

void attribute((interrupt, noautopsv)) U1RXInterrupt(void) { unsigned char datain = 0;

data_in=U1RXREG; // save register value

xQueueSend(UART_queue , &data_in , 1000); // send data via queue

_U1RXIF = 0; // Clear the UART Recieve Interrupt Flag

}

~~~~~~~~~~~~~~~


Queue help

Posted by tomirtos on October 28, 2014

if I use xQueueSendToFrontFromISR(UARTqueue, &datain, &xHigherPriorityTaskWoken); the same thing is happening


Queue help

Posted by markwrichardson on October 28, 2014

UART1PutChar() will be called with the last character for every loop regardless of an item in the queue. Before your first letter received, it will output whatever is in string_received at initialization.


Queue help

Posted by tomirtos on October 28, 2014

but isnt the task only woken up when there is something in the queue?


Queue help

Posted by rtel on October 28, 2014

send it with a queue to a task.

That is fine for low bandwidth comms, such as receiving key presses, but very inefficient for high bandwidth comms where a circular buffer or DMA is better.

 while  (1)
 {
     char  string_received;

     if(xQueueReceive(  UART_queue  ,  &string_received  ,  1000))
     {
         if(  string_received=='s'  )
         {
             LATAbits.LATA0=1;
         }

         else  LATAbits.LATA0=0;
     }

     UART1PutChar(string_received);
 }

}

If you receive only one character then this code will print out that character every 1000 ticks. That is because you have the queue receive timeout set to 1000, and print the character stored in string_received whether a new value was received on the queue or not. Moving the UART1PutChar() call inside the if() statement would fix that.

void attribute((interrupt, noautopsv)) _U1RXInterrupt(void)

I'm guessing this is a PIC32? Please read the documentation page for this port as the way you are defining your interrupt is going to waste a lot of RAM (although it will work).

 xQueueSend(UART_queue  ,  &data_in  ,  1000);  // send data via queue

xQueueSend() cannot be called from an interrupt and definitely cannot attempt to block in an interrupt. Please use xQueueSendFromISR() instead and be sure to read the FAQs, especially the FAQ "My application does not run, what could be wrong?".

Regards.


Queue help

Posted by rtel on October 28, 2014

but isnt the task only woken up when there is something in the queue?

Please see my reply to your first post - your code is not checking if anything is received or not before just printing out whatever was received last.

Regards.


Queue help

Posted by tomirtos on October 28, 2014

Thank you for the answers, cleared up a lot of things!

I am using a dspic33fj128mc802.

"Please read the documentation page for this port"

http://www.freertos.org/portpic24_dspic.html

This was all I found, but i'm not sure what I should be looking for in here. I don't know any alternative ways of declaring an interrupt handler. Is this only a FreeRTOS concern or if I weren't using FreeRTOS this declaration would still be bad?

Thanks! Cheers


Queue help

Posted by rtel on October 28, 2014

Sorry - I was thinking it was a PIC32 - you can disregard that part of my reply.

Regards.


Queue help

Posted by tomirtos on October 28, 2014

In the dspic demo project in serial.c this is how interrupts are defined, they are the same as mine:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~ void attribute((interrupt, autopsv)) _U2RXInterrupt( void ) { char cChar; portBASETYPE xHigherPriorityTaskWoken = pdFALSE;

/* Get the character and post it on the queue of Rxed characters.
If the post causes a task to wake force a context switch as the woken task
may have a higher priority than the task we have interrupted. */
IFS1bits.U2RXIF = serCLEAR_FLAG;
while( U2STAbits.URXDA )
{
	cChar = U2RXREG;
	xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
}

if( xHigherPriorityTaskWoken != pdFALSE )
{
	taskYIELD();
}

} ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Queue help

Posted by tomirtos on October 28, 2014

Sorry I didn't see your reply!


[ 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