<?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>nRF51822 - atomic increment of a variable</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/3584/nrf51822---atomic-increment-of-a-variable</link><description>Using: 
 
 GCC 4.8 
 pure-gcc Setup 
 Sdk 5.2.0 
 Softdevice 6.0 
 
 I execute some code within interrupt context (softdevice handler) and also in main thread.
I need to increment / decrement an integer variable concurrent in both thread context</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 28 Sep 2017 10:52:54 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/3584/nrf51822---atomic-increment-of-a-variable" /><item><title>RE: nRF51822 - atomic increment of a variable</title><link>https://devzone.nordicsemi.com/thread/12983?ContentTypeID=1</link><pubDate>Thu, 28 Sep 2017 10:52:54 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:fb0cc18f-ab26-4166-af93-b1efbfe88ccd</guid><dc:creator>Fedexx2</dc:creator><description>&lt;p&gt;I dug inside the code of CRITICAL_REGION_ENTER/EXIT and studied how it works.
During the ENTER:
first it call __disable_irq(), then it disable all the APP IRQs, then call __enable_irq().
and in the EXIT it does the same stuff, of coure re-enabling the APP IRQs.&lt;/p&gt;
&lt;p&gt;So, for a very very small operation like the increment of a variable wouldn&amp;#39;t it be better to use directly  __disable_irq()/__enable_irq(),
instead of having them called twice in the CRITICAL_REGION_ENTER/EXIT?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - atomic increment of a variable</title><link>https://devzone.nordicsemi.com/thread/12985?ContentTypeID=1</link><pubDate>Thu, 28 Aug 2014 06:55:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:471478c5-fb62-4a9e-89de-9b16a1d748c5</guid><dc:creator>Joe Merten</dc:creator><description>&lt;p&gt;I&amp;#39;d thought about __disable_irq() because of performance reasons (about 800ns against 25µs of critical region), but I now finally decide for critical region.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 - atomic increment of a variable</title><link>https://devzone.nordicsemi.com/thread/12984?ContentTypeID=1</link><pubDate>Sat, 23 Aug 2014 17:15:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:02fb2f1d-9f8e-44c9-b71a-5a145b3ce75e</guid><dc:creator>Nikita</dc:creator><description>&lt;p&gt;When you call sd_nvic_critical_region_enter() it&amp;#39;ll disable all your application interrupts but doesn&amp;#39;t affect softdevice interrupts. Why do you want to disable all interrupts including softdevice? Softdevice interrupts won&amp;#39;t change your variable.&lt;/p&gt;
&lt;p&gt;I think the best way is to disable app interrupts in main on variable write.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>