<?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 power consumption and idle hook</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76680/freertos-power-consumption-and-idle-hook</link><description>Hi, 
 I&amp;#39;m developing a custom application starting from ble_hrm_freertos application. When NRF_LOG is enabled there is no way let the chip in low power consumption. I found the problem related to the following code in logger_thread function 
 
 
 
 
</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 25 Jun 2021 07:38:59 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76680/freertos-power-consumption-and-idle-hook" /><item><title>RE: FreeRTOS power consumption and idle hook</title><link>https://devzone.nordicsemi.com/thread/317130?ContentTypeID=1</link><pubDate>Fri, 25 Jun 2021 07:38:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d80f6f68-0d60-4424-b06a-5137d9b5c09a</guid><dc:creator>acanidio</dc:creator><description>&lt;p&gt;Thanks, it works correcly and the power consumption is reduced when&amp;nbsp;detached from RTT Viewer.&lt;/p&gt;
&lt;p&gt;I also added the hook in the nrf_log_frontend.c as&amp;nbsp;mentioned in the previous post&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/**
 * @brief Log pending hook
 *
 */
__WEAK void log_pending_hook( void )
{
    // Nothing to do
}

/**
 * @brief Allocates chunk in a buffer for one entry and injects overflow if
 * there is no room for requested entry.
 *
 * @param content_len   Number of 32bit arguments. In case of allocating for hex dump it
 *                      is the size of the buffer in 32bit words (ceiled).
 * @param p_wr_idx      Pointer to write index.
 *
 * @return True if successful allocation, false otherwise.
 *
 */
static inline bool buf_prealloc(uint32_t content_len, uint32_t * p_wr_idx, bool std)
{
    uint32_t req_len = content_len + HEADER_SIZE;
    bool     ret            = true;
    CRITICAL_REGION_ENTER();
    *p_wr_idx = m_log_data.wr_idx;
    uint32_t available_words = (m_buffer_mask + 1) - (m_log_data.wr_idx - m_log_data.rd_idx);
    while (req_len &amp;gt; available_words)
    {
        UNUSED_RETURN_VALUE(nrf_atomic_u32_add(&amp;amp;m_log_data.log_dropped_cnt, 1));
        if (NRF_LOG_ALLOW_OVERFLOW)
        {
            uint32_t dropped_in_skip = log_skip();
            UNUSED_RETURN_VALUE(nrf_atomic_u32_add(&amp;amp;m_log_data.log_dropped_cnt, dropped_in_skip));
            available_words = (m_buffer_mask + 1) - (m_log_data.wr_idx - m_log_data.rd_idx);
        }
        else
        {
            ret = false;
            break;
        }
    }

    if (ret)
    {
        nrf_log_main_header_t invalid_header;
        invalid_header.raw = 0;

        if (std)
        {
            invalid_header.std.type        = HEADER_TYPE_STD;
            invalid_header.std.in_progress = 1;
            invalid_header.std.nargs       = content_len;
        }
        else
        {
            invalid_header.hexdump.type = HEADER_TYPE_HEXDUMP;
            invalid_header.hexdump.in_progress = 1;
            invalid_header.hexdump.len = content_len;
        }

        nrf_log_main_header_t * p_header =
                   (nrf_log_main_header_t *)&amp;amp;m_log_data.buffer[m_log_data.wr_idx &amp;amp; m_buffer_mask];

        p_header-&amp;gt;raw = invalid_header.raw;

        m_log_data.wr_idx += req_len;
    }

    CRITICAL_REGION_EXIT();
    log_pending_hook();
    return ret;
}

&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;I will suggest your reply as the answer.&lt;/p&gt;
&lt;p&gt;Andrea&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS power consumption and idle hook</title><link>https://devzone.nordicsemi.com/thread/316973?ContentTypeID=1</link><pubDate>Thu, 24 Jun 2021 12:06:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:55defeb9-84fd-4505-b63f-f946011dee03</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I think this is fixed in the&amp;nbsp;master branch but never got released as the nRF5 SDK is not frozen.&lt;/p&gt;
&lt;p&gt;In short you should apply this patch I showed in the link. Which do not need you to use ApplicationIdleHook for logger threads but instead have a new function that needs to be overridden by the application in main.c&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#if NRF_LOG_ENABLED &amp;amp;&amp;amp; NRF_LOG_DEFERRED
 void log_pending_hook( void )
 {
     BaseType_t result = pdFAIL;

    if ( __get_IPSR() != 0 )
    {
        BaseType_t higherPriorityTaskWoken = pdFALSE;
        result = xTaskNotifyFromISR( m_logger_thread, 0, eSetValueWithoutOverwrite, &amp;amp;higherPriorityTaskWoken );

        if ( pdFAIL != result )
        {
        portYIELD_FROM_ISR( higherPriorityTaskWoken );
        }
    }
    else
    {
        UNUSED_RETURN_VALUE(xTaskNotify( m_logger_thread, 0, eSetValueWithoutOverwrite ));
    }
 }
#endif&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: FreeRTOS power consumption and idle hook</title><link>https://devzone.nordicsemi.com/thread/316971?ContentTypeID=1</link><pubDate>Thu, 24 Jun 2021 11:59:28 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:408f0f30-ea3c-45fc-b301-89d281248079</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Yes, you are right, and we have noticed it in &lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/56898/bus-fault-after-calling-__get_fpscr-in-freertos-configpre_sleep_processing-x"&gt;this&lt;/a&gt;&amp;nbsp;thread. The workaround is also given in my reply taken from the suggestion of other forum member.&lt;/p&gt;
&lt;p&gt;Please try the suggested changes to see if this can be something you can use?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>