# FreeRTOS queue bug?

**URL:** <https://forums.freertos.org/t/freertos-queue-bug/6271>\
**Category:** Kernel\
**Created:** [November 15, 2016, 5:47pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271 "2016-11-15T17:47:32Z")\
**Posts on this page:** 11\
**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:** [November 15, 2016, 5:47pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/1 "2016-11-15T17:47:32Z")

</div>

**[paw3l](https://sourceforge.net/u/paw3l/)** wrote on [Tuesday, November 15, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#c0c0):

Hi All,

I have some problem with queue which I cannot resolve.

```auto
TaskA()
{
   ...
   xQueuePeek(Q, wait_forever);
   xQueueReceive(Q, 0);
   xQueueReceive(Q, 0);
}

ISR(void)
{
    xQueueSendFromISR(Q);
}

```

I can see that if I get a lot if IRQs then taskA gets stuck at queuePeek even though  
Q-\>uxMessagesWaiting is non zero.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 15, 2016, 7:06pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/2 "2016-11-15T19:06:46Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, November 15, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#c0c0/5c71):

Which MCU are you using? That makes a big difference to the what might  
be suggested.

Which version of FreeRTOS are you using? That makes a difference to the  
diagnostic information the RTOS can provide through assert points.

How do you know the interrupt is still firing, and Q-\>uxMessagesWaiting  
is not zero? Is that an assumption, or have counted the interrupts that  
are occurring and viewing Q-\>uxMessageWaiting using the debugger?

([http://www.freertos.org/FAQ-how-to-use-the-FreeRTOS-support-forum.html](http://www.freertos.org/FAQ-how-to-use-the-FreeRTOS-support-forum.html))

Do you have configASSERT() defined, and stack overflow detection turned  
on? ([http://www.freertos.org/FAQHelp.html](http://www.freertos.org/FAQHelp.html))

In the actual code, are you making use of the last parameter in the call  
to xQueueSendFromISR() to request a context switch from the ISR? See  
the example code on the following page: [http://www.freertos.org/a00119.html](http://www.freertos.org/a00119.html)

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 15, 2016, 11:43pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/3 "2016-11-15T23:43:01Z")

</div>

**[paw3l](https://sourceforge.net/u/paw3l/)** wrote on [Tuesday, November 15, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#acf0):

I’m using CM4. Version 7.x (I’ll check exactly tomorrow). For checking the uxMessagesWaiting I use a debugger. I use configASSERT() and stack overflow (2) is used. Last parameter to the xQueueSendFromISR() is set to NULL (I remember the version we use allows to use NULL).

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 15, 2016, 11:52pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/4 "2016-11-15T23:52:00Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Tuesday, November 15, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#acf0/473f):

Most likely problem is an interrupt priority misconfiguration. This is  
a very common (the most common) cause of support requests, so while its  
not certain, it is a good bet. Later versions of FreeRTOS (the current  
is V9.0.0) have much more configASSERT() statements to try and trap just  
that error. I would recommend updating your FreeRTOS version to a newer  
version as a first step to see if any asserts get hit.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 16, 2016, 8:50am UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/5 "2016-11-16T08:50:10Z")

</div>

**[paw3l](https://sourceforge.net/u/paw3l/)** wrote on [Wednesday, November 16, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#acf0/473f/04b7):

```auto
/* The lowest interrupt priority that can be used in a call to a "set priority"
function. */
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0xf

/* The highest interrupt priority that can be used by any interrupt service
routine that makes calls to interrupt safe FreeRTOS API functions. DO NOT CALL
INTERRUPT SAFE FREERTOS API FUNCTIONS FROM ANY INTERRUPT THAT HAS A HIGHER
PRIORITY THAN THIS! (higher priorities are lower numeric values. */
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY	5

/* Interrupt priorities used by the kernel port layer itself. These are generic
to all Cortex-M ports, and do not rely on any particular library functions. */
#define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )
/* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!!
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
#define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) )

```

And the IRQ priority is set to 6 with:

```auto
    NVIC_SetPriority( UART0_IRQn, UART_RX_INTERUPT_PRIORITY );

```

The system is actually alive, meaning other tasks are running (few of them). And when I investigated the ‘suspended list’ I could find my TaskA on that list.  
Upgrading the FreeRTOS to latest version is out of question, since we are in final stage of the development. We rather want to get to the bottom of this issue.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 16, 2016, 3:14pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/6 "2016-11-16T15:14:53Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Wednesday, November 16, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#acf0/473f/04b7/a1b4):

Umm, am I missing something, or did you forget to say what  
UART\_RX\_INTERUPT\_PRIORITY is?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 17, 2016, 1:27pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/7 "2016-11-17T13:27:30Z")

</div>

**[paw3l](https://sourceforge.net/u/paw3l/)** wrote on [Thursday, November 17, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#acf0/473f/04b7/a1b4/e96a):

```auto
And the IRQ priority is set to 6

```

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 17, 2016, 3:10pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/8 "2016-11-17T15:10:14Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, November 17, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#acf0/473f/04b7/a1b4/e96a/49fb):

That would seem to be right. Check the value of configPRIO\_BITS. Is it  
hard coded or is it set to a value read from a header file? As we have  
known the header files provided by vendors to be wrong before.

Please also post all the code for the ISR.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2016, 8:13am UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/9 "2016-11-18T08:13:06Z")

</div>

**[paw3l](https://sourceforge.net/u/paw3l/)** wrote on [Friday, November 18, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#f8c3):

Config prio bits is set to 4.

IRQ bellow:

```auto
{
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;
    uint32_t dw_status;
    uint8_t rx_char;
    uint8_t tx_char;
    Uart *p_uart = (Uart *)CONF_COM_UART;

    dw_status = uart_get_status((Uart *)CONF_COM_UART);
    if ( dw_status & UART_SR_RXRDY )
    {
	RS422_RX_LED_ON;
        xTimerResetFromISR( xTimerRx, &xHigherPriorityTaskWoken );

        uart_read( (Uart *)CONF_COM_UART, &rx_char );
        if ( RDI_DFT_ERROR_BUF_OVERFLOW == ProcessChar( (char) rx_char ) )
	{
	    p_uart->UART_IDR = UART_IDR_RXRDY | UART_IDR_OVRE;
	    vDftFlushQueueFromISR();
	}
        uart_com.rx_total++;
        frame_rx_ok_led = true;
    }
    
    if ( dw_status & UART_SR_OVRE )
    {
        SetRxUartFIFOOverflowStatusBit();
    }

    if ( dw_status & UART_SR_TXRDY )
    {
	if (vDftGetBufferChar( &tx_char ) )
	{
	    RS422_TX_LED_ON;
	    xTimerResetFromISR( xTimerTx, &xHigherPriorityTaskWoken );
	    p_uart->UART_THR = tx_char;
	    p_uart->UART_IDR = UART_IDR_TXRDY;
	    uart_com.tx_total++;
	    frame_tx_ok_led = true;
	}
    }
  
    uart_reset_status((Uart *)CONF_COM_UART);

    portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
    }

```

The SendFromISR is in ProcessChar after some check.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 18, 2016, 6:15pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/10 "2016-11-18T18:15:14Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, November 18, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#f8c3/3096):

> Config prio bits is set to 4.

I’m afraid I can’t do anything with that information - first because you  
didn’t say which MCU you were using when asked, and second it will be up  
to you to determine if this is correct. We have seen the value being  
wrong in both the header file and user manuals provided by MCU vendors,  
so some times you need to figure it out yourself (which can be done by  
writing 255 into a priority register, then reading the value back, to  
see how many bits ‘stick’ at 1 and how many are read back as 0 - I think  
the very latest versions of FreeRTOS will validate this automatically if  
configASSERT() is defined, but it might be that only the head revision  
in SVN does it).

> IRQ bellow:

There is nothing obviously wrong in your code, provided uart\_read() is  
doing nothing more than reading a character you know is already  
available from a register.

It might be an idea to create a very simple test that does not touch the  
UART peripheral, but instead sends dummy characters to the queues from a  
timer interrupt, or something similar, so you can see if you can  
replicate the issue using basic dummy data from a simple ISR that just  
has a few lines of code in it.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 18, 2016, 8:04pm UTC](https://forums.freertos.org/t/freertos-queue-bug/6271/11 "2016-11-18T20:04:55Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, November 18, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/e5f9c6e6/#9d5a):

Can you please show the ProcessChar() and vDftFlushQueueFromISR() code.
