I know that each task in FreeRTOS should have a vTaskDelay() to release CPU resource to other tasks periodically. However, when there is a very time-consuming function in a task, like read a large amount of data from a slow device, other lower priority tasks need to wait a long time. According to my understanding, even I have added delay after this 'heavy' function, lower priority tasks still need to wait a long time before vTaskDelay() is executed. How can other lower priority tasks have the opportunity to use CPU resource while the priority of this 'heavy' task keeps unchanged?

    void vTask()
        func_heavy();      //this function take a lot of time
  • \$\begingroup\$ This is more Stack Overflow, but does FreeRTOS not have a preemptive scheduler? If not, you need to release the task halfway through and resume it later. Or decrease its priority the longer it runs and increase priority the longer it has to wait. Or use a DMA. What is this slow device? \$\endgroup\$
    – DKNguyen
    Feb 18, 2020 at 2:58
  • 1
    \$\begingroup\$ reading from the slow device should probably be handled by an interrupt service routine, use a timer interrupt if there is no dedicated interrupt for that hardware. \$\endgroup\$ Feb 18, 2020 at 3:06
  • \$\begingroup\$ If your task needs to wait for this slow device, then you should write the task in a way which allows the FreeRTOS scheduler to 'know' that the task is just twiddling its thumbs waiting. Use an interrupt (triggered by the slow device) to flag events or semaphores which the task will wait for. Simple 'delay' functions (like vTaskDelay) should be your absolute last resort here. \$\endgroup\$
    – brhans
    Feb 18, 2020 at 3:14
  • 2
    \$\begingroup\$ If you have all of your I/O properly interrupt-driven, then there should be lots of free time during that slow device I/O -- the processor should only be invoked for actual processing. \$\endgroup\$
    – TimWescott
    Feb 18, 2020 at 5:06
  • 2
    \$\begingroup\$ The concept "when there is a very time-consuming function in a task, like read a large amount of data from a slow device," is fundamentally wrong in any system with an even half decent RTOS. This is not FreeRTOS specific -> You should be able to convert a slow "wait on ready" task to a high priority one that either checks a ready flag often enough or that is triggered by a data ready event. Even a completely non-pre-emptive RTOS should be able to handle such requirements. \$\endgroup\$
    – Russell McMahon
    Feb 18, 2020 at 6:06


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.