CMSIS-RTOS2 文档翻译 之 参考(CMSIS-RTOS2 API 之 通用等待函数)

通用等待函数

等待一段时间。更多...

函数

osStatus_t  osDelay (uint32_t ticks)
  等待超时(时间延迟)。更多...
 
osStatus_t  osDelayUntil (uint32_t ticks)
  等到指定的时间。更多...
 

描述

通用的等待函数提供了时间延迟的手段。

注意
通用等待函数不能从中断服务例程中调用。

函数文档

osStatus_t osDelay ( uint32_t  ticks )  
参数
[in] ticks 时间滴答值
返回
状态代码,指示该功能的执行状态。

函数 osDelay 等待内核 tick 中指定的时间段。对于值为 1 的系统等待,直到下一个计时器滴答发生。实际的时间延迟可能高达一个计时器小于指定的时间,即在下一个系统节拍发生之前调用 osDelay(1),线程立即重新调度。

延迟线程进入 BLOCKED 状态并立即发生上下文切换。在给定数量的滴答时间过后,线程会自动回到就绪状态。如果线程在 READY 状态下具有最高优先级,它将被立即调度。

可能的 osStatus_t 返回值:

  • osOK: 时间延迟被执行。
  • osErrorISR: osDelay 不能从中断服务程序调用。
注意
该函数不能从中断服务程序调用。

代码示例

#include "cmsis_os2.h"
void Thread_1 ( void *arg) { // Thread function
osStatus_t status; // capture the return status
uint32_t delayTime; // delay time in milliseconds
delayTime = 1000; // delay 1 second
status = osDelay (delayTime); // suspend thread execution
}

osStatus_t osDelayUntil ( uint32_t  ticks )  
参数
[in] ticks 绝对时间节拍
返回
状态代码,指示该功能的执行状态。

osDelayUntil 函数等待,直到达到绝对时间(在内核 ticks 中指定)。

当核心滴答计数器溢出时的情况由 osDelayUntil 处理。因此,提供一个低于当前刻度值的值(即由 osKernelGetTickCount 返回)是绝对合法的。通常作为用户,你不必关心溢出问题。您必须考虑的唯一限制是最大延迟限制为 (232)-1 节拍。

延迟线程进入 BLOCKED 状态并立即发生上下文切换。达到给定时间时,线程自动回到 READY 状态。如果线程在 READY 状态下具有最高优先级,它将被立即调度。

可能的 osStatus_t 返回值:

  • osOK: 时间延迟被执行。
  • osParameter: 时间不能处理(超出界限)。
  • osErrorISR: osDelayUntil 不能从中断服务程序中调用。
注意
该函数不能从中断服务程序调用。

代码示例

#include "cmsis_os2.h"
void Thread_1 ( void *arg) { // Thread function
uint32_t tick;
tick = osKernelGetTickCount(); // retrieve the number of system ticks
for (;;) {
tick += 1000; // delay 1000 ticks periodically
osDelayUntil(tick);
// ...
}
}

猜你喜欢

转载自blog.csdn.net/u012325601/article/details/80159402