广东湘恒智能科技有限公司
主营产品: 西门子PLC代理商,plc变频器,伺服电机,人机界面,触摸屏,线缆,DP接头
SIEMENS山东省烟台市 西门子代理商——西门子华北一级总代理

2.4 软件定时器API函数实际原理

软件定时器的多种API函数,如启动、停止、删除、复位、改变周期等,实际是通过宏定义的方式提供:



























/*启动定时器*/#define xTimerStart(xTimer, xTicksToWait) xTimerGenericCommand((xTimer), tmrCOMMAND_START, (xTaskGetTickCount()), NULL, (xTicksToWait))
/*停止定时器*/#define xTimerStop(xTimer, xTicksToWait) xTimerGenericCommand((xTimer), tmrCOMMAND_STOP, 0U, NULL, (xTicksToWait))
/*改变定时器周期*/#define xTimerChangePeriod(xTimer, xNewPeriod, xTicksToWait) xTimerGenericCommand((xTimer), tmrCOMMAND_CHANGE_PERIOD, (xNewPeriod), NULL, (xTicksToWait))
/*删除定时器*/#define xTimerDelete(xTimer, xTicksToWait) xTimerGenericCommand((xTimer), tmrCOMMAND_DELETE, 0U, NULL, (xTicksToWait))
/*复位定时器*/#define xTimerReset(xTimer, xTicksToWait) xTimerGenericCommand((xTimer), tmrCOMMAND_RESET, (xTaskGetTickCount()), NULL, (xTicksToWait))
/*从中断中启动定时器*/#define xTimerStartFromISR(xTimer, pxHigherPriorityTaskWoken) xTimerGenericCommand((xTimer), tmrCOMMAND_START_FROM_ISR, (xTaskGetTickCountFromISR()), (pxHigherPriorityTaskWoken), 0U)
/*从中断中停止定时器*/#define xTimerStopFromISR(xTimer, pxHigherPriorityTaskWoken) xTimerGenericCommand((xTimer), tmrCOMMAND_STOP_FROM_ISR, 0, (pxHigherPriorityTaskWoken), 0U)
/*从中断中改变定时器周期*/#define xTimerChangePeriodFromISR(xTimer, xNewPeriod, pxHigherPriorityTaskWoken) xTimerGenericCommand((xTimer), tmrCOMMAND_CHANGE_PERIOD_FROM_ISR, (xNewPeriod), (pxHigherPriorityTaskWoken), 0U)
/*从中断中复位定时器*/#define xTimerResetFromISR(xTimer, pxHigherPriorityTaskWoken) xTimerGenericCommand((xTimer), tmrCOMMAND_RESET_FROM_ISR, (xTaskGetTickCountFromISR()), (pxHigherPriorityTaskWoken), 0U)

这些API函数对应的宏定义,本质上又都是调用了xTimerGenericCommand函数来实现对消息的打包和发送。

2.5 软件定时器打包命令与发送

该函数将命令打包成队列项发送给xTimerQueue消息队列,由软件定时器任务(守护任务来)接收并进行处理。


















































/* 软件定时器打包命令与发送 */BaseType_t xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait ){    BaseType_t xReturn = pdFAIL;    DaemonTaskMessage_t xMessage;
   configASSERT( xTimer );
   if( xTimerQueue != NULL ){        /* 命令码 */        xMessage.xMessageID = xCommandID;        /* 命令有效值 */        xMessage.u.xTimerParameters.xMessageValue = xOptionalValue;        /* 定时器句柄 */        xMessage.u.xTimerParameters.pxTimer = xTimer;        /* 不带中断命令 */        if(xCommandID < tmrFIRST_FROM_ISR_COMMAND)        {            /* 调度器正在运行 */            if(xTaskGetSchedulerState() == taskSCHEDULER_RUNNING)            {                /* 将命令消息发送到队列,可以阻塞一定时间 */                xReturn = x(xTimerQueue, &xMessage, xTicksToWait);            }            /* 调度器不在运行 */            else            {                /* 将命令消息发送到队列 ,不带阻塞时间*/                xReturn = x(xTimerQueue, &xMessage, tmrNO_DELAY);            }        }        /* 带中断命令 */        else        {            /* 将命令消息发送到队列 */            xReturn = xFromISR(xTimerQueue, &xMessage, pxHigherPriorityTaskWoken);        }
       traceTIMER_COMMAND_SEND( xTimer, xCommandID, xOptionalValue, xReturn );}    else{        mtCOVERAGE_TEST_MARKER();}
   return xReturn;}

上面分析的差不多了,现在回到重点,回顾2.2的xTimerCreateTimerTask()函数,在创建列表与消息队列后,会使用xTaskCreate方法来创建软件定时器任务prvTimerTask(),该任务实体的具体内容如下:


展开全文
相关产品
商铺首页 拨打电话 QQ联系 发送询价