<?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>How to manage concurrent accesses to BLE stack in a FreeRTOS based application</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/56161/how-to-manage-concurrent-accesses-to-ble-stack-in-a-freertos-based-application</link><description>I am setting up a relatively complex project that runs on top of FreeRTOS and it is based on the HRS freertos sample app from SDK 16.0.0. The device will need to run in several different modes (e.g. peripheral and/or central) and I am trying to keep it</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 07 Jan 2020 07:55:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/56161/how-to-manage-concurrent-accesses-to-ble-stack-in-a-freertos-based-application" /><item><title>RE: How to manage concurrent accesses to BLE stack in a FreeRTOS based application</title><link>https://devzone.nordicsemi.com/thread/227808?ContentTypeID=1</link><pubDate>Tue, 07 Jan 2020 07:55:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:07390387-af03-4806-be1a-10cef48fa79d</guid><dc:creator>TylerD</dc:creator><description>&lt;p&gt;Thanks for the feedback. I am currently not running into any problems, but I was just thinking ahead how to&amp;nbsp;guarantee my program will run stable and there will be no race conditions etc.&lt;/p&gt;
&lt;p&gt;If the softdevice API&amp;nbsp;is thread safe then there should be no problems. The scenario that I was thinking about was something like this:&lt;/p&gt;
&lt;p&gt;1) task 1 calls some SD function, such as&amp;nbsp;sd_ble_gattc_read&amp;nbsp;&lt;/p&gt;
&lt;p&gt;2) before the&amp;nbsp;above call is finished, there is a context swich and task 2 is resumed&lt;/p&gt;
&lt;p&gt;3) task 2 calls some other SD function (e.g.&amp;nbsp;sd_ble_gattc_write)&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Potential issue here would have been that the 2nd SD function call is invoked before the 1st one is finished. I thought this could happen for example when there are two connections that are managed by two different tasks.&lt;/p&gt;
&lt;p&gt;I just noticed a note about thread-safety in the&amp;nbsp;softdevice &lt;a href="https://infocenter.nordicsemi.com/pdf/S132_SDS_v2.0.pdf"&gt;specification&lt;/a&gt;, Chapter 5 -&amp;gt; &amp;quot;SVCs are software triggered interrupts conforming to a procedure call standard for parameter passing and return values. ....&amp;nbsp; This SVC interface makes SoftDevice API calls thread-safe; they can be invoked from the application&amp;#39;s different priority levels without additional synchronization mechanisms.&amp;quot;&lt;/p&gt;
&lt;p&gt;This is perfectly in line with your response above. So that&amp;#39;s good news for me, no&amp;nbsp;additional synch code needed because the API is inherently thread safe. Nice!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to manage concurrent accesses to BLE stack in a FreeRTOS based application</title><link>https://devzone.nordicsemi.com/thread/227666?ContentTypeID=1</link><pubDate>Mon, 06 Jan 2020 12:53:21 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ba549dbd-554f-48b9-8c55-c59d51bf56a8</guid><dc:creator>Susheel Nuguru</dc:creator><description>[quote user=""]Is there any recommendations on how to manage concurrent accesses to the softdevice API from multiple tasks?[/quote]
&lt;p&gt;&amp;nbsp;All FreeRTOS tasks are running at thread priority which is less than the SVC priority with which all softdevice API are implemented.&lt;/p&gt;
&lt;p&gt;I do not see any need of any of your tasks co-ordinating to process or push softdevice activity, since if the FreeRTOS task is running, that means none of the interrupt context roles are running, including the any softdevice API (since it is running in SVC context).&lt;/p&gt;
&lt;p&gt;Maybe I am missing to see what you are seeing, can you please give me one scenario where you need the tasks to co-ordinate for accessing softdevice API? All you need to do, is to make your tasks self aware of the current role your device is in (advertising or connected) and handle the INVALID_STATE error returned by the softdevice graciously., in case of calling any softdevice API that do not fit the role or state it is in&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to manage concurrent accesses to BLE stack in a FreeRTOS based application</title><link>https://devzone.nordicsemi.com/thread/227458?ContentTypeID=1</link><pubDate>Fri, 03 Jan 2020 15:17:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ddb06a27-9229-4d31-a833-3b40bf7b87c3</guid><dc:creator>TylerD</dc:creator><description>&lt;p&gt;Found an interesting thread from about half a year back:&amp;nbsp;&lt;a href="https://devzone.nordicsemi.com/f/nordic-q-a/47389/is-sd_ble_gatts_hvx-api-thread-safe"&gt;https://devzone.nordicsemi.com/f/nordic-q-a/47389/is-sd_ble_gatts_hvx-api-thread-safe&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;According to that thread, &lt;strong&gt;softdevice calls are thread safe&lt;/strong&gt; (all of them?). If that&amp;#39;s the case, then maybe I&amp;#39;m worrying too much. However, I&amp;#39;d like get confirmation from Nordic folks, or any other customers who have&amp;nbsp;worked on real designs based on FreeRTOS.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How to manage concurrent accesses to BLE stack in a FreeRTOS based application</title><link>https://devzone.nordicsemi.com/thread/227455?ContentTypeID=1</link><pubDate>Fri, 03 Jan 2020 15:02:16 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:47c66a07-f856-4063-a1d3-26b84fbc3264</guid><dc:creator>TylerD</dc:creator><description>&lt;p&gt;I&amp;nbsp;read&amp;nbsp;through the code that implements the BLE task (softdevice_task, implemented in nrf_sdh_freertos.c) and it looks like my queue-based solution will not work. Here&amp;#39;s the relevant code snippet:&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;void SD_EVT_IRQHandler(void)
{
    BaseType_t yield_req = pdFALSE;

    vTaskNotifyGiveFromISR(m_softdevice_task, &amp;amp;yield_req);

    /* Switch the task if required. */
    portYIELD_FROM_ISR(yield_req);
}


/* This function gets events from the SoftDevice and processes them. */
static void softdevice_task(void * pvParameter)
{
    NRF_LOG_DEBUG(&amp;quot;Enter softdevice_task.&amp;quot;);

    if (m_task_hook != NULL)
    {
        m_task_hook(pvParameter);
    }

    while (true)
    {
        nrf_sdh_evts_poll();                    /* let the handlers run first, incase the EVENT occured before creating this task */

        (void) ulTaskNotifyTake(pdTRUE,         /* Clear the notification value before exiting (equivalent to the binary semaphore). */
                                portMAX_DELAY); /* Block indefinitely (INCLUDE_vTaskSuspend has to be enabled).*/
    }
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;If I read this correctly, the task will pop any events from the stack and then it blocks indefinitely (ulTaskNotifyTake) until there is an&amp;nbsp;interrupt from the BLE stack. And if the stack is idle or there is very little radio activity it may take long (or forever) until the task gets into running state.&lt;/p&gt;
&lt;p&gt;What next? I was thinking about maybe creating one additional task for managing BLE requests that are initiated by one or more &amp;quot;worker&amp;quot; tasks in my&amp;nbsp;setup. The purpose of this task would be to collect the requests that are coming from several tasks (through a queue) and then call the relevant softdevice calls and process the responses. This would make sure that there is no overlapping accesses to the BLE stack. I&amp;#39;m quite sure this is doable and safe, but seems kind of an overkill to have two RTOS&amp;nbsp;tasks to manage the BLE operations.&lt;/p&gt;
&lt;p&gt;Another more lightweight(?) solution could be to disable context switches while performing any&amp;nbsp;softdevice API calls.&lt;/p&gt;
&lt;p&gt;And a brute-force (and perhaps unnecessary?) approach would be to use critical section for any code that accesses the BLE stack.&amp;nbsp;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>