FreeRTOS_vTaskDelayUntil

FreeRTOS_vTaskDelayUntil

笔者在音频项目上使用vTaskDelayUntil函数时遇到一个奇怪的现象,就是vTaskDelayUntil绝对延时不精准。

笔者第一次使用如下:

void vCyclicTaskFunction( void * pvParameters )
{
	static TickType_t xLastWakeTime;
	for( ;; )
	{
		printf("hello world!\n");
		vTaskDelayUntil( &xLastWakeTime, xPeriod );
	} 
}

笔者第二次使用时,写法如下:

void vCyclicTaskFunction( void * pvParameters )
{
	TickType_t xLastWakeTime;
	for( ;; )
	{
		printf("hello world!\n");
		vTaskDelayUntil( &xLastWakeTime, xPeriod );
	} 
}

这两种写法唯一的区别就是xLastWakeTime局部变量有没有static修饰。第一种写法,笔者测试过是正常的绝对延时,然而第二种却不是绝对延时。笔者一直都找不到问题所在,于是查看了FreeRTOS官方手册,有以下范例:

/* Define a task that performs an action every 50 milliseconds. */
void vCyclicTaskFunction( void * pvParameters )
{
	TickType_t xLastWakeTime;
	const TickType_t xPeriod = pdMS_TO_TICKS( 50 );
	/* The xLastWakeTime variable needs to be initialized with the current tick
	count. Note that this is the only time the variable is written to explicitly.
	After this assignment, xLastWakeTime is updated automatically internally within
	vTaskDelayUntil(). */
	xLastWakeTime = xTaskGetTickCount();
	
	/* Enter the loop that defines the task behavior. */
	for( ;; )
	{
	/* This task should execute every 50 milliseconds. Time is measured 
	in ticks. The pdMS_TO_TICKS macro is used to convert milliseconds 
	into ticks. xLastWakeTime is automatically updated within vTaskDelayUntil() 
	so is not explicitly updated by the task. */
	vTaskDelayUntil( &xLastWakeTime, xPeriod );
	
	/* Perform the periodic actions here. */
	} 
}

范例中的xLastWakeTime也没有static修饰,但笔者确实测试过是绝对的延时。再仔细看注释内容,如下:

 /* The xLastWakeTime variable needs to be initialized with the current tick
    count. Note that this is the only time the variable is written to explicitly.
    After this assignment, xLastWakeTime is updated automatically internally within
    vTaskDelayUntil(). */

xLastWakeTime变量需要用当前滴答值初始化计数。 注意,这是唯一一次显式地写入变量。在这个赋值之后,xLastWakeTime会在内部自动更新 vTaskDelayUntil()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值