You are on page 1of 14

Embedded systems programming

Page 1
25.1.2016 KRL

Reactive systems
• Embedded systems are often reactive systems by nature
• System responds to events and actions are performed as a response to

If the number of events is large and the system is very complex a
traditional flow chart modeling of a system will not work
• Flow chart modeling typically implies polling
• Must remember to poll for right events and make sure that they are
polled frequently enough
• Communication protocols are very difficult to model with a flow chart
since they tend to be very complex
Many systems can be modeled with a state machine
• System has number of states and is always in one of them
• System switches between states based on received events
• Actions determined either by current state (Moore machine) or current
state and the event (Mealy machine)

If time tick interval is 100 ms then a 5 second wait would require 50 waiting states • Usually we use extended state machines where state is split into several state variables and some of the variables are dedicated to for example time keeping . The number fo states would grow very large if timeouts were implemented with a single state variable • For example TCP retransmission timeout would require one state per time tick to wait.Embedded systems programming Page 2 25.2016 KRL State machine • In a classic state machine the state can be stored in a single variable • Simple to implement • Many communication protocols require different timeouts.1.

Guard conditions are associated with the timers to determine when a state transition is triggered • Selecting-tilassa toteutuskohtaisesti voi päättää kuinka kauan vastauksia odotetaan (tähän tarvitaan tilamuuttuja) Page 3 25.Embedded systems programming Extended state machine • For example DHCP protocol is described in RFC and a state machine with timers T1 and T2. These timers would be extended state variables.2016 KRL .1.

1.)/ Send DHCPDECLINE DHCPNAK / Discard offer ./ Send DHCPDISCOVER REBOOTING SELECTING REQUESTING DHCPOFFER / Discard DHCPACK / Record lease. T2 REBINDING BOUND DHCPOFFER. set timers T1. set timers T1. T2 DHCPACK / Record lease. DHCPACK.Embedded systems programming Page 4 25.2016 KRL DHCP state machine (rfc2131) DHCPNAK / Restart INITREBOOT DHCPACK (not accept. T2 T1 expires / Send DHCPREQUEST to leasing server RENEWING T2 expires / Broadcast DHCPREQUEST . set timers T1. set timers T1. T2 DHCPOFFER / Collect replies DHCPNAK. DHCPNAK / Discard DHCPNAK / Halt network DHCPACK / Record lease. Lease expired / Halt network DHCPACK / Record lease./ Send DHCPREQUEST INIT Select offer / Send DHCPREQUEST .

When event generation and state machine are separated then it is easier to test the state machine and it is easier to port the state machine from one system into another • Keep pending events in a queue Nested if/switch statements • Outer level selects the state and inner level reacts to the events • Easy to implement with small number of states.2016 KRL How to implement a state machine • Implement as a function (C) or an object (C++) • The event to handle is passed as parameter to function or method call • Events are typically of special event type for example structure (or • • object) • Event generation should be outside the state machine.Embedded systems programming Page 5 25. With a complex state machine use state pattern State pattern • State machine states are objects • Changing state changes the object that handles the events • Each state is implemented as a separate class .1.

enum state { Green.Embedded systems programming Page 6 25. } else if(currentState == RedYellow) { currentState = Green. private: state currentState. Red.1. } else if(currentState == Red) { currentState = RedYellow. Yellow. }. } Traffic::~Traffic() { } void Traffic::HandleState() { /* changing state depends on the current state */ if(currentState == Green) { currentState = Yellow.h" Traffic::Traffic() { currentState = Green. RedYellow }. virtual ~Traffic(). } else if(currentState == Yellow) { currentState = Red. void HandleState().2016 KRL Example #ifndef TRAFFIC_H_ #define TRAFFIC_H_ class Traffic { public: Traffic(). } // We can add more states here } . #endif /* TRAFFIC_H_ */ #include "Traffic.

Embedded systems programming Page 7 25.2016 KRL Event handler • A practical approach to event handling is to put events into a queue.1. Event dispatcher then reads the queue and processes them in order • Queuing preserves event order even if the processing would be delayed .

1.Embedded systems programming Page 8 25.2016 KRL Active objects • Each state machine is an object that has it’s own event queue • Events are produced outside the state machines and put into • appropriate queues State machine does not know where the events come from so we can test the state machine in a test bench where we compare actual responses with expected responses .

2016 KRL Rules for implementation • States should be independent • Actions when the state is entered must not depend on the state where • • • we came from • Usually this implies that an action is implemented in the wrong state (should be done in the state where we came from) Pay attention to extended state variables (timers.) and make sure that they are set/reset properly All state variables must be members of the class • The idea of classes is to abstract and encapsulate data States and their actions in the state diagram must be easily identifiable from the source code • You should be able to draw state diagram from the source code – if you can’t maintaining your source code will be difficult .Embedded systems programming Page 9 25.1. etc.

break. 0). break. 0). } } } Page 10 25.type) { case eEnter: timer = 0. } } else if(currentState == State2) { switch(e. case eKey: led_set(e. 0). led_set(4. case eExit: break. case eKey: break. if(timer >= 5) SetState(State2). break. led_set(2. case eTick: led_set(1. break.Embedded systems programming Simple UML chart and state machine (1/2) void StateMachine::HandleState(event &e) { if(currentState == State1) { switch(e.value.type) { case eEnter: timer = 0.2016 KRL . SetState(State1). case eTick: timer++.1. 0). case eExit: break. break. led_set(3. 1).

}. event enter = {eEnter.2016 KRL Simple UML chart and state machine (2/2) void StateMachine::HandleState(event &e) { if(currentState == State1) { switch(e. HandleState(enter). } } enum eventType { eEnter. eTick }. break.value.1. HandleState(exit). eKey. currentState = newState. struct event { eventType type. } .type) { case eEnter: timer = 0. 1). case eKey: led_set(e. case eTick: timer++. eExit. case eExit: break. if(timer >= 5) SetState(State2). break. int value. 0}.Embedded systems programming Page 11 25. break. 0}. void StateMachine::SetState(state newState) { event exit = {eExit.

break. case eExit: break. 0). currentState = newState.type) { case eEnter: timer = 0. } else if(currentState == State2) { HandleState2(e). HandleState(exit). case eTick: timer++. event enter = {eEnter. 1). } } void StateMachine::SetState(state newState) { event exit = {eExit. case eTick: led_set(1. } void StateMachine::HandleState2(event &e) { switch(e.1. break.type) { case eEnter: timer = 0.2016 KRL .value.Embedded systems programming Method per state implementation void StateMachine::HandleState1(event &e) { switch(e. break. 0). 0}. led_set(4. led_set(3. if(timer >= 5) SetState(State2). 0}. } } Page 12 25. HandleState(enter). 0). break. case eExit: break. SetState(State1). case eKey: led_set(e. } } void StateMachine::HandleState(event &e) { if(currentState == State1) { HandleState1(e). 0). case eKey: break. led_set(2. break.

1.Embedded systems programming Page 13 25.2016 KRL UML state machine State name Constraint Event Transition Parameter Action .

1. 2. event interval 100 ms . 4.Embedded systems programming Page 14 25. 3. key number given as parameter • Time tick event.2016 KRL Exercise • Draw UML state machine for a lock with a key pad for entering code to open the door • There are four keys numbered 1 – 4 • Code to open the door is 3. 1 • Lock has a timer that cancels code entry if no key is pressed in 5 seconds • When a correct code is entered the lock opens for 5 seconds • Assume that you have two types of external events • Key event.