0
0
Embedded Cprogramming~10 mins

Hierarchical state machine concept in Embedded C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hierarchical state machine concept
Start
Top State
Substate A
Sub-substate A1
Sub-substate A2
Substate B
Sub-substate B1
Sub-substate B2
Event Occurs
Check Transitions
Change State Hierarchically
Execute Entry/Exit Actions
Wait for Next Event
The hierarchical state machine starts at a top state, which contains nested substates. When an event occurs, transitions are checked from the current substate up through parent states, changing states and running entry/exit actions accordingly.
Execution Sample
Embedded C
typedef enum {TOP, A, A1, A2, B, B1, B2} State;
State current_state = TOP;

void event_handler(Event e) {
  switch(current_state) {
    case A1: if(e == EVT_X) current_state = B1; break;
  }
}
This code shows a simple hierarchical state machine with states and a transition from substate A1 to B1 on event EVT_X.
Execution Table
StepCurrent StateEventTransition ConditionNew StateEntry/Exit Actions
1TOPNoneNo eventTOPNone
2TOPInitEnter initial substate AAEnter A
3AInitEnter initial sub-substate A1A1Enter A1
4A1EVT_XIf EVT_X in A1, go to B1B1Exit A1, Exit A, Enter B, Enter B1
5B1NoneNo eventB1None
6B1EVT_YNo transitionB1None
7B1EVT_ZTransition to B2B2Exit B1, Enter B2
8B2NoneNo eventB2None
9B2EVT_STOPTransition to TOPTOPExit B2, Exit B, Enter TOP
10TOPNoneNo eventTOPNone
💡 Execution stops when no more events or transitions occur.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 7After Step 9Final
current_stateTOPAA1B1B2TOPTOP
Key Moments - 3 Insights
Why do we exit both A1 and A when transitioning from A1 to B1?
Because B1 is under B, a different branch from A, we must exit the current substate A1 and its parent A before entering B and B1, as shown in step 4 of the execution_table.
What happens if an event has no matching transition in the current state?
The state machine stays in the current state without changes or actions, as seen in step 6 where EVT_Y does not cause a transition from B1.
How does the machine know which substate to enter initially?
On entering a parent state, the machine enters its initial substate automatically, like entering A then A1 in steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the current_state after step 4?
AB1
BA1
CA
DTOP
💡 Hint
Check the 'New State' column at step 4 in the execution_table.
At which step does the state machine exit state B1?
AStep 6
BStep 9
CStep 7
DStep 4
💡 Hint
Look for 'Exit B1' in the Entry/Exit Actions column in the execution_table.
If the event EVT_X did not cause a transition in A1, what would happen to current_state after step 4?
AIt would change to B1 anyway
BIt would remain A1
CIt would change to TOP
DIt would change to A
💡 Hint
Refer to the key_moments about no matching transition and step 6 where no transition keeps the state unchanged.
Concept Snapshot
Hierarchical State Machine (HSM):
- States can contain nested substates.
- Events trigger transitions checked from current substate up.
- Exiting a state exits all nested substates.
- Entering a state enters its initial substate.
- Entry/Exit actions run on state changes.
Full Transcript
A hierarchical state machine organizes states in a tree structure with parent and nested substates. The machine starts at a top state and enters initial substates automatically. When an event occurs, it checks for transitions starting from the current substate up through parent states. If a transition is found, it exits the current substate and any parent states not shared with the target, then enters the target state and its initial substates, running entry and exit actions accordingly. If no transition matches, the state remains unchanged. This structure helps manage complex behaviors by grouping related states and transitions.