This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

What happens if handlers are called at the same time? (nrf51422)

Hi,

I am new to firmware engineering. I have a simple and basic question.

If there'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 handler functions will be called at every same, right? If it's right, how does nrf51422(s130) handle this situation? Does it runs only one handler? Or both?

define A_TIMER_INTERVAL APP_TIMER_TICKS(100,APP_TIMER_PRESCALER)

define B_TIMER_INTERVAL APP_TIMER_TICKS(100,APP_TIMER_PRESCALER)

APP_TIMER_DEF(A_timer_id);

APP_TIMER_DEF(B_timer_id);

static void A_timeout_handler(void * p_context) {/* some codes */}

static void B_timeout_handler(void * p_context) {/* some codes */}

app_timer_create(&A_timer_id,APP_TIMER_MODE_REPEATED,A_timeout_handler);

app_timer_create(&B_timer_id,APP_TIMER_MODE_REPEATED,B_timeout_handler);

app_timer_start(A_timer_id,A_TIMER_INTERVAL,NULL);

app_timer_start(B_timer_id,B_TIMER_INTERVAL,NULL);

  • 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:

    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;}
    

    Next in your main loop:

    for (;;) {
    
    if (flag_A)
    
     {
    
         complicated_task_A();
    
         flag_A = false;
     
     }
    
    if (flag_B)
    
     {
    
         complicated_task_B();
    
         flag_B = false;
     
     }
    }
    

    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.

  • define A_TIMER_INTERVAL APP_TIMER_TICKS(10000,APP_TIMER_PRESCALER) //interval: 10 sec

    define B_TIMER_INTERVAL APP_TIMER_TICKS(10000,APP_TIMER_PRESCALER) //interval: 10 sec

    static void A_timeout_handler(void * p_context) {play_music_for_5s();}

    static void B_timeout_handler(void * p_context) {led_glowing_for_5s();}

    app_timer_start(A_timer_id,A_TIMER_INTERVAL,NULL);

    app_timer_start(B_timer_id,B_TIMER_INTERVAL,NULL);

    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?

  • So if task_B is something simple and it'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?

  • 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.

  • Is there any way to adjust priority of interrupt? To make sure timeout_handler doesn'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.

Related