OS_TMR *OSTmrCreate (INT32U dly, INT32U period, INT8U opt, OS_TMR_CALLBACK callback, void *callback_arg, INT8U *pname, INT8U *perr) { OS_TMR *ptmr; #ifdef OS_SAFETY_CRITICAL if (perr == (INT8U *)0) { OS_SAFETY_CRITICAL_EXCEPTION(); } #endif #ifdef OS_SAFETY_CRITICAL_IEC61508 if (OSSafetyCriticalStartFlag == OS_TRUE) { OS_SAFETY_CRITICAL_EXCEPTION(); } #endif #if OS_ARG_CHK_EN > 0u switch (opt) { /* Validate arguments */ case OS_TMR_OPT_PERIODIC: if (period == 0u) { *perr = OS_ERR_TMR_INVALID_PERIOD; return ((OS_TMR *)0); } break; case OS_TMR_OPT_ONE_SHOT: if (dly == 0u) { *perr = OS_ERR_TMR_INVALID_DLY; return ((OS_TMR *)0); } break; default: *perr = OS_ERR_TMR_INVALID_OPT; return ((OS_TMR *)0); } #endif if (OSIntNesting > 0u) { /* See if trying to call from an ISR */ *perr = OS_ERR_TMR_ISR; return ((OS_TMR *)0); } OSSchedLock(); ptmr = OSTmr_Alloc(); /* Obtain a timer from the free pool */ if (ptmr == (OS_TMR *)0) { OSSchedUnlock(); *perr = OS_ERR_TMR_NON_AVAIL; return ((OS_TMR *)0); } ptmr->OSTmrState = OS_TMR_STATE_STOPPED; /* Indicate that timer is not running yet */ ptmr->OSTmrDly = dly; ptmr->OSTmrPeriod = period; ptmr->OSTmrOpt = opt; ptmr->OSTmrCallback = callback; ptmr->OSTmrCallbackArg = callback_arg; #if OS_TMR_CFG_NAME_EN > 0u ptmr->OSTmrName = pname; #endif OSSchedUnlock(); *perr = OS_ERR_NONE; return (ptmr); }
OS_TMR *OSTmrCreate (INT32U dly, INT32U period, INT8U opt, OS_TMR_CALLBACK callback, void *callback_arg, INT8U *pname, INT8U *perr) { OS_TMR *ptmr; #if OS_TMR_CFG_NAME_SIZE > 0 INT8U len; #endif #if OS_ARG_CHK_EN > 0 if (perr == (INT8U *)0) { /* Validate arguments */ return ((OS_TMR *)0); } switch (opt) { case OS_TMR_OPT_PERIODIC: if (period == 0) { *perr = OS_ERR_TMR_INVALID_PERIOD; return ((OS_TMR *)0); } break; case OS_TMR_OPT_ONE_SHOT: if (dly == 0) { *perr = OS_ERR_TMR_INVALID_DLY; return ((OS_TMR *)0); } break; default: *perr = OS_ERR_TMR_INVALID_OPT; return ((OS_TMR *)0); } #endif if (OSIntNesting > 0) { /* See if trying to call from an ISR */ *perr = OS_ERR_TMR_ISR; return ((OS_TMR *)0); } OSTmr_Lock(); ptmr = OSTmr_Alloc(); /* Obtain a timer from the free pool */ if (ptmr == (OS_TMR *)0) { OSTmr_Unlock(); *perr = OS_ERR_TMR_NON_AVAIL; return ((OS_TMR *)0); } ptmr->OSTmrState = OS_TMR_STATE_STOPPED; /* Indicate that timer is not running yet */ ptmr->OSTmrDly = dly; ptmr->OSTmrPeriod = period; ptmr->OSTmrOpt = opt; ptmr->OSTmrCallback = callback; ptmr->OSTmrCallbackArg = callback_arg; #if OS_TMR_CFG_NAME_SIZE > 0 if (pname !=(INT8U *)0) { len = OS_StrLen(pname); /* Copy timer name */ if (len < OS_TMR_CFG_NAME_SIZE) { (void)OS_StrCopy(ptmr->OSTmrName, pname); } else { #if OS_TMR_CFG_NAME_SIZE > 1 ptmr->OSTmrName[0] = '#'; /* Invalid size specified */ ptmr->OSTmrName[1] = OS_ASCII_NUL; #endif *perr = OS_ERR_TMR_NAME_TOO_LONG; OSTmr_Unlock(); return (ptmr); } } #endif OSTmr_Unlock(); *perr = OS_ERR_NONE; return (ptmr); }
OS_TMR *OSTmrCreate (INT32U dly, INT32U period, INT8U opt, OS_TMR_CALLBACK callback, void *callback_arg, INT8U *pname, INT8U *perr) { OS_TMR *ptmr; #ifdef OS_SAFETY_CRITICAL if (perr == (INT8U *)0) { OS_SAFETY_CRITICAL_EXCEPTION(); return ((OS_TMR *)0); } #endif #ifdef OS_SAFETY_CRITICAL_IEC61508 if (OSSafetyCriticalStartFlag == OS_TRUE) { OS_SAFETY_CRITICAL_EXCEPTION(); return ((OS_TMR *)0); } #endif #if OS_ARG_CHK_EN > 0u /* 检查参数 */ switch (opt) { /* Validate arguments */ /* 周期定时器的period不能是0 */ case OS_TMR_OPT_PERIODIC: if (period == 0u) { *perr = OS_ERR_TMR_INVALID_PERIOD; return ((OS_TMR *)0); } break; /* 单次定时器的dly不能是0 */ case OS_TMR_OPT_ONE_SHOT: if (dly == 0u) { *perr = OS_ERR_TMR_INVALID_DLY; return ((OS_TMR *)0); } break; default: *perr = OS_ERR_TMR_INVALID_OPT; return ((OS_TMR *)0); } #endif if (OSIntNesting > 0u) { /* See if trying to call from an ISR */ *perr = OS_ERR_TMR_ISR; return ((OS_TMR *)0); } /* 关调度 */ OSSchedLock(); /* 从空闲定时器控制块链表中获得一个空闲定时器控制块 */ ptmr = OSTmr_Alloc(); /* Obtain a timer from the free pool */ /* 如果没有空闲定时器控制块,则返回错误信息 */ if (ptmr == (OS_TMR *)0) { OSSchedUnlock(); *perr = OS_ERR_TMR_NON_AVAIL; return ((OS_TMR *)0); } /* 设置获取到的定时器控制块 */ /* 创建定时器后,定时器是停止状态 */ ptmr->OSTmrState = OS_TMR_STATE_STOPPED; /* Indicate that timer is not running yet */ /* 设置第一次延时值 */ ptmr->OSTmrDly = dly; /* 设置周期值 */ ptmr->OSTmrPeriod = period; /* 设置定时器类型,单次或者周期 */ ptmr->OSTmrOpt = opt; /* 设置定时器回调函数 */ ptmr->OSTmrCallback = callback; /* 设置定时器回调函数的参数 */ ptmr->OSTmrCallbackArg = callback_arg; #if OS_TMR_CFG_NAME_EN > 0u if (pname == (INT8U *)0) { /* Is 'pname' a NULL pointer? */ ptmr->OSTmrName = (INT8U *)(void *)"?"; } else { ptmr->OSTmrName = pname; } #endif /* 允许调度 */ OSSchedUnlock(); *perr = OS_ERR_NONE; return (ptmr); }