uC/OS-II时间管理相对比较简单,大体包含定时器创建、启动、删除,systick中断isr,timer线程处理,可能会涉及到任务调度,优先级反转,线程同步相关知识,本部分内容重点讲解定时器机制,其他内容,可在本人博客中的其他文章查询,欢迎提出宝贵意见,共同学习uC/OS-II,本书遵循先总后分的一贯做法,希望大家提出宝贵建议和意见。
一.废话不多讲,直接上图
上图基本涵盖了定时器的全部内容,首先大概解释下上图的内容,uC/OS中的定时器机制,是靠芯片内部的systick驱动的,当然也可以用其他的timer(已超出本文范围),uC/OS内核的定时机制是一种软定时机制,这样就具有很好的跨平台性,系统在每个systick中断到来时,在中断处理函数中释放定时器线程信号量,同时将各线程的tcb结构体的OSTCBDly成员减1,当减为0时,将对应的线程tcb移动到就绪队列表中(关于任务调度内容会在后续的博客中持续更新),在退出中断处理函数时,触发一次pendsv异常,进行任务上下文切换,此时定时器处理线程从阻塞状态恢复到运行状态(当有更高优先级的任务时例外),开始递减每个定时器的计数值,当发现超时时,执行对应的处理函数。
下面逐条逐个函数进行分析
二. 创建定时器
/*
*********************************************************************************************************
* CREATE A TIMER
*
* Description: This function is called by your application code to create a timer.
*
* Arguments : dly Initial delay.
* If the timer is configured for ONE-SHOT mode, this is the timeout used.
* If the timer is configured for PERIODIC mode, this is the first timeout to
* wait for before the timer starts entering periodic mode.
*
* period The 'period' being repeated for the timer.
* If you specified 'OS_TMR_OPT_PERIODIC' as an option, when the timer
* expires, it will automatically restart with the same period.
*
* opt Specifies either:
* OS_TMR_OPT_ONE_SHOT The timer counts down only once
* OS_TMR_OPT_PERIODIC The timer counts down and then reloads itself
*
* callback Is a pointer to a callback function that will be called when the timer expires.
* The callback function must be declared as follows:
*
* void MyCallback (OS_TMR *ptmr, void *p_arg);
*
* callback_arg Is an argument (a pointer) that is passed to the callback function when it is called.
*
* pname Is a pointer to an ASCII string that is used to name the timer. Names are
* useful for debugging.
*
* perr Is a pointer to an error code. '*perr' will contain one of the following:
* OS_ERR_NONE
* OS_ERR_TMR_INVALID_DLY you specified an invalid delay
* OS_ERR_TMR_INVALID_PERIOD you specified an invalid period
* OS_ERR_TMR_INVALID_OPT you specified an invalid option
* OS_ERR_TMR_ISR if the call was made from an ISR
* OS_ERR_TMR_NON_AVAIL if there are no free timers from the timer pool
*
* Returns : A pointer to an OS_TMR data structure.
* This is the 'handle' that your application will use to reference the timer created.
*********************************************************************************************************
*/
OS_TMR *OSTmrCreate (INT32U dly,
INT32U period,
INT8U opt,
OS_TMR_CALLBACK callback,