查看: 670|回复: 0

[经验] TI官方代码中的任务状态机

[复制链接]

该用户从未签到

发表于 2020-12-7 22:29:40 | 显示全部楼层 |阅读模式
分享到:

TI官方例程代码中的任务状态机用于处理各种不同的非环路控制任务,这种状态机编程对于多任务管理比较有用





  • // Timing sync for background loops  
  • // Timer period definitions found in PeripheralHeaderIncludes.h  
  •     CpuTimer0Regs.PRD.all =  mSec1;     // A tasks,200000,1ms,CPU frequency=200MHz  
  •     CpuTimer1Regs.PRD.all =  mSec10;    // B tasks,2000000,10ms  
  •     CpuTimer2Regs.PRD.all =  mSec100;   // C tasks,20000000,100ms  
  •   
  • // Tasks State-machine init  
  •     Alpha_State_Ptr = &A0;  


然后在main函数里的while循环中写下如下代码:
  1. //--------------------------------- FRAMEWORK -------------------------------------  
  2.     for(;;)  //infinite loop  
  3.     {  
  4.         // State machine entry & exit point  
  5.         //===========================================================  
  6.         (*Alpha_State_Ptr)();   // jump to an Alpha state (A0,B0,...)  
  7.         //===========================================================  
  8.     }  
复制代码



在main函数之外有:



    1. <font microsoft="" yahei";"=""><ul type="1" class="litype_1" style="margin-left: 14px; padding-right: 20px; padding-left: 20px; list-style: none; font-style: italic;"><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">//--------------------------------- FRAMEWORK -------------------------------------  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">void A0(void)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">{  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    // loop rate synchronizer for A-tasks  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    if(CpuTimer0Regs.TCR.bit.TIF == 1)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    {  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        CpuTimer0Regs.TCR.bit.TIF = 1;  // clear flag  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        //-----------------------------------------------------------  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        (*A_Task_Ptr)();        // jump to an A Task (A1,A2,A3,...)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        //-----------------------------------------------------------  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    }  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    Alpha_State_Ptr = &B0;      // Comment out to allow only A tasks  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">}  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">void B0(void)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">{  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    // loop rate synchronizer for B-tasks  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    if(CpuTimer1Regs.TCR.bit.TIF == 1)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    {  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        CpuTimer1Regs.TCR.bit.TIF = 1;  // clear flag  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        //-----------------------------------------------------------  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        (*B_Task_Ptr)();        // jump to a B Task (B1,B2,B3,...)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        //-----------------------------------------------------------  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    }  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    Alpha_State_Ptr = &C0;      // Allow C state tasks  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">}  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">void C0(void)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">{  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    // loop rate synchronizer for C-tasks  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    if(CpuTimer2Regs.TCR.bit.TIF == 1)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    {  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        CpuTimer2Regs.TCR.bit.TIF = 1;  // clear flag  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        //-----------------------------------------------------------  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        (*C_Task_Ptr)();        // jump to a C Task (C1,C2,C3,...)  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">        //-----------------------------------------------------------  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">      </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    }  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">    Alpha_State_Ptr = &A0;      // Back to State A0  </font></li><li style="list-style-position: initial; list-style-image: initial; font-style: normal !important;"><font color="black">}  </font>
    2. </li></ul></font>
    复制代码


任务A:
  1. void A1(void)  
  2. {  
  3.         do something  
  4.     //the next time CpuTimer0 'counter' reaches Period value go to A2  
  5.     A_Task_Ptr = &A2;  
  6. }  
  7.   
  8. void A2(void)  
  9. {  
  10.         do something  
  11.     //the next time CpuTimer0 'counter' reaches Period value go to A3  
  12.     A_Task_Ptr = &A3;  
  13. }  
  14.   
  15. void A3(void)  
  16. {  
  17.         do something  
  18.     //the next time CpuTimer0 'counter' reaches Period value go to A4  
  19.     A_Task_Ptr = &A4;  
  20. }  
  21.   
  22. void A4(void)  
  23. {  
  24.         do nothing  
  25.     //the next time CpuTimer0 'counter' reaches Period value go to A1  
  26.     A_Task_Ptr = &A1;  
  27. }  
复制代码



任务B和C的细分也类似于A,同上
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /4 下一条



手机版|小黑屋|与非网

GMT+8, 2024-11-22 20:55 , Processed in 0.105139 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2024, Tencent Cloud.