【发布时间】:2017-07-05 16:53:24
【问题描述】:
在使用 STM32CubeMx 创建 FreeRTOS 应用项目时,您可以使用两种方法来引入延迟,即 osDelay 和 HAL_Delay。
它们之间有什么区别,应该首选哪一个?
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)
{
}
}
【问题讨论】:
-
显然从给定的代码(甚至不知道任何关于 RTOS 的情况下),第一个依赖于
vTaskDelay,第二个依赖于轮询。所以你基本上应该看看vTaskDelay的实现。
标签: c embedded stm32 microcontroller freertos