μC/OS-II---计时器管理2(os_tmr.c)

目录

    • 获取计时器的名字
    • 获取计时器到期前剩余的时间
    • 查看计时器的状态

在这里插入图片描述

  • 计时器是倒计时器,当计数器达到零时执行某个动作。用户通过回调函数提供这个动作。回调函数是用户声明的函数,在计时器到期时被调用。在回调函数中绝对不能进行阻塞调用(例如调用OSTimeDly()、OSTimeDlyHMSM()…或任何导致计时器任务阻塞或被删除的操作),这一点非常重要。

获取计时器的名字

#if OS_TMR_EN > 0u && OS_TMR_CFG_NAME_EN > 0u
INT8U  OSTmrNameGet (OS_TMR   *ptmr,
										 INT8U   **pdest,
										 INT8U    *perr)
{
	INT8U  len;
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return (0u);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (pdest == (INT8U **)0)
	{
		*perr = OS_ERR_TMR_INVALID_DEST;
		return (0u);
	}
	
	if (ptmr == (OS_TMR *)0)
	{
		*perr = OS_ERR_TMR_INVALID;
		return (0u);
	}
	
#endif
	
	if (ptmr->OSTmrType != OS_TMR_TYPE)                /* Validate timer structure                                    */
	{
		*perr = OS_ERR_TMR_INVALID_TYPE;
		return (0u);
	}
	
	if (OSIntNesting > 0u)                             /* See if trying to call from an ISR                           */
	{
		*perr = OS_ERR_NAME_GET_ISR;
		return (0u);
	}
	
	OSSchedLock();
	
	switch (ptmr->OSTmrState)
	{
		case OS_TMR_STATE_RUNNING:
		case OS_TMR_STATE_STOPPED:
		case OS_TMR_STATE_COMPLETED:
			*pdest = ptmr->OSTmrName;
			len    = OS_StrLen (*pdest);
			OSSchedUnlock();
			*perr = OS_ERR_NONE;
			return (len);
			
		case OS_TMR_STATE_UNUSED:                      /* Timer is not allocated                                      */
			OSSchedUnlock();
			*perr = OS_ERR_TMR_INACTIVE;
			return (0u);
			
		default:
			OSSchedUnlock();
			*perr = OS_ERR_TMR_INVALID_STATE;
			return (0u);
	}
}
#endif

获取计时器到期前剩余的时间

#if OS_TMR_EN > 0u
INT32U  OSTmrRemainGet (OS_TMR  *ptmr,
												INT8U   *perr)
{
	INT32U  remain;
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return (0u);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (ptmr == (OS_TMR *)0)
	{
		*perr = OS_ERR_TMR_INVALID;
		return (0u);
	}
	
#endif
	
	if (ptmr->OSTmrType != OS_TMR_TYPE)                /* Validate timer structure                                    */
	{
		*perr = OS_ERR_TMR_INVALID_TYPE;
		return (0u);
	}
	
	if (OSIntNesting > 0u)                             /* See if trying to call from an ISR                           */
	{
		*perr = OS_ERR_TMR_ISR;
		return (0u);
	}
	
	OSSchedLock();
	
	switch (ptmr->OSTmrState)
	{
		case OS_TMR_STATE_RUNNING:
			remain = ptmr->OSTmrMatch - OSTmrTime;    /* Determine how much time is left to timeout                  */
			OSSchedUnlock();
			*perr  = OS_ERR_NONE;
			return (remain);
			
		case OS_TMR_STATE_STOPPED:                     /* It's assumed that the timer has not started yet             */
			switch (ptmr->OSTmrOpt)
			{
				case OS_TMR_OPT_PERIODIC:
					if (ptmr->OSTmrDly == 0u)
					{
						remain = ptmr->OSTmrPeriod;
					}
					
					else
					{
						remain = ptmr->OSTmrDly;
					}
					
					OSSchedUnlock();
					*perr  = OS_ERR_NONE;
					break;
					
				case OS_TMR_OPT_ONE_SHOT:
				default:
					remain = ptmr->OSTmrDly;
					OSSchedUnlock();
					*perr  = OS_ERR_NONE;
					break;
			}
			
			return (remain);
			
		case OS_TMR_STATE_COMPLETED:                   /* Only ONE-SHOT that timed out can be in this state           */
			OSSchedUnlock();
			*perr = OS_ERR_NONE;
			return (0u);
			
		case OS_TMR_STATE_UNUSED:
			OSSchedUnlock();
			*perr = OS_ERR_TMR_INACTIVE;
			return (0u);
			
		default:
			OSSchedUnlock();
			*perr = OS_ERR_TMR_INVALID_STATE;
			return (0u);
	}
}
#endif

查看计时器的状态

#if OS_TMR_EN > 0u
INT8U  OSTmrStateGet (OS_TMR  *ptmr,
											INT8U   *perr)
{
	INT8U  state;
#ifdef OS_SAFETY_CRITICAL
	
	if (perr == (INT8U *)0)
	{
		OS_SAFETY_CRITICAL_EXCEPTION();
		return (0u);
	}
	
#endif
#if OS_ARG_CHK_EN > 0u
	
	if (ptmr == (OS_TMR *)0)
	{
		*perr = OS_ERR_TMR_INVALID;
		return (0u);
	}
	
#endif
	
	if (ptmr->OSTmrType != OS_TMR_TYPE)                /* Validate timer structure                                    */
	{
		*perr = OS_ERR_TMR_INVALID_TYPE;
		return (0u);
	}
	
	if (OSIntNesting > 0u)                             /* See if trying to call from an ISR                           */
	{
		*perr = OS_ERR_TMR_ISR;
		return (0u);
	}
	
	OSSchedLock();
	state = ptmr->OSTmrState;
	
	switch (state)
	{
		case OS_TMR_STATE_UNUSED:
		case OS_TMR_STATE_STOPPED:
		case OS_TMR_STATE_COMPLETED:
		case OS_TMR_STATE_RUNNING:
			*perr = OS_ERR_NONE;
			break;
			
		default:
			*perr = OS_ERR_TMR_INVALID_STATE;
			break;
	}
	
	OSSchedUnlock();
	return (state);
}
#endif

你可能感兴趣的:(μC/OS-II学习,c语言)