<?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>disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/8572/disable-interrupts-and-enable-interrupts-if-they-where-enabled</link><description>Hello, 
 I&amp;#39;m not sure if this was already asked before. I didn’t found a similar question, so here it is: When I have a function that manipulates a data structure to communicate with an interrupt service routine and if that manipulation can not be done</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 06 Aug 2015 13:09:54 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/8572/disable-interrupts-and-enable-interrupts-if-they-where-enabled" /><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31346?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 13:09:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:be2fdd6f-7c99-4a2d-8b0b-235b5785e2dd</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;It is actually a mask, so i&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if ( was_masked )
 __enable_irq();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;would mean that &amp;quot;enable it now, even though it was disabled before I disabled it&amp;quot;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31345?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 13:04:05 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b858c68-36d8-416d-bb23-8605c6711121</guid><dc:creator>Torsten Robitzki</dc:creator><description>&lt;p&gt;And by the way. I was not just using it wrong, but named the variable wrong too. Otherwise &amp;quot;if it was not enabled, enable&amp;quot; makes not sense ;-)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31344?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 12:58:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:97f34d5f-8e91-4742-874c-02bd74973dde</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;This is actually a good discovery. I will have to point this out to some people :)&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31348?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 12:18:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d24b8dc8-d4f6-4dcc-80b2-e509039342b5</guid><dc:creator>Torsten Robitzki</dc:creator><description>&lt;p&gt;Thank you. I was in deed concerned about a race condition. Good point with the special property of the ISR that could possibly introduce the race!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31343?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 12:15:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2b42864a-4baf-4727-8cff-f1ba1729bc94</guid><dc:creator>Torsten Robitzki</dc:creator><description>&lt;p&gt;Ups, my code was ment as pseudo code. I wasn&amp;#39;t aware that there is a version of __disable_irq() that works like I would expect it. I was looking up the function in core_cm0.h and there it returns void.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31342?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 12:04:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:8e0a2f37-4b7a-44db-8d20-43e6b44da5fd</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;There&amp;#39;s more confusion than that. The __disable_irq() function which returns something is in the ARM toolchain (only I think). The CMSIS function which is in core_cmFunc.h declares it as returning void, and that&amp;#39;s the one my compiler picks up at least and I&amp;#39;d think most others would if CMSIS is included.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31347?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 12:01:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76ac8688-0061-450e-97c1-c56c97f1bfe4</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;You should be able to use __get_PRIMASK() and __set_PRIMASK() like so&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;uint32_t old_primask = __get_PRIMASK();
__disable_irq();
manipulation();
__set_PRIMASK( old_primask );
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This looks like it might have a race condition but doesn&amp;#39;t. If interrupts were enabled and something interrupts you between the __get_PRIMASK() and the __disable_irq() and disables IRQs itself, it will restore them before it finishes so your &amp;#39;old_primask&amp;#39; variable will still be valid.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31341?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 11:47:45 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c3a59d77-8a1b-4798-afc2-0a419f923a1f</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;I see that ARM documentation says that it returns value of PSR, that is incorrect or it does not make sense, because that is not single bit. It should be PRIMASK&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: disable interrupts and enable interrupts if they where enabled</title><link>https://devzone.nordicsemi.com/thread/31340?ContentTypeID=1</link><pubDate>Thu, 06 Aug 2015 11:40:20 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:58fe2fdc-48f1-48a1-a784-c15c6b66e510</guid><dc:creator>Susheel Nuguru</dc:creator><description>&lt;p&gt;You are using it wrong.
The if statement should be&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;if ( !was_enabled )
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;a href="http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0491h/CJAFBCBB.html"&gt;infocenter.arm.com/.../index.jsp&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The __disable_irq gives you the value of PRIMASK  prior to calling that function.
If it returns 0, then it means that no interrupts were masked and this current call __disable_irq  has set the mask disabling configurable interrupts.&lt;/p&gt;
&lt;p&gt;If it returns 1, it means that it was already masked before calling this __disable_irq  and enabling interrupts is not done at current context.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>