<?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>What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/23810/what-happens-if-handlers-are-called-at-the-same-time-nrf51422</link><description>Hi, 
 I am new to firmware engineering. I have a simple and basic question. 
 If there&amp;#39;s Timer A and B, and both interval is 100ms, what happens to the timeout handler functions? If both timers start at the same time then also the predefined timeout</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Fri, 28 Jul 2017 09:19:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/23810/what-happens-if-handlers-are-called-at-the-same-time-nrf51422" /><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93666?ContentTypeID=1</link><pubDate>Fri, 28 Jul 2017 09:19:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:eea3bb76-4ebc-44de-a11b-ba6e66094c9a</guid><dc:creator>Jakub Rzeszutko</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;You can set interrupt priority using NVIC driver. To ensure that interrupt A will not block interrupt B you must assign to interupt A higher interrupt priority number (interrupt priority == 0 is the most important one and it will block all other).
For example&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;interrupt priority A = 7
interrupt priority B = 6
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In such case B can stop A execution.&lt;/p&gt;
&lt;p&gt;However in your case you can&amp;#39;t do it. APP_TIMER_DEF creates handlers on the same interrupt level.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93665?ContentTypeID=1</link><pubDate>Fri, 28 Jul 2017 08:49:35 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f0c82e8c-a796-49f5-bed0-1c33197deff4</guid><dc:creator>Yi, Changjae</dc:creator><description>&lt;p&gt;Is there any way to adjust priority of interrupt? To make sure timeout_handler doesn&amp;#39;t block interrupts of any other timeout_handler, what can be done? Your reply is really helpful than any other materials I found on internet. Thank you so much.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93669?ContentTypeID=1</link><pubDate>Fri, 28 Jul 2017 06:11:24 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1aca0c00-33a2-4849-a01f-a4a4e5400c11</guid><dc:creator>Jakub Rzeszutko</dc:creator><description>&lt;p&gt;Yes. If task A was working in main loop context it will be stopped, next short task B will be executed via interrupt handler and it will go back to task A execution.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93668?ContentTypeID=1</link><pubDate>Fri, 28 Jul 2017 01:11:51 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a5f9c13a-765b-427c-ae0c-9a53d6794999</guid><dc:creator>Yi, Changjae</dc:creator><description>&lt;p&gt;So if task_B is something simple and it&amp;#39;s inside timeout_handler, no matter how task_A is complicated, task_B works fine? While mcu is working on task_A(long task) if interrupt occurs, does mcu stops working on task_A and do task_B(short task)? After finishing task_B does mcu get back to the work that he has been doing?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93673?ContentTypeID=1</link><pubDate>Thu, 27 Jul 2017 15:12:38 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6d7f0311-6d7b-428c-b58d-588c666d0964</guid><dc:creator>Yi, Changjae</dc:creator><description>&lt;p&gt;define A_TIMER_INTERVAL APP_TIMER_TICKS(10000,APP_TIMER_PRESCALER) //interval: 10 sec&lt;/p&gt;
&lt;p&gt;define B_TIMER_INTERVAL APP_TIMER_TICKS(10000,APP_TIMER_PRESCALER) //interval: 10 sec&lt;/p&gt;
&lt;p&gt;static void A_timeout_handler(void * p_context) {play_music_for_5s();}&lt;/p&gt;
&lt;p&gt;static void B_timeout_handler(void * p_context) {led_glowing_for_5s();}&lt;/p&gt;
&lt;p&gt;app_timer_start(A_timer_id,A_TIMER_INTERVAL,NULL);&lt;/p&gt;
&lt;p&gt;app_timer_start(B_timer_id,B_TIMER_INTERVAL,NULL);&lt;/p&gt;
&lt;p&gt;If I want to make a device play a song while led is glowing for 5 seconds and repeating it every 10 second will this code work fine?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93671?ContentTypeID=1</link><pubDate>Thu, 27 Jul 2017 09:12:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e42f376f-9f5b-4f2a-aaed-397011f93043</guid><dc:creator>Jakub Rzeszutko</dc:creator><description>&lt;p&gt;I strongly recommend to not do it like you proposed. If you will execute complicated_task_A and complicated_task_B inside handlers you will block other interrupts which are on the same or lower level.
What you can do instead is for example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;volatile bool flag_A = false;

 volatile bool flag_B = false;

static void A_timeout_handler(void * p_context) {flag_A = true;}

static void B_timeout_handler(void * p_context) {flag_b = true;}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next in your main loop:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for (;;) {

if (flag_A)

 {

     complicated_task_A();

     flag_A = false;
 
 }

if (flag_B)

 {

     complicated_task_B();

     flag_B = false;
 
 }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It is not possible on 1 core to do it in the same time. However if task B is only to blink a LED (short task) you can leve it inside timout_handler. In such case when  complicated_task_A(); is executed within a main loop B_timeout_handler can realize LED blink from interrupt context. It will look like both tasks are executed at the same time.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93670?ContentTypeID=1</link><pubDate>Thu, 27 Jul 2017 08:47:53 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1429e888-98d8-4ae0-8ea6-e0cba5d5aff7</guid><dc:creator>Yi, Changjae</dc:creator><description>&lt;p&gt;I really appreciate your kind answer! I have one last question and hope it won&amp;#39;t bother you... If these handlers are defined like this,&lt;/p&gt;
&lt;p&gt;&amp;quot;static void A_timeout_handler(void * p_context) { complicated_task_A();}&lt;/p&gt;
&lt;p&gt;static void B_timeout_handler(void * p_context) { complicated_task_B(); }&amp;quot;&lt;/p&gt;
&lt;p&gt;and assuming these &amp;quot;tasks&amp;quot; are so complicated that it will take a some time to be done, these tasks will never be run at the same time? Does task_B always be start after the task_A is done? Is it not possible to play music and blink lights at the same time when mcu is single core?&lt;/p&gt;
&lt;p&gt;Thank you for reading this article written in nasty grammar. I am really looking forward for your answer.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93672?ContentTypeID=1</link><pubDate>Thu, 27 Jul 2017 02:55:40 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6287a042-a2e5-42db-9239-742bc788afc7</guid><dc:creator>RK</dc:creator><description>&lt;p&gt;They&amp;#39;re totally independent timers. You start them, the start functions return immediately, the timers run from the time they were started. So the led blinks every 100ms and after 50ms absolutly nothing happens.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93667?ContentTypeID=1</link><pubDate>Wed, 26 Jul 2017 14:04:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:862a90dc-04b3-46a6-9e84-24639e9fafc8</guid><dc:creator>Jakub Rzeszutko</dc:creator><description>&lt;p&gt;In such scenario:&lt;/p&gt;
&lt;p&gt;A_timeout_handler will be called every 100ms - timer interval is counted between function calls.&lt;/p&gt;
&lt;p&gt;B_timer will be called also every 100ms but with 50ms offset to A_timeout_handler due to blocking time in A_timeout_handler.&lt;/p&gt;
&lt;p&gt;So at the end LED will be blinking every 100ms.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93674?ContentTypeID=1</link><pubDate>Wed, 26 Jul 2017 13:55:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:bb8b3da4-6027-49d2-8e39-6ec892c224cd</guid><dc:creator>Yi, Changjae</dc:creator><description>&lt;p&gt;To Jakub Rzeszutko,&lt;/p&gt;
&lt;p&gt;Oh, you&amp;#39;re right. I think the question was wrong. If handler functions of timer A,B is defined&lt;/p&gt;
&lt;p&gt;static void A_timeout_handler(void p_context) {nrf_delay_ms(50);}
static void B_timeout_handler(void p_context) {led_blink();}&lt;/p&gt;
&lt;p&gt;like this, then what happens? Does light blinks right after the program starts and blinks every 100ms? Or waits 50ms and blinks?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: What happens if handlers are called at the same time? (nrf51422)</title><link>https://devzone.nordicsemi.com/thread/93664?ContentTypeID=1</link><pubDate>Wed, 26 Jul 2017 13:35:52 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d290e972-eb97-481b-bf90-39b13fcb8a20</guid><dc:creator>Jakub Rzeszutko</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;First of all these timers don&amp;#39;t start at the same time. In your code A_timer_id was started before B_timer_id. Both hadlers will be called - one after another.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>