<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>FreeRTOS Queues seem broken.</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/13247/freertos-queues-seem-broken</link><description>Hi, 
 The xQueueReceive function in FreeRTOS seems to be broken, when a non-zero timeout is specified. 
 Example, I&amp;#39;m trying to create and ordered output mechanism for the UART using a task and a FIFO queue: 
 static void uart_output_task(void * arg</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sun, 17 Apr 2016 11:57:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/13247/freertos-queues-seem-broken" /><item><title>RE: FreeRTOS Queues seem broken.</title><link>https://devzone.nordicsemi.com/thread/50529?ContentTypeID=1</link><pubDate>Sun, 17 Apr 2016 11:57:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:53e4c53f-0531-437b-8ba0-fdc6f0e53f06</guid><dc:creator>maxv</dc:creator><description>&lt;p&gt;Unbelievable. The problem was that the default &lt;code&gt;configTOTAL_HEAP_SIZE&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#define configTOTAL_HEAP_SIZE                                                     ( 4096 )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Breaks after only 2 user tasks have been implemented. Trying to start a third task causes &lt;code&gt;vTaskStartScheduler&lt;/code&gt; to fail.&lt;/p&gt;
&lt;p&gt;Reconfiguring this, for now, to:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;#define configTOTAL_HEAP_SIZE                                                     ( 8192 )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Fixes the problem.&lt;/p&gt;
&lt;p&gt;So it&amp;#39;s not a problem in the Queue. Thanks for the help guys.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the sample code with &lt;code&gt;gdb&lt;/code&gt; trace:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;/**@brief Function for application main entry.
 */
int main(void)
{
    // Do not start any interrupt that uses system functions before system initialisation.
    // The best solution is to start the OS before any other initalisation.

    // Init a semaphore for the BLE thread.
    m_ble_event_ready = xSemaphoreCreateBinary();

    if(NULL == m_ble_event_ready)
    {
        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }
    
    m_uart_output_queue = xQueueCreate(5, sizeof (struct SerialPortOutput));
    
    if (m_uart_output_queue)
    {
        printf(&amp;quot;UART output queue created successfully!\n&amp;quot;);
    }
    else
    {
        printf(&amp;quot;UART output queue could not be created!\n&amp;quot;);    
    }

    // Start execution.
    if(pdPASS != xTaskCreate(ble_stack_thread, &amp;quot;BLE&amp;quot;, 256, NULL, 1, &amp;amp;m_ble_stack_thread))
    {
        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }

    nrf_drv_uart_config_t uart_config = NRF_DRV_UART_DEFAULT_CONFIG;

    if (NRF_SUCCESS == nrf_drv_uart_init(&amp;amp;uart_config, uart_event_handler))
    {
        printf(&amp;quot;UART initialized properly.\n&amp;quot;);
    }
    else
    {
        printf(&amp;quot;UART failed to initialize properly.\n&amp;quot;);
    }
    
    if (pdPASS != xTaskCreate(uart_output_task, &amp;quot;UART Output Task&amp;quot;, 256, NULL, 1, &amp;amp;m_uart_output_thread))
    {
        printf(&amp;quot;Out of memory.&amp;quot;);
        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }

    if (pdPASS != xTaskCreate(uart_test_task, &amp;quot;Test UART Task&amp;quot;, 256, NULL, 1, &amp;amp;m_uart_test_task))
    {
        printf(&amp;quot;Out of memory.&amp;quot;);
        APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
    }
    
    /* Activate deep sleep mode */
    // SCB-&amp;gt;SCR |= SCB_SCR_SLEEPDEEP_Msk;

    // Start FreeRTOS scheduler.
    vTaskStartScheduler();

    while (true)
    {
        printf(&amp;quot;Oops.\n&amp;quot;);
        APP_ERROR_HANDLER(NRF_ERROR_FORBIDDEN);
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;gdb&lt;/code&gt; output is:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Breakpoint 1, main () at /home/max/nRF5_SDK_11.0.0_89a8197/examples/ble_peripheral/ble_app_hrs_freertos/main.c:1205
1205	{

(gdb) break vTaskStartScheduler
Breakpoint 2 at 0x1e2c0: file /home/max/nRF5_SDK_11.0.0_89a8197/external/freertos/source/tasks.c, line 1544.
(gdb) continue
Continuing.

Breakpoint 2, vTaskStartScheduler () at /home/max/nRF5_SDK_11.0.0_89a8197/external/freertos/source/tasks.c:1544
1544	{
(gdb) n
1552			xReturn = xTaskCreate( prvIdleTask, &amp;quot;IDLE&amp;quot;, tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &amp;amp;xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
(gdb) n
1565				xReturn = xTimerCreateTimerTask();
(gdb) n
1574		if( xReturn == pdPASS )
(gdb) n
main () at /home/max/nRF5_SDK_11.0.0_89a8197/examples/ble_peripheral/ble_app_hrs_freertos/main.c:1304
1304	        printf(&amp;quot;Oops.\n&amp;quot;);
(gdb) 
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS Queues seem broken.</title><link>https://devzone.nordicsemi.com/thread/50528?ContentTypeID=1</link><pubDate>Fri, 15 Apr 2016 17:27:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8be5df3b-4233-4629-91d0-2837890da890</guid><dc:creator>www.FreeRTOS.org</dc:creator><description>&lt;p&gt;The first thing is - what is the system doing when its apparently stuck in xQueueReceive().  Are other tasks running.  If you pause the debugger, what is executing?  Are you sure its not in a &lt;a href="http://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html"&gt;fault handler&lt;/a&gt;?&lt;/p&gt;
&lt;p&gt;Are you sure the tick interrupt is executing?  If you put a break point in xTaskIncrementTick() in FreeRTOS/Source/tasks.c, does it ever get hit?  If the tick count is not increment the timeout will never occur.&lt;/p&gt;
&lt;p&gt;How is your printf() implemented.  That can often cause in small microcontroller systems, especially when it is used in more than one task, or uses semihosting (which will conflict with the kernel interrupts).&lt;/p&gt;
&lt;p&gt;Have you checked for &lt;a href="http://www.freertos.org/Stacks-and-stack-overflow-checking.html"&gt;stack overflow&lt;/a&gt;?  Do you have &lt;a href="http://www.freertos.org/a00110.html#configASSERT"&gt;configASSERT()&lt;/a&gt; defined?  Etc.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>