vTaskDelay和_delay_ms之间的行为差异是什么?

时间:2022-11-21 23:43:47

1. Introduction

1。介绍

I cannot seem to find information or a detailed explanation about the behavioral differences between the following functions in a FreeRTOS task:

在FreeRTOS的任务中,我似乎找不到关于以下功能之间行为差异的信息或详细解释:

  • vTaskDelay
  • vTaskDelay
  • _delay_ms
  • _delay_ms

2. Code

2。代码

Suppose you have the following codes:

假设您有以下代码:

IdleHook + task creation

IdleHook +任务创建

Long value = 0;

void vApplicationIdleHook( void ) {
    while(1)
    {
        // empty
    }
}

int main(void)
{
     xTaskCreate(TaskIncrement, (const portCHAR *)"up" , 256, NULL, 2, NULL );
     xTaskCreate(TaskDecrement, (const portCHAR *)"down" , 256, NULL, 1, NULL );

     vTaskStartScheduler();
}

Tasks with vTaskDelay

任务vTaskDelay

static void TaskDecrement(void *param)
{
    while(1)
    {
        for(unsigned long i=0; i < 123; i++) {
            //semaphore take
            value--;
            //semaphore give
        }
        vTaskDelay(100);
    }
}

static void TaskIncrement(void *param)
{
    while(1)
    {
        for(unsigned long i=0; i < 123; i++) {
            //semaphore take
            value++;
            //semaphore give
        }
        vTaskDelay(100);
    }
}

Tasks with _delay_ms

任务_delay_ms

static void TaskDecrement(void *param)
{
    while(1)
    {
        for(unsigned long i=0; i < 123; i++) {
            //semaphore take
            value--;
            //semaphore give
        }
        _delay_ms(100);
    }
}

static void TaskIncrement(void *param)
{
    while(1)
    {
        for(unsigned long i=0; i < 123; i++) {
            //semaphore take
            value++;
            //semaphore give
        }
        _delay_ms(100);
    }
}

3. Question

3所示。问题

What happens with the flow of the program when the tasks are provided with a vTaskDelay versus _delay_ms?

当为任务提供vTaskDelay和_delay_ms时,程序流会发生什么?

Note: the two given example tasks have different priorities.

注意:这两个示例任务有不同的优先级。

1 个解决方案

#1


3  

From https://www.freertos.org/Documentation/FreeRTOS_Reference_Manual_V10.0.0.pdf:

从https://www.freertos.org/Documentation/FreeRTOS_Reference_Manual_V10.0.0.pdf:

Places the task that calls vTaskDelay() into the Blocked state for a fixed number of tick interrupts. Specifying a delay period of zero ticks will not result in the calling task being placed into the Blocked state, but will result in the calling task yielding to any Ready state tasks that share its priority. Calling vTaskDelay(0) is equivalent to calling taskYIELD().

将调用vTaskDelay()的任务设置为阻塞状态,以获取固定数量的滴答中断。指定一个0节拍的延迟周期不会导致调用任务被放置到阻塞状态,而是会导致调用任务屈服于共享其优先级的任何就绪状态任务。调用vTaskDelay(0)等同于调用taskYIELD()。

I think you get the idea already, but if you have multiple tasks created, then vTaskDelay() will put the running task into the "Blocked" state for the specified number of tick interrupts (not milliseconds!) and allow the task with the next highest priority to run until it yields control (or gets preempted, depending on your FreeRTOS configuration).

我觉得你已经明白我的意思,但如果你有多个任务创建,然后vTaskDelay()将运行的任务进入“阻塞”状态的指定数量的蜱虫中断(而不是毫秒!),允许下一个优先级最高的任务运行,直到它得到控制(或者被抢占,取决于你FreeRTOS配置)。

I don't think _delay_ms() is part of the FreeRTOS library. Are you sure it's not a platform specific function? My guess is, if the highest priority task calls _delay_ms(), then it will result in a busy wait. Otherwise, a task with a higher priority may preempt the task that calls _delay_ms() as it is delaying (that is, _delay_ms() will not yield control immediately).

我不认为_delay_ms()是FreeRTOS库的一部分。你确定它不是一个特定于平台的功能吗?我的猜测是,如果最高优先级的任务调用_delay_ms(),那么它将导致繁忙的等待。否则,具有较高优先级的任务可能会抢占调用_delay_ms()的任务,因为它正在延迟(即,_delay_ms()不会立即产生控制权)。

Perhaps a better summary of the above: in a multitasking application, _delay_ms() is not deterministic.

也许可以更好地总结一下上面的内容:在多任务应用程序中,_delay_ms()不是确定性的。

#1


3  

From https://www.freertos.org/Documentation/FreeRTOS_Reference_Manual_V10.0.0.pdf:

从https://www.freertos.org/Documentation/FreeRTOS_Reference_Manual_V10.0.0.pdf:

Places the task that calls vTaskDelay() into the Blocked state for a fixed number of tick interrupts. Specifying a delay period of zero ticks will not result in the calling task being placed into the Blocked state, but will result in the calling task yielding to any Ready state tasks that share its priority. Calling vTaskDelay(0) is equivalent to calling taskYIELD().

将调用vTaskDelay()的任务设置为阻塞状态,以获取固定数量的滴答中断。指定一个0节拍的延迟周期不会导致调用任务被放置到阻塞状态,而是会导致调用任务屈服于共享其优先级的任何就绪状态任务。调用vTaskDelay(0)等同于调用taskYIELD()。

I think you get the idea already, but if you have multiple tasks created, then vTaskDelay() will put the running task into the "Blocked" state for the specified number of tick interrupts (not milliseconds!) and allow the task with the next highest priority to run until it yields control (or gets preempted, depending on your FreeRTOS configuration).

我觉得你已经明白我的意思,但如果你有多个任务创建,然后vTaskDelay()将运行的任务进入“阻塞”状态的指定数量的蜱虫中断(而不是毫秒!),允许下一个优先级最高的任务运行,直到它得到控制(或者被抢占,取决于你FreeRTOS配置)。

I don't think _delay_ms() is part of the FreeRTOS library. Are you sure it's not a platform specific function? My guess is, if the highest priority task calls _delay_ms(), then it will result in a busy wait. Otherwise, a task with a higher priority may preempt the task that calls _delay_ms() as it is delaying (that is, _delay_ms() will not yield control immediately).

我不认为_delay_ms()是FreeRTOS库的一部分。你确定它不是一个特定于平台的功能吗?我的猜测是,如果最高优先级的任务调用_delay_ms(),那么它将导致繁忙的等待。否则,具有较高优先级的任务可能会抢占调用_delay_ms()的任务,因为它正在延迟(即,_delay_ms()不会立即产生控制权)。

Perhaps a better summary of the above: in a multitasking application, _delay_ms() is not deterministic.

也许可以更好地总结一下上面的内容:在多任务应用程序中,_delay_ms()不是确定性的。