0% found this document useful (0 votes)
35 views21 pages

Lecture 7 - State Machines

This document provides an introduction to embedded systems and state machines. It discusses representing systems using state machine models, including finite state machines (FSM) and finite state machines with datapath (FSMD). It describes modeling an elevator control system as an FSM and defines the formal components of an FSM, including states, inputs, outputs, transition functions, and an initial state. It also discusses implementing FSMs using a language subset approach in sequential programming languages like C.

Uploaded by

teddy tigabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views21 pages

Lecture 7 - State Machines

This document provides an introduction to embedded systems and state machines. It discusses representing systems using state machine models, including finite state machines (FSM) and finite state machines with datapath (FSMD). It describes modeling an elevator control system as an FSM and defines the formal components of an FSM, including states, inputs, outputs, transition functions, and an initial state. It also discusses implementing FSMs using a language subset approach in sequential programming languages like C.

Uploaded by

teddy tigabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Embedded Systems

State Machines
Getachew Teshome (Room 120-B)
Addis Ababa University, AAIT
School of Electrical and Computer Engineering
System Representation
• State Machine Model
– FSM/FSMD, Program-State Machine (PSM) Model
• Concurrent Process Model
– Communication,Synchronization,Implementation
• Dataflow Model
• Real-Time Systems
Elevator Control
System interface
Partial English description up
Unit
Control down
open
“Move the elevator either up or down to floor
reach the requested floor. Once at the req

requested floor, open the door for at least Request


Resolver
10 seconds, and keep it open until the b1 buttons
inside
b2 elevator
requested floor changes. Ensure the door ... bN

is never open while moving. Don’t change up1 up/down


up2
directions unless there are no higher dn2
buttons
on each
up3
requests when moving up or no lower dn3
floor

requests when moving down…” ...


dnN
Sequential program model

Inputs: int floor; bit b1..bN; up1..upN-1; dn2..dnN;


Outputs: bit up, down, open;
Global variables: int req;

void UnitControl() void RequestResolver()


{ {
up = down = 0; open = 1; while (1)
while (1) { ...
while (req == floor); req = ...
open = 0; ...
if (req > floor) { up = 1;} }
else {down = 1;}
while (req != floor); void main()
up = down = 0; {
open = 1; Call concurrently:
delay(10); UnitControl() and
} RequestResolver()
} }
Finite-state machine (FSM)
model
• Sequential program is not Handy to capture a bit awkward
Consider an FSM model:
– Possible states
• E.g., Idle, GoingUp, GoingDn, DoorOpen
– Possible transitions from one state to another based on input
• E.g., req > floor
– Actions that occur in each state
• E.g., In the GoingUp state, u,d,o,t = 1,0,0,0 (up = 1, down, open, and
timer_start = 0)

5
Finite-state machine (FSM) model
req > floor

u,d,o, t = 1,0,0,0 GoingUp !(req > floor)

req > floor timer < 10


u,d,o,t = 0,0,1,0 !(timer < 10)
Idle DoorOpen
req == floor u,d,o,t = 0,0,1,1
req < floor

!(req<floor)
u,d,o,t = 0,1,0,0
GoingDn

u is up, d is down, o is open


req < floor
t is timer_start

6
Formal definition
• An FSM is a 6-tuple F<S, I, O, F, H, s0>
– S is a set of all states {s0, s1, …, sl}
– I is a set of inputs {i0, i1, …, im}
– O is a set of outputs {o0, o1, …, on}
– F is a next-state function (S x I → S)
– H is an output function (S → O)
– s0 is an initial state
• Moore-type
– Associates outputs with states (as given above, H maps S → O)
• Mealy-type
– Associates outputs with transitions (H maps S x I → O)
• Shorthand notations to simplify descriptions
– Implicitly assign 0 to all unassigned outputs in a state
– Implicitly AND every transition condition with clock edge (FSM is synchronous)

7
Finite-state machine with
datapath model (FSMD)
• FSMD = FSM + complex data types & variables for storing data
– FSMs use only Boolean data types and operations, no variables
We described UnitControl as an FSMD
• FSMD: 7-tuple <S, I , O, V, F, H, s0>
req > floor
– S is a set of states {s0, s1, …, sl}
u,d,o, t = 1,0,0,0 GoingUp !(req > floor)
– I is a set of inputs {i0, i1, …, im}
req > floor timer < 10
– O is a set of outputs {o0, o1, …, on} u,d,o,t = 0,0,1,0 !(timer < 10)
Idle DoorOpen
– V is a set of variables {v0, v1, …, vn} req ==
floor
req < floor
u,d,o,t =
0,0,1,1
– F is a next-state function (S x I x V → S) u,d,o,t = 0,1,0,0 GoingDn
!(req<floor)

– H is an action function (S → O + V) u is up, d is down, o is open


req < floor t is timer_start
– s0 is an initial state
• I,O,V may represent complex data types (i.e., integers, floating point, etc.)
• F,H may include arithmetic operations
• H is an action function, not just an output function
– Describes variable updates as well as outputs
• Complete system state now consists of current state, si, and values of all variables

8
Describing a system as a state machine

1. List all possible states


2. Declare all variables
3. For each state, list possible transitions, with conditions, to other states
4. For each state and/or transition, list associated actions
5. For each state, ensure exclusive and complete exiting transition
conditions
• No two exiting conditions can be true at same time
– Otherwise nondeterministic state machine
• One condition must be true at any given time
– Reducing explicit transitions should be avoided when first learning

9
FSM in sequential programming

• Most popular development tools use sequential programming language


– C, C++, Java, Ada, VHDL, Verilog, etc.
• Two approaches
– Front-end tool approach
• Additional tool installed to support state machine language
– Graphical and/or textual state machine languages
– May support graphical simulation
– Automatically generate code in sequential programming language that is input to main development tool
• Drawback: must support additional tool (licensing costs, upgrades, training, etc.)
– Language subset approach
• Most common approach...

10
Language subset
approach
• Equivalent sequential language
constructs (e.g. C and VHDL) #define IDLE0
#define GOINGUP1
#define GOINGDN2
• State machine in C #define DOOROPEN3
– Enumerate all states (#define) void UnitControl() {
int state = IDLE;
– Declare state variable initialized to while (1) {
switch (state) {
initial state (IDLE) IDLE: up=0; down=0; open=1; timer_start=0;
if (req==floor) {state = IDLE;}
– Single switch statement branches to if (req > floor) {state = GOINGUP;}
current state’s case if (req < floor) {state = GOINGDN;}
break;
– Each case has actions GOINGUP: up=1; down=0; open=0; timer_start=0;
if (req > floor) {state = GOINGUP;}
• up, down, open, timer_start if (!(req>floor)) {state = DOOROPEN;}
break;
– Each case checks transition conditions GOINGDN: up=1; down=0; open=0; timer_start=0;
to determine next state if (req < floor) {state = GOINGDN;}
if (!(req<floor)) {state = DOOROPEN;}
• if(…) {state = …;} break;
DOOROPEN: up=0; down=0; open=1; timer_start=1;
if (timer < 10) {state = DOOROPEN;}
if (!(timer<10)){state = IDLE;}
break;
}
}
}

11
General template
#define S0 0
#define S1 1
...
#define SN N
void StateMachine() {
int state = S0; // or whatever is the initial state.
while (1) {
switch (state) {
S0:
// Insert S0’s actions here & Insert transitions Ti leaving S0:
if( T0’s condition is true ) {state = T0’s next state; /*actions*/ }
if( T1’s condition is true ) {state = T1’s next state; /*actions*/ }
...
if( Tm’s condition is true ) {state = Tm’s next state; /*actions*/ }
break;
S1:
// Insert S1’s actions here
// Insert transitions Ti leaving S1
break;
...
SN:
// Insert SN’s actions here
// Insert transitions Ti leaving SN
break;
}
}
}
HCFSM
• Hierarchical/concurrent state machine model (HCFSM)
– Extension to state machine model to support hierarchy and concurrency
– States can be decomposed into another state machine
• With hierarchy has identical functionality as Without hierarchy, but has one less transition (z)
• Known as OR-decomposition
– States can execute concurrently
• Known as AND-decomposition

Without hierarchy With hierarchy


Concurrency
A B
A1
z C D
A1
z
x w C1 D1
y B x
y B x u
w y v

A2 z A2 C2 D2
Concurrent processes
• Communication Encoded video packets
– Shared memory processA() {
// Decode packet
• No time overhead, easy to implement // Communicate packet to B
}
}
• But, hard to use – mistakes are common
– Message passing
Decoded video packets
• Data explicitly sent from one process to processB() {
another // Get packet from A
// Display packet
• Safer model, but less flexible }

• Synchronization
• Scheduling To display

16
Scheduling: priority
• Process with highest priority selected first
– Statica, Dynamic
• FIFO
– Runnable processes added to end of FIFO as created or become runnable
– Front process removed from FIFO when time quantum of current process is up
or process is blocked
• Priority queue
– Runnable processes again added as created or become runnable
– Process with highest priority chosen when new process needed
– If multiple processes with same highest priority value then selects from them
using first-come first-served
– non preemptive  Priority scheduling
– Preemptive Called round-robin

17
Priority assignment
• Rate monotonic scheduling
– Processes with shorter periods have higher priority Rate monotonic
Process Period Priority
– Typically used when execution deadline = period
• Deadline monotonic scheduling A 25 ms 5
B 50 ms 3
– Processes with shorter deadlines have higher priority C 12 ms 6
D 100 ms 1
– Typically used when execution deadline < period E 40 ms 4
F 75 ms 2

Deadline monotonic
Process Deadline Priority

G 17 ms 5
H 50 ms 2
I 32 ms 3
J 10 ms 6
K 140 ms 1
L 32 ms 4

18
Creating Tasks in C
Main()

You might also like