<?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 S110 Re-entrancy Question</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/1560/nrf51822-s110-re-entrancy-question</link><description>When a callback function is registered via the softdevice_ble_evt_handler_set(), are there re-entrancy issues when the callback is called by the softdevice? 
 For instance, in the S110 example code the following is indicated about the callback: &amp;quot;Function</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 11 Feb 2014 17:30:10 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/1560/nrf51822-s110-re-entrancy-question" /><item><title>RE: nRF51822 S110 Re-entrancy Question</title><link>https://devzone.nordicsemi.com/thread/6897?ContentTypeID=1</link><pubDate>Tue, 11 Feb 2014 17:30:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:dab7f12e-35c1-4555-ba58-4421884ad89e</guid><dc:creator>Mike Thomspon</dc:creator><description>&lt;p&gt;Yes, I&amp;#39;ve started to find the SDK BLE source code very helpful.  Thanks again for supplying the missing details.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 S110 Re-entrancy Question</title><link>https://devzone.nordicsemi.com/thread/6896?ContentTypeID=1</link><pubDate>Tue, 11 Feb 2014 09:05:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:4b7b66f7-1fdf-41d7-a358-71bcf9f9b1aa</guid><dc:creator>Ole Morten</dc:creator><description>&lt;p&gt;As long as the softdevice_handler module is not initialized to use the SDK scheduler (app_scheduler), the callback will run directly from interrupt context. Service handlers and similar will normally run in the same context as the softdevice event handler.&lt;/p&gt;
&lt;p&gt;The same goes for app_timer, app_button and app_gpiote handlers; unless the module is initialized with the scheduler, the callbacks will run in interrupt context. You typically choose whether to use the scheduler or not with the last parameter of the init function.&lt;/p&gt;
&lt;p&gt;You can adjust the interrupt priority using the regular sd_nvic_SetPriority() function.&lt;/p&gt;
&lt;p&gt;There isn&amp;#39;t any document that covers this in detail, so if you&amp;#39;re in doubt, I will normally recommend you to just take a look at the source code. All SDK BLE modules are delivered with full source code, enabling you to fully investigate what happens.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 S110 Re-entrancy Question</title><link>https://devzone.nordicsemi.com/thread/6895?ContentTypeID=1</link><pubDate>Tue, 11 Feb 2014 08:47:32 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c7511889-249d-43d4-8489-76703079366a</guid><dc:creator>Mike Thomspon</dc:creator><description>&lt;p&gt;Thank you for the detailed response.&lt;/p&gt;
&lt;p&gt;I guess my question should have been stated simpler.  Is the context of the callback set in softdevice_ble_evt_handler_set() in the interrupt context or the main context?&lt;/p&gt;
&lt;p&gt;Your answer indicate its from the SWI2_IRQHandler() context so data structures accessed from this context and the main context would need some type of protection.&lt;/p&gt;
&lt;p&gt;Is there a Nordic document that gives a general overview of the different contexts related to the softdevice and SDK?  I&amp;#39;ve a number of books on ARM Cortex programming that cover this in general, but the Nordic SDK is my first experience of putting such considerations in practice and it&amp;#39;s not always clear what context a function is called from.&lt;/p&gt;
&lt;p&gt;Thanks.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: nRF51822 S110 Re-entrancy Question</title><link>https://devzone.nordicsemi.com/thread/6894?ContentTypeID=1</link><pubDate>Mon, 10 Feb 2014 12:02:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:83cbfe59-fa6f-426d-892e-3261ef773cf1</guid><dc:creator>Ole Morten</dc:creator><description>&lt;p&gt;I&amp;#39;m not absolutely certain that I fully understand your question, but in general, you have to take care to protect any data structure that you share between interrupt and main contexts.&lt;/p&gt;
&lt;p&gt;The softdevice provides a function called sd_nvic_critical_region_enter()/_exit(), which should be usable for this. Beware that this protects your code snippet only against application level interrupts, so interrupts that are fully handled internal in the softdevice may still occur. The call hence does not make any guarantees about timing, only about data safety.&lt;/p&gt;
&lt;p&gt;Also, since these functions are SVC calls, they can only be called from interrupt priority 3 (APP_LOW) and from main. However, when in APP_HIGH, you are in effect already in a critical region, since no application level interrupt can interrupt you. The SDK macro CRITICAL_REGION_ENTER()/_EXIT() in app_util.h wraps this difference, and can hence be used from any level.&lt;/p&gt;
&lt;p&gt;In SDK applications that does not use the scheduler, the sd_ble_evt_get() and the resulting ble_evt_dispatch() are both run directly from the SWI2_IRQHandler() context (as can be seen in softdevice_handler.c). Hence, if you manipulate data in this event handler, but also from main context, you should protect this data access from main, to avoid the SWI2 interrupt being triggered during the manipulation.&lt;/p&gt;
&lt;p&gt;If you use the scheduler or some other mechanism to transfer the actual retrieving of events from the softdevice and the ble_evt_dispatch() call to main context, there should however not be any concerns like this.&lt;/p&gt;
&lt;p&gt;Let me know if I misunderstand anything, or if anything is still unclear.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>