<?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>Can&amp;#39;t access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/20599/can-t-access-variable-in-irq-handler</link><description>Hi, 
 I&amp;#39;m getting familiar with my nRF52 DK (PCA 10040). 
 I stumbled on something when I was doing an application that toggles LED1 on every TIMER0 event (200ms). Here is my code 
 #include &amp;quot;stdint.h&amp;quot;
#include &amp;quot;nrf52.h&amp;quot;

//volatile uint32_t varA;</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Sat, 25 Mar 2017 22:22:52 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/20599/can-t-access-variable-in-irq-handler" /><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80284?ContentTypeID=1</link><pubDate>Sat, 25 Mar 2017 22:22:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1e03a029-3687-44e1-b7c3-becaf734a393</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;Made a little change in the IRQHandler and now the LED is toggeling..
varA is declared as a global I added this to the IRQHandler&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;volatile uint32_t varB;

varA = NRF_P0-&amp;gt;OUTSET &amp;amp; (1&amp;lt;&amp;lt;17);
varB = varA;

if (varB)
// The rest is same as before
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80283?ContentTypeID=1</link><pubDate>Sat, 25 Mar 2017 21:53:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:358f5430-db38-4857-81e9-98e34289c481</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;I Rebuild the code, load it to the target using J-LINK / J-TRACE Cortex driver with the option Reset and Run. Then I start the debugger J-Link / J-Trace Adapter with Port = SW @ Max Clock 5 Mhz.
I set a breakpoint at &amp;quot;varA = NRF_P0-&amp;gt;OUTSET &amp;amp; (1&amp;lt;&amp;lt;17) in the IRQHandler Press Run (F5) and the code stops at the breakpoint. From here I can either press Run again or single step the IRQHandler. In both ways the LED is toggled.
When I stop the debugger I&amp;#39;m not sure if the code is still running on the DK because the LED doesn&amp;#39;t toggle anymore.
Doesn&amp;#39;t help if unplug/plug the USB cable.
I&amp;#39;m running in evaluation mode if that is a problem? My code is faaaar less than the 32k limit..
Everything is default in my project options except i&amp;#39;m also producing a Hex file and using J-LINK/J-TRACE Cortex driver instead of ULINK Pro Cortex Debugger&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80282?ContentTypeID=1</link><pubDate>Sat, 25 Mar 2017 18:57:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:66bc7b16-09b4-41aa-88c1-ebb86cf5e7e3</guid><dc:creator>butch</dc:creator><description>&lt;p&gt;That is really strange.  The debugger should have no effect: AFAIK it doesn&amp;#39;t change your code or the timing of it at all (since the JTAG that assists the debugger server and client is in hw.)  How exactly are you proceeding?  As I recall, when you terminate the debugger client (from Keil) you app keeps on running on the target (or will restart when you unplug and plug in the USB cable.)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80281?ContentTypeID=1</link><pubDate>Sat, 25 Mar 2017 15:27:06 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:da899655-7d9d-444e-af16-acc8a6d7138e</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;Code works in the debugger but not when I run it outside the debugger..&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80272?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 15:47:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4955bb77-ba21-433a-aca8-a2f80447cb4c</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;I also think there is one or more options I should select/deselect in my project. But there are so many options which I have no clue. I use one of the latest versions of Keil and the software packs (Installed computer last week).
But I&amp;#39;ll try some more when I get access to the DK again.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80274?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 14:52:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4abb42ef-87a6-4a52-84ba-118654ad2a7b</guid><dc:creator>butch</dc:creator><description>&lt;p&gt;BTW, the var is not really volatile, only the IO regs, since you only write and read the var from one thread (the IRQ handler.)  But I suppose you declared it volatile to insure it was not optimized away.  The Nordic definitions should declare the IO regs as volatile to insure reads and writes are not optimized away.  But you already said you have tested without optimizing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80273?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 14:41:42 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6f29ae5e-52a2-4642-92c8-312562835aa7</guid><dc:creator>butch</dc:creator><description>&lt;p&gt;I tested your code in both configurations (local or global var) and the LED blinks in both.  I.E. unable to replicate in my environment: Eclipse Neon, GNU ARM tools, Ubuntu, Nordic SDK 12.2.  So I would suspect a toolchain problem.  I did compile using C++ compiler, so I did extern &amp;quot;C&amp;quot; the IRQ handler.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80271?ContentTypeID=1</link><pubDate>Thu, 23 Mar 2017 13:17:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:61f6dc58-83b2-4b58-a709-414e816a0364</guid><dc:creator>butch</dc:creator><description>&lt;p&gt;Could it be the ARM write cache aka write buffers?  See the ARM documents about &amp;quot;write buffers.&amp;quot;  Also see the terse note in the Nordic docs about migrating from 51 to 52.  Briefly, a write to an IO register is buffered, and may not take effect until a read (from another IO register.)  In your code, you don&amp;#39;t seem to do any reads until the first interrupt.  Thus it might be that some (none?) of your writes take effect or take effect a little later than you think, causing a race.  I don&amp;#39;t believe that myself, since then why would the Timer be started?  But you could test it by inserting reads just after writes to flush the cache.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80276?ContentTypeID=1</link><pubDate>Wed, 22 Mar 2017 21:46:44 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:830cd817-068e-4604-b3b4-ffab87c505fc</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;I know I don&amp;#39;t need a variable in this case. But I may in other cases. I will try to debug it and see what happens.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80275?ContentTypeID=1</link><pubDate>Wed, 22 Mar 2017 21:19:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:22333d21-3215-44ec-a027-0d40cc2105d5</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;Why don&amp;#39;t you just put a debugger on it and look to see what&amp;#39;s happening. Since you&amp;#39;re assigning a value to the variable every time it doesn&amp;#39;t matter if it&amp;#39;s global or local so it must be something else.&lt;/p&gt;
&lt;p&gt;In fact you don&amp;#39;t even need the variable at all, just&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if( NRF_P0-&amp;gt;OUT &amp;amp; ( 1 &amp;lt;&amp;lt; 17 ) )
    .. toggle
else 
    .. other toggle
&lt;/code&gt;&lt;/pre&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80278?ContentTypeID=1</link><pubDate>Wed, 22 Mar 2017 19:46:09 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c5f10f19-5bd9-4f1a-833f-256b952feec9</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;@Kristin &lt;code&gt;static volatile&lt;/code&gt; does not help me. Nor do any changes in optimization&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80277?ContentTypeID=1</link><pubDate>Wed, 22 Mar 2017 19:44:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:46e2eb4b-9644-4319-aabb-522cbcd66d0d</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;@butch Now you have the complete code in a simplified example.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80270?ContentTypeID=1</link><pubDate>Wed, 22 Mar 2017 16:05:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:90cccd1f-896c-4884-8154-55b40c91e42a</guid><dc:creator>butch</dc:creator><description>&lt;p&gt;If you still have a problem, it might help to post all of the exact code that doesn&amp;#39;t work.  You have only posted the code (and only part of it) that does work.  The problem could be something as simple as a typo.  Few readers will try to debug code they can&amp;#39;t see.  Some readers won&amp;#39;t mind reading a lot of code, but sometimes it helps to reduce the code to a minimum that demonstrates the problem.  Also, have you tried without optimization?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80269?ContentTypeID=1</link><pubDate>Mon, 20 Mar 2017 14:10:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1d81418f-9c61-4d1a-8099-45415c3ec846</guid><dc:creator>FormerMember</dc:creator><description>&lt;p&gt;It should normally not be any problem to use global variables in an interrupt context. When defining &amp;#39;temp&amp;#39;  as a global variable, does it change anything if it is declared as &lt;code&gt;static volatile&lt;/code&gt;?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80280?ContentTypeID=1</link><pubDate>Mon, 20 Mar 2017 06:05:18 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0c2bea72-1264-45ab-9fbf-35a49fab3c05</guid><dc:creator>bitFrost</dc:creator><description>&lt;p&gt;I&amp;#39;m sorry. There is of course some code not shown in this example. Like UARTE initialization and pin configuration.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Can't access variable in IRQ Handler</title><link>https://devzone.nordicsemi.com/thread/80279?ContentTypeID=1</link><pubDate>Sun, 19 Mar 2017 16:14:01 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:75db48d3-eab5-4cd3-82bf-02481544ffaa</guid><dc:creator>butch</dc:creator><description>&lt;p&gt;Don&amp;#39;t you need to configure P0.17 as an output since it resets to an input with buffer disconnected?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>