<?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 is the easiest way to find if certain part of code is executed from ISR?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/69500/what-is-the-easiest-way-to-find-if-certain-part-of-code-is-executed-from-isr</link><description>I am using FreeRTOS and some of the commands there have two flavors: for using in regular code flow and when are executed from an ISR, but in nRF SDK components and examples is difficult to see right away all the call stack through if there is some ISR</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 18 Dec 2020 11:27:48 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/69500/what-is-the-easiest-way-to-find-if-certain-part-of-code-is-executed-from-isr" /><item><title>RE: What is the easiest way to find if certain part of code is executed from ISR?</title><link>https://devzone.nordicsemi.com/thread/285809?ContentTypeID=1</link><pubDate>Fri, 18 Dec 2020 11:27:48 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:42fc2f9b-b188-4a62-b655-cb519cf3bde6</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user="samsam"]Is there a way to see this register in debug mode at a breakpoint? I dont see this register exposed in SES in any of the registers groups[/quote]
&lt;p&gt;I put a breakpoint in FreeRTOS RTC interrupt handler, Start debugger in Segger IDE-&amp;gt;View-&amp;gt;Memory and put&amp;nbsp;0xE000ED04 as shown below&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/620x488/__key/communityserver-discussions-components-files/4/pastedimage1608290316207v4.png" /&gt;&lt;/p&gt;
&lt;p&gt;You can see that 0XE000ED04 has the value of &lt;strong&gt;0x00000821&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;From the register definition below&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/534x247/__key/communityserver-discussions-components-files/4/pastedimage1608290406778v5.png" /&gt;&lt;/p&gt;
&lt;p&gt;The first six bits of the register address will give the active vector number, so, from 0X0000821, the first 6 bits will be 0x21 which is decimal 33.&lt;/p&gt;
&lt;p&gt;You need to substract 16 from this as this is the offset to get the platform based ISR number. So 33-16 = 17.&lt;/p&gt;
&lt;p&gt;From the &lt;a href="https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.nrf52832.ps.v1.1%2Fmemory.html&amp;amp;cp=4_2_0_7_2&amp;amp;anchor=memorymap"&gt;memory map&lt;/a&gt; of nRF52832 you can see that number 17 is RTC1&lt;/p&gt;
&lt;p&gt;&lt;img alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/553x423/__key/communityserver-discussions-components-files/4/pastedimage1608290585161v6.png" /&gt;&lt;/p&gt;
&lt;p&gt;That is exactly we put the breakpoint and hence you can see that the SCB-ICSR value can give us the context of the interrupt you are running. If VECTACTIVE is 0, then you are running in Thread mode.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
[quote user="samsam"]Do you know what is the worst possible&amp;nbsp; consequence if use&amp;nbsp; some FreeRTOS&amp;nbsp; &amp;nbsp;*FromISR() functions in non ISR context? I didnt find any visible problems so far[/quote]
&lt;p&gt;&amp;nbsp;No one can answer you that, because the behavior is unpredictable and your system might not fail or fail eventually over time. It is not allowed to call a FromISR function from non ISR context. Instead just do below&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;if (( SCB-&amp;gt;ICSR &amp;amp; SCB_ICSR_VECTACTIVE_Msk ) == 0 ))
{
    // not inside ISR (Thread mode)
    
}else {
  // inside  ISR
  
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What is the easiest way to find if certain part of code is executed from ISR?</title><link>https://devzone.nordicsemi.com/thread/285653?ContentTypeID=1</link><pubDate>Thu, 17 Dec 2020 13:46:29 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b7a55e69-bdc3-4640-93f0-f9075e95395b</guid><dc:creator>samsam</dc:creator><description>&lt;p&gt;Hello Susheel,,&lt;/p&gt;
&lt;p&gt;Is there a way to see this register in debug mode at a breakpoint? I dont see this register exposed in SES in any of the registers groups :(&lt;/p&gt;
&lt;p&gt;Do you know what is the worst possible&amp;nbsp; consequence if use&amp;nbsp; some FreeRTOS&amp;nbsp; &amp;nbsp;*FromISR() functions in non ISR context? I didnt find any visible problems so far.&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What is the easiest way to find if certain part of code is executed from ISR?</title><link>https://devzone.nordicsemi.com/thread/285217?ContentTypeID=1</link><pubDate>Wed, 16 Dec 2020 07:13:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:41ff3996-f751-4a42-a1eb-ddba2a1c22e2</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;Hi Samsam,&lt;/p&gt;
&lt;p&gt;The ARM register SCB-&amp;gt;ICSR would be best thing to look into to know the context of the CPU state (ISR or Thread). I find &lt;a href="https://interrupt.memfault.com/blog/arm-cortex-m-exceptions-and-nvic"&gt;this&lt;/a&gt;&amp;nbsp;external page very useful when it comes to Cortex-M4 interrupt context. If you look at the ICSR section you can see that this register has VECTACTIVE register which you can poll to know the current context.&lt;/p&gt;
&lt;p&gt;For example if you see that&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;SCB-&amp;gt;ICSR &amp;amp; SCB_ICSR_VECTACTIVE_Msk )&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Then it means that you are executing in the thread context and not in any ISR.&lt;/p&gt;
&lt;p&gt;A non zero number would give you the raw interrupt number from which you need to substract 16 (Since nRF implements only 4 bits of these) to get the interrupt number mapped on nRF device&amp;nbsp;&lt;/p&gt;
&lt;p&gt;The direct way is to use the CMSIS API as below&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;IRQn_Type current_irqn = ((uint32_t)__get_IPSR()) - 16;&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>