# How should xQueueSendFromISR and xQueueReceive be used?

**URL:** <https://forums.freertos.org/t/how-should-xqueuesendfromisr-and-xqueuereceive-be-used/3043>\
**Category:** Kernel\
**Created:** [September 15, 2015, 9:41am UTC](https://forums.freertos.org/t/how-should-xqueuesendfromisr-and-xqueuereceive-be-used/3043 "2015-09-15T09:41:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 15, 2015, 9:41am UTC](https://forums.freertos.org/t/how-should-xqueuesendfromisr-and-xqueuereceive-be-used/3043/1 "2015-09-15T09:41:25Z")

</div>

**[adissida](https://sourceforge.net/u/adissida/)** wrote on [Tuesday, September 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/86103680/#0038):

The example projects I have looked at, its simple. To be more shure and understand the xQueue I have read the manual for it and this video guide [https://www.youtube.com/watch?v=8lIpI30Tj-g](https://www.youtube.com/watch?v=8lIpI30Tj-g)

This is my code for setting up the interrupt handler:

* * *

INTC\_register\_interrupt((int\_handler)&trvCLI\_UART\_ISR, AVR32\_USART1\_IRQ, AVR32\_INTC\_INT0);

* * *

then from the example project the portENTER\_SWITCHING\_ISR is needed:

* * *

\_\_attribute((naked)) static void trvCLI\_UART\_ISR( void )  
{  
portENTER\_SWITCHING\_ISR();

```
trvCLI_UART_ISR_Recieve();

portEXIT_SWITCHING_ISR();

```

}

* * *

and at last this function is called after entering switching ISR:

* * *

\_\_attribute((noinline)) static portBASE\_TYPE trvCLI\_UART\_ISR\_Recieve( void )  
{  
/\* Now we can declare the local variables. \*/  
signed portCHAR cChar = 0;  
portBASE\_TYPE xHigherPriorityTaskWoken = pdFALSE;  
unsigned portLONG ulStatus;  
volatile avr32\_usart\_t \*usart = trvCLI\_USART;

```
/* What caused the interrupt? */
ulStatus = usart->csr & usart->imr;

if (ulStatus & AVR32_USART_CSR_RXRDY_MASK)
{
	/* The interrupt was caused by the receiver getting data. */
	cChar = usart->rhr; //TODO

	/* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
	calls in a critical section . */
	portENTER_CRITICAL();
		if (xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken))
		{
			gpio_tgl_gpio_pin(trvLED_GPS);
		}
	portEXIT_CRITICAL();
}

usart->csr;

/* The return value will be used by portEXIT_SWITCHING_ISR() to know if it
should perform a vTaskSwitchContext(). */
return ( xHigherPriorityTaskWoken );

```

}

* * *

I have a simple task, to watch the **xRxedChars** queue:

* * *

void trvUART\_CLI\_RX( void \*pvParameters )  
{  
signed portCHAR receivedChars = 0;  
uint8\_t str[30];  
( void ) pvParameters;

```
for ( ;; )
{
	if (xQueueReceive(xRxedChars, &receivedChars, comRX_BLOCK_TIME))
	{
		sprintf( str,"%d",receivedChars );
		trvCLI_WriteLine( &str );
	}
}
//vTaskDelete(NULL);

```

}

* * *

As soon as I enter something in the console, everything hangs. If I change the xQueueReceive to xQueueReceiveFromISR it doesent hangs but the value is never copied to receivedChars.

Using ATMEL UC3, FreeRTOS V8.2.2

---

<div class="post-metadata">

**Author:** ![htibosch](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/htibosch/32/62_2.png) [@htibosch](https://forums.freertos.org/u/htibosch)\
**Post date:** [September 15, 2015, 2:19pm UTC](https://forums.freertos.org/t/how-should-xqueuesendfromisr-and-xqueuereceive-be-used/3043/2 "2015-09-15T14:19:53Z")

</div>

**[heinbali01](https://sourceforge.net/u/heinbali01/)** wrote on [Tuesday, September 15, 2015](https://sourceforge.net/p/freertos/discussion/382005/thread/86103680/#769f):

From your previous post:

> So it has something to do with usart-\>ier = AVR32\_USART\_IER\_RXRDY\_MASK  
> When I delete that line in the code above, it always works (two LEDs blinking)

When you delete that line there won’t be any RX interrupt.

Your ISR is indeed masking off bits that have not been enabled:

```auto
    /* What caused the interrupt? */
    ulStatus = usart->csr & usart->imr;

```

> Because FreeRTOS is not supposed to run with nested interrupts,  
> put all OS calls in a critical section  
> portENTER\_CRITICAL();  
> if (xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken))  
> portEXIT\_CRITICAL();

I don’t think the critical section is necessary. Interrupts are not enabled (by the global flag) until you enable them.  
And ask your self: will there be another ISR that will try to access your `xRxedChars`? That shouldn’t be.

Note that if you expect lots of data (kilobytes), you better look at other methods like using PDC (DMA) channels.

My guess is that taking away the critical sections will solve the problem.

From `trvUART_CLI_RX()` you will call `xQueueReceive()` (without the FromISR), and `xQueueReceive()` will use critical sections when necessary.

Regards.
