Wednesday 7 December 2016

STATE MACHINE



 STATE MACHINE IN EMBEDDED SOFTWARE

"In this blog we will discuss about STATE MACHINE architecture of Embedded Software Programming. This blog covers what is State Machine Architecture and STATE machine Implementation using Switch with a small multi state program"


What is STATE MACHINE?

STATE MACHINE also named as FINITE STATE MACHINE. In embedded system programming we divide the whole system functionality with in small functions and give name STATE to these Function. SO a STATE is Function or condition that give a prescribed INPUT to OUTPUT relation or input for next stage. So a STATE Machine architecture is algorithm divided in small functions named as STATE.
So STATE machine is a combination of small states, definition of state transition from one state to another state. Transition will occur from one state to another state in correspondence to input to state and next state will function of current state and output of current state.
State machines are of two types, one is Mealy Machine and another is Moore Machine.

Mealy Machine

In mealy machine architecture out-put depend on the present state and present input. Mealy architecture has less no of stages and reacts to input faster.

Moore Machine

In Moore architecture out- put of state depends only on present state. So there will be large no of states.

State Machine Example

Figure show the state diagram of a system consist 4 main states and 2 sub states:



This system consist four main states State1, State2, State3, State4 and two sub state of State1 i.e. Substate11 and Substate12. After startup State1 is the first state.
In state1 It will check NEXT_SUB_STATE, if it is Substate11 or Substate12 then ot will go to SubState11 or Substate12. It will check NEXT_STATE, if it is State3 then it will goes to State3 else it will goes to State2.
In State2 it will check if NEXT_STATE is State4 it will goes to State4 else it will goes to State3.
In State3 there is direct transition from State3 to State4 and from State4 to State1.

Implementation of State Machine

In this example I have implemented the state machine using nested switch architecture.
First we declare the STATE and SUB_STATE of enumeration type.  In SUB_STATE enum I have declared first state as NO_SUB_STATE, I this state no sub state will be executed.

typedef enum {STATE1,STATE2,STATE3,STATE4} MAIN_STATE;
typedef enum {N0_SUB_STATE,SUB_STATE1, SUB_STATE2} SUB_STATE;

MAIN_STATE CURRENT_STATE = STATE1;//set default main state to STATE1
SUB_STATE CURRENT_SUB_STATE=N0_SUB_STATE; //set default sub state to                                           //N0_SUB_STATE
MAIN_STATE NEXT_STATE;   // NEXT_STATE value depends on function //calculation                                                     

State machines functions Declaration

/***Function declaration********/
void STATE1_FUNC(void);
void STATE2_FUNC(void);
void STATE3_FUNC(void);
void STATE4_FUNC(void);
void SUB_STATE11_FUNC(void);
void SUB_STATE12_FUNC(void);

Here I have taken the example with all function with void input and output. This structure can be modified to handle function that have input passing values and return values.

Functions Definitions

/***STATE1_FUNC DEFINITION*********/
void STATE1_FUNC(void)
{
      //Function Code
     if (CURRENT_SUB_STATE!=N0_SUB_STATE)
           {
                 switch(CURRENT_SUB_STATE)
                {
                      case SUB_STATE1:SUB_STATE11_FUNC();
                             break
                      case   SUB_STATE1: SUB_STATE12_FUNC();
                             break;

                }
           }

     else if (NEXT_STATE==STATE3)
            CURRENT_STATE = STATE2;
        else
                CURRENT_STATE = STATE2;
}

/***STATE2_FUNC DEFINITION*********/
void STATE2_FUNC(void)
{
        //Function Code
    if (NEXT_STATE==STATE4)
        CURRENT_STATE=STATE4;
    else
         CURRENT_STATE=STATE3;
}

/***STATE3_FUNC DEFINITION*********/
void STATE3_FUNC(void)
{
        //Function Code
    CURRENT_STATE=STATE4;
}

/***STATE4_FUNC DEFINITION*********/
void STATE4_FUNC(void)
{
        //Function Code
      CURRENT_STATE=STATE1;
}

/***STATE11_FUNC DEFINITION*********/

void SUB_STATE11_FUNC(void)
{
    //Function Code
    CURRENT_STATE=STATE2;
}
/***STATE12_FUNC DEFINITION*********/
void SUB_STATE12_FUNC(void)
{
     //Function Code
        CURRENT_STATE=STATE1;
}

Main Routine

Main routine has been implemented with Simple Switch Statements of CURRENT_STATE. 

       switch(CURRENT_STATE)
        {
            case STATE1:
                STATE1_FUNC();
                break;
               
            case STATE2:
                STATE2_FUNC();
                break;
                       
            case STATE3:
                STATE3_FUNC();
                break;
                       
            case STATE4: 
                STATE4_FUNC();
                break;
        }

Similarly we can implement state machine architecture with large no of Main States and sub states. This will give accessibility of maintaining code easily and adding new state machines.
In next blog we will try to understand state machine implementation using function pointer. 

No comments:

Post a Comment