<?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 process a queue in the background?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/30735/how-to-process-a-queue-in-the-background</link><description>nRF52832 SDK 14.2 
 Hi. 
 I would like to know how I can run a function in the background? In my case, I want to process a queue in the background. Here is the scneario: 
 Let&amp;#39;s say I have two functions fooA(..) and fooB(...). If I call fooA(..), item</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Thu, 22 Feb 2018 14:42:56 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/30735/how-to-process-a-queue-in-the-background" /><item><title>RE: How to process a queue in the background?</title><link>https://devzone.nordicsemi.com/thread/121693?ContentTypeID=1</link><pubDate>Thu, 22 Feb 2018 14:42:56 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f95bf029-b08c-4df0-8129-24ba1f0848b9</guid><dc:creator>H&amp;#229;kon Alseth</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This is commonly done using some sort of scheduler, to reduce the processing time in interrupts and change priority level from interrupt context to thread (also called main) context.&lt;/p&gt;
&lt;p&gt;We have the library app_scheduler than can do this, where you add the call to&amp;nbsp;app_sched_execute() to the main loop.&lt;/p&gt;
&lt;p&gt;To add an item to the queue, you can call&amp;nbsp;&lt;span&gt;app_sched_event_put&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span&gt;NULL&lt;/span&gt;&lt;span&gt;, &lt;/span&gt;&lt;span&gt;0&lt;/span&gt;&lt;span&gt;, my_function); and it will get processed when you enter the main for-loop.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Many BLE-examples (ble_app_hids_*) has the scheduler&amp;nbsp;initialized and uses this for BLE events and app_timer handlers, and the nfc\writeable_ndef_msg uses it as well.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Most RTOSes have the same concept available.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;You can also go the other way around. Everytime fooA or fooB is called, you set a interrupt pending, and have a software interrupt in your module, here shown with some crude code:&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;ret_t fooA(...)
{
  ...
  ret = add_to_queue(fooA);
  if ( ret != success)
    NVIC_SetPending(SWIx_IRQn);
  return ret;
}

ret_t fooB(...)
{
  ...
  ret = add_to_queue(fooB);
  if ( ret != success)
    NVIC_SetPending(SWIx_IRQn);
  return ret;
}

void SWIx_IRQHandler(void)
{
  process_queue();
}

static ret_t fooA_process(...)
{
  ...
}

static ret_t fooB_process(...)
{
  ...
}&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;It all depends on how this module is hooked into the rest of your system.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Cheers,&lt;/p&gt;
&lt;p&gt;Håkon&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>