<?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>What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/6678/what-could-explain-a-hardfault-when-entering-in-critical-region</link><description>Hello, 
 We are developing an application which is using the app_scheduler (especially to put events etc.) 
 It seems that sometimes (very rarely) we detected a Hard Fault when the app_scheduler is calling the CRITICAL_REGION_ENTER() macro (into the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 27 Apr 2015 06:45:45 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/6678/what-could-explain-a-hardfault-when-entering-in-critical-region" /><item><title>RE: What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/thread/23433?ContentTypeID=1</link><pubDate>Mon, 27 Apr 2015 06:45:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:57a0bd58-9a6d-49e7-be6b-4f5337329744</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;good observation sebastien, there is a limitation on how you use CRITICAL_REGION_xx macro. You cannot use it when softdevice is disabled and have the macro SOFTDEVICE_PRESENT defined in your project. you can just use below when you are not using softdevice&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;static uint32_t m_in_critical_region = 0;

void critical_region_enter(void)
{
    __disable_irq();    
    m_in_critical_region++;    
}

void critical_region_exit(void)
{
    m_in_critical_region--;    
    if (m_in_critical_region == 0)
    {
        __enable_irq();
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code is also available in components\libraries\util\app_util_platform.c&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/thread/23432?ContentTypeID=1</link><pubDate>Fri, 24 Apr 2015 15:25:47 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:393b7523-b993-4141-a3ca-22c748baf186</guid><dc:creator>Sebastien</dc:creator><description>&lt;p&gt;Important note : two nested CRITICAL_REGION_ENTER generate an hardfault only if the softdevice is not enabled !&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/thread/23431?ContentTypeID=1</link><pubDate>Fri, 24 Apr 2015 09:53:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b293b629-1504-477f-9696-6d9e0b075a64</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;good to know, can you please accept the right answer so that we can close this case.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/thread/23430?ContentTypeID=1</link><pubDate>Fri, 24 Apr 2015 09:31:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fe4957d5-cdac-4716-88a2-f414875e7bcf</guid><dc:creator>Sebastien</dc:creator><description>&lt;p&gt;Hello Aryan,&lt;/p&gt;
&lt;p&gt;Thanks for your comment.&lt;/p&gt;
&lt;p&gt;Yes we are using the app_timer and the RTC1 IRQ is always set to LOW.&lt;/p&gt;
&lt;p&gt;But, actually, we have modify the app_timer in order to save RAM consumption, and I found the issue:
In our RTC1_IRQHandler, we add CRITICAL_REGION_ENTER when calling &amp;quot;timer_timeouts_check()&amp;quot;, so when there is a task to execute, we will perform another CRITICAL_REGION_ENTER in the app_scheduler.&lt;/p&gt;
&lt;p&gt;You routed me to the issue ;) Thx!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/thread/23429?ContentTypeID=1</link><pubDate>Fri, 24 Apr 2015 08:32:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3e637cc8-071f-4d05-afe4-f3d6fe79c7be</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Check if you are using APP_TIMER and if you have set RTC1 interrupt handler priority to APP_HIGH, then like RK said it will cause an hardfault in app_scheculer&lt;/p&gt;
&lt;p&gt;below m_evt_schedule_func = app_timer_evt_schedule (set in APP_TIMER_INIT macro)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;RTC1_IRQHandler-&amp;gt;timer_timeouts_check()-&amp;gt;timeout_handler_exec-&amp;gt;m_evt_schedule_func-&amp;gt;app_sched_event_put-&amp;gt;CRITICAL_REGION_ENTER
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/thread/23428?ContentTypeID=1</link><pubDate>Fri, 24 Apr 2015 08:10:00 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46f9ae50-c417-4ef4-a01f-e9edb0557fb3</guid><dc:creator>Sebastien</dc:creator><description>&lt;p&gt;Hello RK,&lt;/p&gt;
&lt;p&gt;Thanks a lot for your quick answer!&lt;/p&gt;
&lt;p&gt;About the first reason, I think this is handled by the CRITICAL_REGION_ENTER() macro because we won&amp;#39;t disable IRQs when the current interrupt priority is HIGH =1. I don&amp;#39;t see any app irq priority equal to 0, is it possible?&lt;/p&gt;
&lt;p&gt;I did a simple test, consisting to call two CRITICAL_REGION_ENTER, and I got the same panic trace. So it looks like we try to disable irq whereas it&amp;#39;s already done. I will double check in our SW where it could happen.&lt;/p&gt;
&lt;p&gt;Thx again.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What could explain a HardFault when entering in critical region?</title><link>https://devzone.nordicsemi.com/thread/23427?ContentTypeID=1</link><pubDate>Fri, 24 Apr 2015 01:55:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bbf6ce23-79aa-4027-afa3-d0c72b521834</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;Since it looks from what you&amp;#39;ve posted that the hardfault is happening at the critical region enter (not inside the softdevice ie not a softdevice bug) then it seems it&amp;#39;s happening at the svc() call. The only two reasons I know of that an svc() call will go to hardfault are if it&amp;#39;s executed&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;from a higher interrupt state, SVC() in the softdevice is a level 2 interrupt, so if it&amp;#39;s called from a level 0 or 1 interrupt it will hardfault&lt;/li&gt;
&lt;li&gt;It&amp;#39;s called when interrupts are disabled, ie PRIMASK is 1.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Don&amp;#39;t know how you are getting into one or other of those states, if you have the registers on the panic you should be able to figure out whether it&amp;#39;s in thread or handler mode, what the current interrupt priority is if in handler mode and whether PRIMASK is or is not 1 and confirm that.&lt;/p&gt;
&lt;p&gt;I can&amp;#39;t think of anything else.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>