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

FreeRTOS and app_scheduler combined

Hi all,

For a project I need to use a private library which uses the app_scheduler to queue events triggered from a second MCU. I am using FreeRTOS on the nRF and I am unsure how to make the app_scheduler work together with FreeRTOS. The supplier of the library compiled it with the FreeRTOS version of app_timer. So it should be compatible.

After reading the app_scheduler documentation I figured I need to initialise it first. I do this before calling the vTaskStartScheduler() function. But I read I also need to call app_sched_execute() in my main loop to check for items in the event queue and process them. 

Now vTaskStartScheduler() does not return, so I don't have such a literal main loop. I am wondering how to make sure tasks in the app_scheduler queue still get executed.

What is the best way to achieve this? I'm currently thinking about creating a task/thread which wil just call app_sched_execute() whenever it gets CPU time but it seems a bit ugly. Is there a cleaner way to use both FreeRTOS and the app_scheduler at the same time?

P.S I also found a post asking the same question but it was never answered: devzone.nordicsemi.com/.../freertos-and-app_scheduler

Parents
  • Hi Jur,

    After reading the app_scheduler documentation I figured I need to initialise it first. I do this before calling the vTaskStartScheduler() function. But I read I also need to call app_sched_execute() in my main loop to check for items in the event queue and process them. 

    You can enable the application idle hook in FreeRTOSConfig.h and call the  app_sched_execute in it so that it gets called from the idle_task.. It suits very well in your case. I do not see any conflicts in using app_scheduler when executing the queued contexts from the application idle hook.

  • This worked out great, but now I would like to use the tickless idle to save some more power A verified answer made by you explains how USE_TICKLESS_IDLE and USE_IDLE_HOOK should not be used at the same time.

    What do you think about the following approach:

    Adding the call to app_sched_execute() to the top of the vPortSuppressTicksAndSleep() function defined in port_cmsis_systick.c .

  • Jur, 

    The developer in the thread link you provided was using sleep function both in idle_hook and also enabled the tickless idle (tickless idle one of the main task is to handle sleep). My recommendation to the customer was not to use sleep in both of these (idle_hook and tickless idle) as it is not necessary and also incorrect.

    In your case, i assume you are only using app_scheduler in idle_hook and not making the chip go into any sleep states? If this is the case, then using both idle_hook and tickless idle should work very well together in my opinion.

  •  Thanks for your reply! I am sorry I should have explained! I was making the chip go to sleep. So in the idle hook, I would call app_sched_execute() and after that make the chip go to sleep using sd_app_evt_wait(). So I was actually doing the same as the developer in the thread I linked.

    Meanwhile I did the following:
    I added a call to app_sched_execute() to the top of the vPortSuppressTicksAndSleep() function defined in port_cmsis_systick.c .

    This works great but it is not the cleanest solution. So do I understand correctly that I can use tickless idle and idle hook together when not making the chip sleep in the idle hook? In that case I can use tickless idle for sleeping and the idle hook for just calling app_sched_execute(). Will this first execute the idle hook and after completing this go into tickless idle? Or is there no way to guarantee that the hook will be executed before going into tickless idle?

  • Jur said:
    This works great but it is not the cleanest solution. So do I understand correctly that I can use tickless idle and idle hook together when not making the chip sleep in the idle hook?

     Firstly sorry for the late reply. I was sick and just returned to work today.

    Yes your understanding is correct. The idea of enabling tickless idle is to give the RTOS control on sleep states of the chip. So when enabling tickless idle, the sleep state should only be at one place for the RTOS to properly have control on the ticks and later adjusting the missing ticks.

    Using idle hook and tickless idle is completely ok as long as you do not use any sleep states in the idle hook.

     

    Jur said:
    So do I understand correctly that I can use tickless idle and idle hook together when not making the chip sleep in the idle hook? In that case I can use tickless idle for sleeping and the idle hook for just calling app_sched_execute().

     correct.

Reply
  • Jur said:
    This works great but it is not the cleanest solution. So do I understand correctly that I can use tickless idle and idle hook together when not making the chip sleep in the idle hook?

     Firstly sorry for the late reply. I was sick and just returned to work today.

    Yes your understanding is correct. The idea of enabling tickless idle is to give the RTOS control on sleep states of the chip. So when enabling tickless idle, the sleep state should only be at one place for the RTOS to properly have control on the ticks and later adjusting the missing ticks.

    Using idle hook and tickless idle is completely ok as long as you do not use any sleep states in the idle hook.

     

    Jur said:
    So do I understand correctly that I can use tickless idle and idle hook together when not making the chip sleep in the idle hook? In that case I can use tickless idle for sleeping and the idle hook for just calling app_sched_execute().

     correct.

Children
Related