问题描述
在使用 STM32CubeMx 创建 FreeRTOS 应用项目时,您可以使用两种方法来引入延迟,即 osDelay 和 HAL_Delay.
While creating FreeRTOS application project with STM32CubeMx, there are two ways you can use to introduce delay, namely osDelay and HAL_Delay.
它们之间有什么区别,应该首选哪一个?
What's the difference among them and which one should be preferred?
osDelay代码:
/*********************** Generic Wait Functions *******************************/
/**
* @brief Wait for Timeout (Time Delay)
* @param millisec time delay value
* @retval status code that indicates the execution status of the function.
*/
osStatus osDelay (uint32_t millisec)
{
#if INCLUDE_vTaskDelay
TickType_t ticks = millisec / portTICK_PERIOD_MS;
vTaskDelay(ticks ? ticks : 1); /* Minimum delay = 1 tick */
return osOK;
#else
(void) millisec;
return osErrorResource;
#endif
}
HAL_Delay 代码:
/**
* @brief This function provides accurate delay (in milliseconds) based
* on variable incremented.
* @note In the default implementation , SysTick timer is the source of time base.
* It is used to generate interrupts at regular time intervals where uwTick
* is incremented.
* @note ThiS function is declared as __weak to be overwritten in case of other
* implementations in user file.
* @param Delay: specifies the delay time length, in milliseconds.
* @retval None
*/
__weak void HAL_Delay(__IO uint32_t Delay)
{
uint32_t tickstart = 0;
tickstart = HAL_GetTick();
while((HAL_GetTick() - tickstart) < Delay)
{
}
}
推荐答案
HAL_Delay 不是 FreeRTOS函数和 _osDelay 是围绕 FreeRTOS 函数构建的函数.(acc @Clifford:)它们都是完全不同的功能,由不同的开发人员用于不同的目的.
HAL_Delay is NOT a FreeRTOS function and _osDelay is a function built around FreeRTOS function. (acc @Clifford: ) They both are entirely different functions by different developers for different purposes.
osDelay 是 CMSIS 库 的一部分,并在内部使用 vTaskDelay() 来引入延迟,区别在于 的输入参数osDelay 是以毫秒为单位的延迟时间,而 _vTaskDelay() 的输入参数是要延迟的 Ticks 数.(acc. @Bence Kaulics:) 使用此功能,操作系统将收到延迟通知,并且操作系统将在特定时间段内将任务状态更改为阻塞.
osDelay is part of the CMSIS Library and uses vTaskDelay() internally to introduce delay with the difference that input argument of osDelay is delay time in milliseconds while the input argument of _vTaskDelay() is number of Ticks to be delayed. (acc. @Bence Kaulics:) Using this function, OS will be notified about the delay and OS will change the status of task to blocked for that particular time period.
HAL_Delay 是我们处理器的硬件抽象层的一部分.它基本上使用轮询来引入延迟.(acc. @Bence Kaulics:) 使用此功能,操作系统不会收到延迟通知.此外,如果您不使用操作系统,则 HAL_Delay 是 HAL 库提供的默认且唯一的阻塞延迟.(acc.@Clifford:)这是 HAL 库 的一部分,可以在没有 FreeRTOS 的情况下使用(或在 FreeRTOS 未运行时)
HAL_Delay is part of the hardware abstraction layer for our processor. It basically uses polling to introduce delay. (acc. @Bence Kaulics:) Using this function, OS won't be notified about the delay. Also if you do not use OS, then HAL_Delay is the default and only blocking delay to use provided by the HAL library. (acc. @Clifford: ) This is part of HAL Library and can be used without FreeRTOS (or when FreeRTOS is not running)
要使用 FreeRTOS 函数引入延迟,您可以使用 vTaskDelay() 或 vTaskDelayUntil() 在调度器启动后.
To introduce Delay using FreeRTOS functions, you can use vTaskDelay() or vTaskDelayUntil() after the scheduler has started.
(acc.@Clifford:)
如果您希望您的应用程序具有确定性,请始终支持 FreeRTOS API 函数.
CubeMX 是来自多个来源的部件的集合.
(acc. @Clifford: )
Always Favour FreeRTOS API function if you want your application to be deterministic.
CubeMX is a collection of parts from multiple sources.
这篇关于FreeRTOS:osDelay 与 HAL_delay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!