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循环中写下如下代码:
- //--------------------------------- FRAMEWORK -------------------------------------
- for(;;) //infinite loop
- {
- // State machine entry & exit point
- //===========================================================
- (*Alpha_State_Ptr)(); // jump to an Alpha state (A0,B0,...)
- //===========================================================
- }
在main函数之外有:
- //--------------------------------- FRAMEWORK -------------------------------------
- void A0(void)
- {
- // loop rate synchronizer for A-tasks
- if(CpuTimer0Regs.TCR.bit.TIF == 1)
- {
- CpuTimer0Regs.TCR.bit.TIF = 1; // clear flag
-
- //-----------------------------------------------------------
- (*A_Task_Ptr)(); // jump to an A Task (A1,A2,A3,...)
- //-----------------------------------------------------------
- }
-
- Alpha_State_Ptr = &B0; // Comment out to allow only A tasks
- }
-
- void B0(void)
- {
- // loop rate synchronizer for B-tasks
- if(CpuTimer1Regs.TCR.bit.TIF == 1)
- {
- CpuTimer1Regs.TCR.bit.TIF = 1; // clear flag
-
- //-----------------------------------------------------------
- (*B_Task_Ptr)(); // jump to a B Task (B1,B2,B3,...)
- //-----------------------------------------------------------
-
- }
-
- Alpha_State_Ptr = &C0; // Allow C state tasks
- }
-
- void C0(void)
- {
- // loop rate synchronizer for C-tasks
- if(CpuTimer2Regs.TCR.bit.TIF == 1)
- {
- CpuTimer2Regs.TCR.bit.TIF = 1; // clear flag
-
- //-----------------------------------------------------------
- (*C_Task_Ptr)(); // jump to a C Task (C1,C2,C3,...)
- //-----------------------------------------------------------
-
- }
-
- Alpha_State_Ptr = &A0; // Back to State A0
- }
任务A:
- void A1(void)
- {
- do something
- //the next time CpuTimer0 'counter' reaches Period value go to A2
- A_Task_Ptr = &A2;
- }
-
- void A2(void)
- {
- do something
- //the next time CpuTimer0 'counter' reaches Period value go to A3
- A_Task_Ptr = &A3;
- }
-
- void A3(void)
- {
- do something
- //the next time CpuTimer0 'counter' reaches Period value go to A4
- A_Task_Ptr = &A4;
- }
-
- void A4(void)
- {
- do nothing
- //the next time CpuTimer0 'counter' reaches Period value go to A1
- A_Task_Ptr = &A1;
- }
任务B和C的细分也类似于A,同上
|