Hello Experts,
I came up with this state machine for one of my embedded projects, looking at a few reference programs online. And was wondering if someone could verify it for me, I am not able to simulated the timers are parallel operations on windows.
Basically I have 3 cpu timers which overflow at 50,100 and 150 microseconds respectively. And each timer is associated with a 'branch' and each branch is further has 3 states. And these states should run at regular intervals.
Here is the code:
Code:
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
// USER Variables
unsigned int taskA1_counter = 0;
unsigned int taskA2_counter = 0;
unsigned int taskA3_counter = 0;
unsigned int taskB1_counter = 0;
unsigned int taskB2_counter = 0;
unsigned int taskB3_counter = 0;
unsigned int taskC1_counter = 0;
unsigned int taskC2_counter = 0;
unsigned int taskC3_counter = 0;
// Alpha states
void A0(void); //state A0
void B0(void); //state B0
void C0(void); //state C0
// A branch states
void A1(void); //state A1
void A2(void); //state A2
void A3(void); //state A3
// B branch states
void B1(void); //state B1
void B2(void); //state B2
void B3(void); //state B3
// C branch states
void C1(void); //state C1
void C2(void); //state C2
void C3(void); //state C3
// Variable declarations
void (*Alpha_State_Ptr)(void); // Base States pointer
void (*A_Task_Ptr)(void); // State pointer A branch
void (*B_Task_Ptr)(void); // State pointer B branch
void (*C_Task_Ptr)(void); // State pointer C branch
int main()
{
// Tasks State-machine init
Alpha_State_Ptr = &A0;
A_Task_Ptr = &A1;
B_Task_Ptr = &B1;
C_Task_Ptr = &C1;
for (;;)
{
// State machine entry & exit point
(*Alpha_State_Ptr)(); // jump to an Alpha state (A0,B0,...)
}
return 0;
}
void A0(void)
{
// loop rate synchronizer for A-tasks
if (CPUTimer_getTimerOverflowStatus(CPUTIMER0_BASE)) //overflowes every 50 usec
{
TestA = 0;
CPUTimer_clearOverflowFlag(CPUTIMER0_BASE); // 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 (CPUTimer_getTimerOverflowStatus(CPUTIMER1_BASE)) //overflowes every 100 usec
{
CPUTimer_clearOverflowFlag(CPUTIMER1_BASE); // clear flag
TestB = 0;
(*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 (CPUTimer_getTimerOverflowStatus(CPUTIMER2_BASE)) //overflowes every 150 usec
{
TestC = 0;
CPUTimer_clearOverflowFlag(CPUTIMER2_BASE); // clear flag
(*C_Task_Ptr)(); // jump to a C Task (C1,C2,C3,...)
}
Alpha_State_Ptr = &A0; // Back to State A0
}
void A1(void)
//--------------------------------------------------------
{
//A1 task
taskA1_counter++;
printf("A1 \n");
//-------------------
//the next time CpuTimer0 'counter' reaches Period value go to A2
A_Task_Ptr = &A2;
//-------------------
}
void A2(void)
//--------------------------------------------------------
{
//A2 task
taskA2_counter++;
printf("A2 \n");
//-------------------
//the next time CpuTimer0 'counter' reaches Period value go to A3
A_Task_Ptr = &A3;
//-------------------
}
void A3(void)
//--------------------------------------------------------
{
//A3 task
taskA3_counter++;
printf("A3 \n");
//-------------------
//the next time CpuTimer0 'counter' reaches Period value go to A1
A_Task_Ptr = &A1;
//-------------------
}
void B1(void)
//--------------------------------------------------------
{
//B1 task
taskB1_counter++;
printf("B1 \n");
//-------------------
//the next time CpuTimer1 'counter' reaches Period value go to B2
B_Task_Ptr = &B2;
//-------------------
}
void B2(void)
//--------------------------------------------------------
{
//A2 task
taskB2_counter++;
printf("B2 \n");
//-------------------
//the next time CpuTimer1 'counter' reaches Period value go to B3
B_Task_Ptr = &B3;
//-------------------
}
void B3(void)
//--------------------------------------------------------
{
//B3 task
taskB3_counter++;
printf("B3 \n");
//-------------------
//the next time CpuTimer1 'counter' reaches Period value go to B1
B_Task_Ptr = &B1;
//-------------------
}
void C1(void)
//----------------------------------------
{
//C1 task
taskC1_counter++;
printf("C1 \n");
//-----------------
//the next time CpuTimer2 'counter' reaches Period value go to C2
C_Task_Ptr = &C2;
//-----------------
}
void C2(void)
//----------------------------------------
{
//C2 task
taskC2_counter++;
printf("C2 \n");
//-----------------
//the next time CpuTimer2 'counter' reaches Period value go to C3
C_Task_Ptr = &C3;
//-----------------
}
void C3(void)
//----------------------------------------
{
//C3 task
taskC3_counter++;
printf("C3 \n");
//-----------------
//the next time CpuTimer2 'counter' reaches Period value go to C3
C_Task_Ptr = &C3;
//-----------------
}
This is the end result that I need:
https://cboard.cprogramming.com/imag...BJRU5ErkJggg==