<?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>Reentrancy with SDK15.3</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/115820/reentrancy-with-sdk15-3</link><description>I&amp;#39;m using SDK1.3 on a nRF52840 and running BLE on it. I&amp;#39;m using FreeRTOS under it and specifically non-preemptive scheduling. From time to time I do a scan using a filter of the names. 
 I&amp;#39;ve got a bug that acts more like a stack overrun (it isn&amp;#39;t as</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 25 Oct 2024 14:05:26 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/115820/reentrancy-with-sdk15-3" /><item><title>RE: Reentrancy with SDK15.3</title><link>https://devzone.nordicsemi.com/thread/507972?ContentTypeID=1</link><pubDate>Fri, 25 Oct 2024 14:05:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5193a753-9447-48fb-af88-4fdefd4afc11</guid><dc:creator>Randy Lee</dc:creator><description>&lt;p&gt;that&amp;#39;s exactly what I needed to know... thank you.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Reentrancy with SDK15.3</title><link>https://devzone.nordicsemi.com/thread/507952?ContentTypeID=1</link><pubDate>Fri, 25 Oct 2024 13:17:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b7dfb8e9-3edc-4394-be1a-933fcaff2628</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user=""]*) These are eventually being called by the soft device but I don&amp;#39;t know in what context...&amp;nbsp;&amp;nbsp; Are these essentially ISR level calls being made from the SD so that they are preemptive (i,.e. if I&amp;#39;m in the callbacks too long I&amp;#39;m going to get overrun by another)?[/quote]
&lt;p&gt;Yes, the callbacks triggered by the SoftDevice (e.g., &lt;code&gt;ble_evt_handler&lt;/code&gt;) are indeed called from the SoftDevice&amp;#39;s Interrupt Service Routine (ISR) context. The SoftDevice runs at a high interrupt priority level, so when it triggers an event (like BLE_GAP_EVT_ADV_REPORT), it is effectively running in an ISR. This means:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Preemption is enabled&lt;/strong&gt;: The SoftDevice can interrupt lower-priority tasks, but other ISRs with a higher priority than the SoftDevice ISR (like other critical hardware interrupts) can still preempt the SoftDevice ISR.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Time sensitivity&lt;/strong&gt;: If your handler takes too long to execute, you risk causing issues, such as missed BLE events or potential overrun situations. Hence, it&amp;#39;s crucial to keep these callbacks short and efficient, deferring longer operations to a different context (like a FreeRTOS task).&lt;/p&gt;
[quote user=""]*) Can I use semaphores inside those callbacks so that I can hold off calls that are happening too often?[/quote]
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;FreeRTOS FromISR Functions&lt;/strong&gt;: When interacting with FreeRTOS from an ISR, you need to use the special &amp;quot;FromISR&amp;quot; versions of functions. For example:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;xSemaphoreGiveFromISR()&lt;/code&gt; instead of &lt;code&gt;xSemaphoreGive()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;xQueueSendFromISR()&lt;/code&gt; instead of &lt;code&gt;xQueueSend()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Deferring Work to Tasks&lt;/strong&gt;: A common pattern is to defer longer operations to a FreeRTOS task by using a &lt;strong&gt;semaphore&lt;/strong&gt; or &lt;strong&gt;queue&lt;/strong&gt; to signal the task from the ISR. This approach allows the ISR (callback) to quickly return, avoiding potential problems with reentrancy and keeping the SoftDevice responsive.&lt;/p&gt;
[quote user=""]*) Can these callbacks in fact be overlapped or does the SD itself wait till it all comes back to it before sending another one?[/quote]
&lt;p&gt;The SoftDevice itself can queue multiple events escpecially when the events are triggered from the higher priority contexts within the softdevice, but does not call your event handler if a previous event is still being processed. In other words, the SoftDevice does not &amp;quot;re-enter&amp;quot; the same event callback. This means you don&amp;#39;t have to worry about reentrancy within the same event type&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;However, you can still run into issues if multiple different ISRs (or callbacks) try to access shared resources. For example, if you have a high-priority ISR that modifies data that another ISR (from the SoftDevice) also accesses, you need to ensure proper synchronization (e.g., with mutexes or critical sections).&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>