获得更多资料欢迎进入我的网站或者 csdn或者博客园

对于有热心的小伙伴在微博上私信我,说我的uC/OS-II 一些函数简介篇幅有些过于长应该分开介绍。应小伙伴的要求,特此将文章分开进行讲解。上文主要介绍了任务相关的函数,本文介绍时间相关的函数:OSTimeDly()延时节拍函数,OSTimeDlyHMSM()系统延时函数,OSTimeDlyResume()延时恢复函数

OSTimeDly()延时节拍函数

1、主要作用:调用该函数的任务将自己延时一段时间并执行一次任务调度,一旦规定的延时时间完成或有其它的任务通过调用OSTimeDlyResume()取消了延时,调用OSTimeDly()函数的任务马上进入就绪状态(前提是先将任务调度后执行的任务执行到程序尾,且调用OSTimeDly的任务此时优先级最高)。

2、函数原型:void OSTimeDly (INT16U ticks);

3、参数说明:ticks为需要延时的时钟节拍数;

4、返回值:无

5、函数主体在os_time.c中

OSTimeDlyHMSM()系统延时函数

1、主要作用:函数是以小时(H)、分(M)、秒(S)和毫秒(m)四个参数来定义延时时间的,函数在内部把这些参数转换为时钟节拍,再通过单次或多次调用OSTimeDly()进行延时和任务调度,所以延时原理和调用延时函数OSTimeDly()是一样的。调用 OSTimeDlyHMSM() 后,如果延时时间不为0,系统将立即进行任务调度。

2、函数原型:INT8U OSTimeDlyHMSM (INT8U hours,INT8U minutes,INT8U seconds,INT16U milli);

3、参数说明:

hours 为延时小时数,范围从0-255。

minutes 为延时分钟数,范围从0-59

seconds 为延时秒数,范围从0-59

milli 为延时毫秒数,范围从0-999

4、返回值说明:

OS_NO_ERR:函数调用成功。

OS_TIME_INVALID_MINUTES:参数错误,分钟数大于59。

OS_TIME_INVALID_SECONDS:参数错误,秒数大于59。

OS_TIME_INVALID_MILLI:参数错误,毫秒数大于999。

OS_TIME_ZERO_DLY:四个参数全为0。

5、函数主体在os_time.c中

OSTimeDlyResume()延时恢复函数

1、主要作用:任务在延时之后,进入阻塞态。当延时时间到了就从阻塞态恢复到就绪态,可以被操作系统调度执行。但是,并非回到就绪态就只有这么一种可能,因为即便任务的延时时间没到,还是可以通过函数OSTimeDlyResume恢复该任务到就绪态的。另外,OSTimeDlyResume也不仅仅能恢复使用OSTimeDly或OSTimeDlyHMSM而延时的任务。对于因等待事件发生而阻塞的,并且设置了超时(timeout)时间的任务,也可以使用OSTimeDlyResume来恢复。对这些任务使用了OSTimeDlyResume,就好像已经等待超时了一样。但是,对于采用OSTaskSuspend挂起的任务,是不允许采用OSTimeDlyResume来恢复的。

2、函数原型:INT8U OSTimeDlyResume (INT8U prio)

3.参数说明:prio 被恢复任务的优先级

4、返回值:

OS_ERR_TASK_NOT_EXIST:任务优先级指针表中没有此任务

OS_NO_ERR:函数调用成功。

OS_ERR_PRIO_INVALID:参数指定的优先级大于或等于OS_LOWEST_PRIO。

OS_ERR_TIME_NOT_DLY:任务没有被延时阻塞

5、函数主体在os_time.c中

附os_time.c代码

  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-II
  4. * The Real-Time Kernel
  5. * TIME MANAGEMENT
  6. *
  7. * (c) Copyright 1992-2013, Micrium, Weston, FL
  8. * All Rights Reserved
  9. *
  10. * File : OS_TIME.C
  11. * By : Jean J. Labrosse
  12. * Version : V2.92.08
  13. *
  14. * LICENSING TERMS:
  15. * ---------------
  16. * uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
  17. * If you plan on using uC/OS-II in a commercial product you need to contact Micrium to properly license
  18. * its use in your product. We provide ALL the source code for your convenience and to help you experience
  19. * uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a
  20. * licensing fee.
  21. *********************************************************************************************************
  22. */
  23. #define MICRIUM_SOURCE
  24. #ifndef OS_MASTER_FILE
  25. #include <ucos_ii.h>
  26. #endif
  27. /*
  28. *********************************************************************************************************
  29. * DELAY TASK 'n' TICKS
  30. *
  31. * Description: This function is called to delay execution of the currently running task until the
  32. * specified number of system ticks expires. This, of course, directly equates to delaying
  33. * the current task for some time to expire. No delay will result If the specified delay is
  34. * 0. If the specified delay is greater than 0 then, a context switch will result.
  35. *
  36. * Arguments : ticks is the time delay that the task will be suspended in number of clock 'ticks'.
  37. * Note that by specifying 0, the task will not be delayed.
  38. *
  39. * Returns : none
  40. *********************************************************************************************************
  41. */
  42. void OSTimeDly (INT32U ticks)
  43. {
  44. INT8U y;
  45. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  46. OS_CPU_SR cpu_sr = 0u;
  47. #endif
  48. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  49. return;
  50. }
  51. if (OSLockNesting > 0u) { /* See if called with scheduler locked */
  52. return;
  53. }
  54. if (ticks > 0u) { /* 0 means no delay! */
  55. OS_ENTER_CRITICAL();
  56. y = OSTCBCur->OSTCBY; /* Delay current task */
  57. OSRdyTbl[y] &= (OS_PRIO)~OSTCBCur->OSTCBBitX;
  58. if (OSRdyTbl[y] == 0u) {
  59. OSRdyGrp &= (OS_PRIO)~OSTCBCur->OSTCBBitY;
  60. }
  61. OSTCBCur->OSTCBDly = ticks; /* Load ticks in TCB */
  62. OS_EXIT_CRITICAL();
  63. OS_Sched(); /* Find next task to run! */
  64. }
  65. }
  66. /*$PAGE*/
  67. /*
  68. *********************************************************************************************************
  69. * DELAY TASK FOR SPECIFIED TIME
  70. *
  71. * Description: This function is called to delay execution of the currently running task until some time
  72. * expires. This call allows you to specify the delay time in HOURS, MINUTES, SECONDS and
  73. * MILLISECONDS instead of ticks.
  74. *
  75. * Arguments : hours specifies the number of hours that the task will be delayed (max. is 255)
  76. * minutes specifies the number of minutes (max. 59)
  77. * seconds specifies the number of seconds (max. 59)
  78. * ms specifies the number of milliseconds (max. 999)
  79. *
  80. * Returns : OS_ERR_NONE
  81. * OS_ERR_TIME_INVALID_MINUTES
  82. * OS_ERR_TIME_INVALID_SECONDS
  83. * OS_ERR_TIME_INVALID_MS
  84. * OS_ERR_TIME_ZERO_DLY
  85. * OS_ERR_TIME_DLY_ISR
  86. *
  87. * Note(s) : The resolution on the milliseconds depends on the tick rate. For example, you can't do
  88. * a 10 mS delay if the ticker interrupts every 100 mS. In this case, the delay would be
  89. * set to 0. The actual delay is rounded to the nearest tick.
  90. *********************************************************************************************************
  91. */
  92. #if OS_TIME_DLY_HMSM_EN > 0u
  93. INT8U OSTimeDlyHMSM (INT8U hours,
  94. INT8U minutes,
  95. INT8U seconds,
  96. INT16U ms)
  97. {
  98. INT32U ticks;
  99. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  100. return (OS_ERR_TIME_DLY_ISR);
  101. }
  102. if (OSLockNesting > 0u) { /* See if called with scheduler locked */
  103. return (OS_ERR_SCHED_LOCKED);
  104. }
  105. #if OS_ARG_CHK_EN > 0u
  106. if (hours == 0u) {
  107. if (minutes == 0u) {
  108. if (seconds == 0u) {
  109. if (ms == 0u) {
  110. return (OS_ERR_TIME_ZERO_DLY);
  111. }
  112. }
  113. }
  114. }
  115. if (minutes > 59u) {
  116. return (OS_ERR_TIME_INVALID_MINUTES); /* Validate arguments to be within range */
  117. }
  118. if (seconds > 59u) {
  119. return (OS_ERR_TIME_INVALID_SECONDS);
  120. }
  121. if (ms > 999u) {
  122. return (OS_ERR_TIME_INVALID_MS);
  123. }
  124. #endif
  125. /* Compute the total number of clock ticks required.. */
  126. /* .. (rounded to the nearest tick) */
  127. ticks = ((INT32U)hours * 3600uL + (INT32U)minutes * 60uL + (INT32U)seconds) * OS_TICKS_PER_SEC
  128. + OS_TICKS_PER_SEC * ((INT32U)ms + 500uL / OS_TICKS_PER_SEC) / 1000uL;
  129. OSTimeDly(ticks);
  130. return (OS_ERR_NONE);
  131. }
  132. #endif
  133. /*$PAGE*/
  134. /*
  135. *********************************************************************************************************
  136. * RESUME A DELAYED TASK
  137. *
  138. * Description: This function is used resume a task that has been delayed through a call to either
  139. * OSTimeDly() or OSTimeDlyHMSM(). Note that you can call this function to resume a
  140. * task that is waiting for an event with timeout. This would make the task look
  141. * like a timeout occurred.
  142. *
  143. * Arguments : prio specifies the priority of the task to resume
  144. *
  145. * Returns : OS_ERR_NONE Task has been resumed
  146. * OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum allowed
  147. * (i.e. >= OS_LOWEST_PRIO)
  148. * OS_ERR_TIME_NOT_DLY Task is not waiting for time to expire
  149. * OS_ERR_TASK_NOT_EXIST The desired task has not been created or has been assigned to a Mutex.
  150. *********************************************************************************************************
  151. */
  152. #if OS_TIME_DLY_RESUME_EN > 0u
  153. INT8U OSTimeDlyResume (INT8U prio)
  154. {
  155. OS_TCB *ptcb;
  156. #if OS_CRITICAL_METHOD == 3u /* Storage for CPU status register */
  157. OS_CPU_SR cpu_sr = 0u;
  158. #endif
  159. if (prio >= OS_LOWEST_PRIO) {
  160. return (OS_ERR_PRIO_INVALID);
  161. }
  162. OS_ENTER_CRITICAL();
  163. ptcb = OSTCBPrioTbl[prio]; /* Make sure that task exist */
  164. if (ptcb == (OS_TCB *)0) {
  165. OS_EXIT_CRITICAL();
  166. return (OS_ERR_TASK_NOT_EXIST); /* The task does not exist */
  167. }
  168. if (ptcb == OS_TCB_RESERVED) {
  169. OS_EXIT_CRITICAL();
  170. return (OS_ERR_TASK_NOT_EXIST); /* The task does not exist */
  171. }
  172. if (ptcb->OSTCBDly == 0u) { /* See if task is delayed */
  173. OS_EXIT_CRITICAL();
  174. return (OS_ERR_TIME_NOT_DLY); /* Indicate that task was not delayed */
  175. }
  176. ptcb->OSTCBDly = 0u; /* Clear the time delay */
  177. if ((ptcb->OSTCBStat & OS_STAT_PEND_ANY) != OS_STAT_RDY) {
  178. ptcb->OSTCBStat &= ~OS_STAT_PEND_ANY; /* Yes, Clear status flag */
  179. ptcb->OSTCBStatPend = OS_STAT_PEND_TO; /* Indicate PEND timeout */
  180. } else {
  181. ptcb->OSTCBStatPend = OS_STAT_PEND_OK;
  182. }
  183. if ((ptcb->OSTCBStat & OS_STAT_SUSPEND) == OS_STAT_RDY) { /* Is task suspended? */
  184. OSRdyGrp |= ptcb->OSTCBBitY; /* No, Make ready */
  185. OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
  186. OS_EXIT_CRITICAL();
  187. OS_Sched(); /* See if this is new highest priority */
  188. } else {
  189. OS_EXIT_CRITICAL(); /* Task may be suspended */
  190. }
  191. return (OS_ERR_NONE);
  192. }
  193. #endif
  194. /*$PAGE*/
  195. /*
  196. *********************************************************************************************************
  197. * GET CURRENT SYSTEM TIME
  198. *
  199. * Description: This function is used by your application to obtain the current value of the 32-bit
  200. * counter which keeps track of the number of clock ticks.
  201. *
  202. * Arguments : none
  203. *
  204. * Returns : The current value of OSTime
  205. *********************************************************************************************************
  206. */
  207. #if OS_TIME_GET_SET_EN > 0u
  208. INT32U OSTimeGet (void)
  209. {
  210. INT32U ticks;
  211. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  212. OS_CPU_SR cpu_sr = 0u;
  213. #endif
  214. OS_ENTER_CRITICAL();
  215. ticks = OSTime;
  216. OS_EXIT_CRITICAL();
  217. return (ticks);
  218. }
  219. #endif
  220. /*
  221. *********************************************************************************************************
  222. * SET SYSTEM CLOCK
  223. *
  224. * Description: This function sets the 32-bit counter which keeps track of the number of clock ticks.
  225. *
  226. * Arguments : ticks specifies the new value that OSTime needs to take.
  227. *
  228. * Returns : none
  229. *********************************************************************************************************
  230. */
  231. #if OS_TIME_GET_SET_EN > 0u
  232. void OSTimeSet (INT32U ticks)
  233. {
  234. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  235. OS_CPU_SR cpu_sr = 0u;
  236. #endif
  237. OS_ENTER_CRITICAL();
  238. OSTime = ticks;
  239. OS_EXIT_CRITICAL();
  240. }
  241. #endif

uC/OS-II 函数之时间相关函数的更多相关文章

  1. uC/OS II 函数说明 之–OSTaskCreate()与OSTaskCreateExt()

    1. OSTaskCreate()    OSTaskCreate()建立一个新任务,能够在多任务环境启动之前,或者执行任务中建立任务.注意,ISR中禁止建立任务,一个任务必须为无限循环结构.    ...

  2. uC/OS II原理分析及源码阅读(一)

    uC/OS II(Micro Control Operation System Two)是一个可以基于ROM运行的.可裁减的.抢占式.实时多任务内核,具有高度可移植性,特别适合于微处理器和控制器,是和 ...

  3. 【原创】uC/OS II 任务切换原理

    今天学习了uC/OS II的任务切换,知道要实现任务的切换,要将原先任务的寄存器压入任务堆栈,再将新任务中任务堆栈的寄存器内容弹出到CPU的寄存器,其中的CS.IP寄存器没有出栈和入栈指令,所以只能引 ...

  4. 【小梅哥SOPC学习笔记】NIOS II处理器运行UC/OS II

    SOPC开发流程之NIOS II 处理器运行 UC/OS II 这里以在芯航线FPGA学习套件的核心板上搭建 NIOS II 软核并运行 UCOS II操作系统为例介绍SOPC的开发流程. 第一步:建 ...

  5. uC/OS-II 一些函数简介

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 以前搞硬件的经验,最近突然翻出来了.分享给大家:主要讲解uC/OS-II常用函数:虽说现在转行软件了,但是感觉之前搞硬件的经验还真是很有用对于理解 ...

  6. uc/os iii移植到STM32F4---IAR开发环境

    也许是先入为主的原因,时钟用不惯Keil环境,大多数的教程都是拿keil写的,尝试将官方的uc/os iii 移植到IAR环境. 1.首先尝试从官网上下载的官方移植的代码,编译通过,但是执行会报堆栈溢 ...

  7. uC/OS-II 函数之任务相关函数

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 对于有热心的小伙伴在微博上私信我,说我的uC/OS-II 一些函数简介篇幅有些过于长应该分开介绍.应小伙伴的要求,特此将文章分开进行讲解.上文主要 ...

  8. 关于uC/OS的简单学习(转)

    1.微内核 与Linux的首要区别是,它是一个微内核,内核所实现的功能非常简单,主要包括: 一些通用函数,如TaskCreate(),OSMutexPend(),OSQPost()等. 中断处理函数, ...

  9. uC/OS-III 时钟节拍,时间管理,时间片调度

    uC/OS-III 时钟节拍,时间管理,时间片调度   时钟节拍 时钟节拍可谓是 uC/OS 操作系统的心脏,它若不跳动,整个系统都将会瘫痪. 时钟节拍就是操作系统的时基,操作系统要实现时间上的管理, ...

随机推荐

  1. 小程序动态生成二维码,生成image图片

    前端: <image src="{{img_usrl}}" style="width:100%;height:104px;" bindlongtap=&q ...

  2. svn配置多仓库与权限控制

    telnet: connect to address 47.106.115.228: Connection refused svn执行上下文错误由于目标计算机积极拒绝无法连接 标签: svn 2017 ...

  3. ubuntu18 tensorflow cpu fast_rcnn

    (flappbird) luo@luo-All-Series:~/MyFile/TensorflowProject/tf-faster-rcnn/lib$ makepython setup.py bu ...

  4. Docker CE部署

    一.概述 Docker 在1.13版本之后,从2017年的3月1日开始,版本命名规则变为如下: 项目 说明 版本格式 YY.MM Stable 每个季度发行 Edge版本 每个月发行 同时Docker ...

  5. VMware Workstation 虚拟机暂停后无法启动 出现Exception 0xc0000006 (disk error while paging) has occurred.错误

    虚拟机暂停了,突然停电,再开机后无法启动暂停的虚拟机,出现下面的错误 VMware Workstation unrecoverable error: (vmx)Exception 0xc0000006 ...

  6. 关于sudo apt-get update错误 http://archive.canonical.com natty InRelease

    sudo apt-get update 错误 http://archive.canonical.com natty InRelease 错误 http://mirror.rootguide.org n ...

  7. sublime 3插件安装记录

    安装sublime 3的package control管理器: 从菜单 View - Show Console 或者 ctrl + ~ 快捷键,调出 console.将以下 Python 代码粘贴进去 ...

  8. YII2 选择布局

    方案1:控制器内成员变量 public $layout = false; //不使用布局 public $layout = "main"; //设置使用的布局文件 方案2:控制器成 ...

  9. Linux查询系统信息命令

    Linux查看系统信息是比较基础的知识,所以这个应该都需要掌握,命令和解释如下: #uname -a           查看操作系统.内核.CPU信息 #head -n 1 /etc/issue   ...

  10. Selenium2+python自动化之数据驱动(ddt)

    环境准备 1.安装ddt模块,打开cmd输入pip install ddt在线安装 数据驱动原理 1.测试数据为多个字典的list类型 2.测试类前加修饰@ddt.ddt 3.case前加修饰@ddt ...