<?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>Starting a new thread from a BLE callback</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/76309/starting-a-new-thread-from-a-ble-callback</link><description>Hi, I have a BLE service: 
 
 BT_GATT_SERVICE_DEFINE (lres_svc, 
 BT_GATT_PRIMARY_SERVICE ( BT_UUID_LRES ), 
 BT_GATT_CHARACTERISTIC ( BT_UUID_LRES_STAT , BT_GATT_CHRC_NOTIFY , 
 BT_GATT_PERM_NONE , NULL , NULL , NULL ), 
 BT_GATT_CCC ( lec_ccc_cfg_changed</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 14 Jun 2021 11:26:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/76309/starting-a-new-thread-from-a-ble-callback" /><item><title>RE: Starting a new thread from a BLE callback</title><link>https://devzone.nordicsemi.com/thread/315095?ContentTypeID=1</link><pubDate>Mon, 14 Jun 2021 11:26:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e6e09053-afaa-4b1e-8c6a-d14024370c5c</guid><dc:creator>bachelor</dc:creator><description>&lt;p&gt;Found the problem now: thread_data was declared inside the callback function, so it lies at address &amp;amp;thread_data on the Bluetooth stack . It is therefore not a valid address for other threads. As a consequence the MPU recognizes a memory access violation. When I declared it outside the callback function the problem went away.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Starting a new thread from a BLE callback</title><link>https://devzone.nordicsemi.com/thread/315093?ContentTypeID=1</link><pubDate>Mon, 14 Jun 2021 11:20:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6e37dfbc-728c-44af-bc7b-b9539f46d874</guid><dc:creator>bachelor</dc:creator><description>&lt;p&gt;Thanks for taking the time to answer. It didn&amp;#39;t work for me though. But I have a solution now, I will post it in a separate answer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Starting a new thread from a BLE callback</title><link>https://devzone.nordicsemi.com/thread/315018?ContentTypeID=1</link><pubDate>Mon, 14 Jun 2021 00:09:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d9eaea7d-bd1a-4370-a210-98559d1dce02</guid><dc:creator>smackenzie</dc:creator><description>&lt;p&gt;I had a&amp;nbsp; similar problem trying to&amp;nbsp;start advertising from a uart interrupt.&lt;/p&gt;
&lt;p&gt;The problem came from zephyr attempting to aquire the lock for a &lt;span&gt;semaphore&lt;/span&gt;, but zephyr disallows this inside interrupts.&amp;nbsp;&lt;a href="https://github.com/zephyrproject-rtos/zephyr/blob/c9aa260f0cfc54453aef70f876e9ba9385428a30/kernel/sem.c#L121"&gt;https://github.com/zephyrproject-rtos/zephyr/blob/c9aa260f0cfc54453aef70f876e9ba9385428a30/kernel/sem.c#L121&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I suspect this is a similar issue.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;My solution was to create the thread on the main execution, then inside the uart interrupt, feed the data to the thread with a fifo.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;K_FIFO_DEFINE(rx_fifo);

static void uart_irq(...){
    uart_data_t * data = static_cast &amp;lt; uart_data_t *&amp;gt;(k_malloc(sizeof(uart_data_t)));
    ...
    k_fifo_put(&amp;amp;rx_fifo, data);
}


static void uart_hander_thread(void *, void *, void *) {
   uart_data_t* data_ptr = static_cast &amp;lt;uart_data_t*&amp;gt;(k_fifo_get(&amp;amp;rx_fifo, K_FOREVER));
   // Handle data
   
   k_free(data_ptr);
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I assume&amp;nbsp;using a worker thread would also resolve this, but I have not tested it.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://docs.zephyrproject.org/1.9.0/kernel/threads/workqueues.html"&gt;docs.zephyrproject.org/.../workqueues.html&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>