uC/OS-II 内核源码分析(三)---时间管理机制详解

本文详细介绍了uC/OS-II内核的时间管理机制,包括定时器创建、启动、删除以及systick中断处理。通过systick中断,定时器线程在每个中断到来时释放信号量,任务的tcb结构体的延迟值递减,当达到0时将任务移到就绪队列。文章还探讨了定时器线程如何处理超时任务,以及如何通过回调函数执行相应操作。此外,文章还分析了OSTmrCreate和OSTmrStart等关键函数的实现细节。
摘要由CSDN通过智能技术生成


 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,

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值