Sunteți pe pagina 1din 16

Components for embedded

programs
● State machine

● Circular buffer
3 components ● Queue
State machine
When inputs appear intermittently rather than as periodic samples, it is often convenient to think of the
system as reacting to those inputs. The reaction of most systems can be characterized in terms of the
input received and the current state of the system.This leads naturally to a finite-state machine style of
describing the reactive system’s behavior.

Example of finite state machine:

The behavior we want to implement is a simple seat belt controller [Chi94]. The controller’s job is to turn
on a buzzer if a person sits in a seat and does not fasten the seat belt within a fixed amount of time. This
system has three inputs and one output. The inputs are a sensor for the seat to know when a person has
sat down, a seat belt sensor that tells when the belt is fastened, and a timer that goes off when the
required time interval has elapsed. The output is the buzzer. Appearing below is a state diagram that
describes the seat belt controller’s behavior.
Circular Buffer
● The circular buffer is a data structure that lets us handle streaming data in an efficient way.
● At each point in time, the algorithm needs a subset of the data stream that forms a window into the
stream.
● The window slides with time as we throw out old values no longer needed and add new values.
● Because the size of the window does not change, we can use a fixed-size buffer to hold the current
data. To avoid constantly copying data within the buffer, we will move the head of the buffer in time.
● Because the size of the window does not change, we can use a fixed-size buffer to hold the current
data. To avoid constantly copying data within the buffer, we will move the head of the buffer in time.
● When the pointer gets to the end of the buffer, it wraps around to the top.
FIR Filter using circular buffer
● The filter operates at a sample rate with inputs arriving and outputs generated at the sample rate.
● The inputs x[n] and y[n] are sequences indexed by n, which corresponds to the sequence of samples.
● Nodes in the graph can represent either arithmetic operators or delay operators. The + node adds its two inputs and produces
the output y[n]. The box labeled z-1 is a delay operator.
The delay elements running vertically hold the input samples with the most recent sample at the top and the oldest one at the bottom.
Unfortunately, the signal flow graph doesn’t explicitly label all of the values that we use as inputs to operations, so the figure also
shows the values (xi) we need to operate on in our FIR loop. When we compute the filter function, we want to match the bi’s and xi’s.
We will use our circular buffer for the x’s, which change over time. We will use a standard array for the b’s, which don’t change. In order
for the filter function to be able to use the same I value for both sets of data, we need to put the x data in the proper order. We can put
the b data in a standard array with b0 being the first element. When we add a new x value, it becomes x 0 and replaces the oldest data
value in the buffer. This means that the buffer head moves from higher to lower values, not lower to higher as we might expect.
Queues and producer/consumer
Queues are used whenever data may arrive and depart at somewhat unpredictable times or when variable amounts of data may arrive.
A queue is often referred to as an elastic buffer.
● When several of these systems operate in a chain, the variable-rate output of one stage becomes
the variable-rate input of another stage.
● In producer/consumer system. p1 and p2 are the two blocks that perform algorithmic processing.
The data is fed to them by queues that act as elastic buffers.
● The queues modify the flow of control in the system as well as store data. If, for example, p2 runs
ahead of p1, it will eventually run out of data in its q12 input queue. At that point, the queue will
return an empty signal to p2. At this point, p2 should stop working until more data is available.
● The queues in a producer/consumer may hold either uniform-sized data elements or variable-sized
data elements.
Models of programs
● Data flow graph

2 models ● Control/data flow graph


Data flow graph
● A data flow graph is a model of a program with no conditionals. In a high-level programming
language, a code segment with no conditionals—more precisely, with only one entry and exit
point—is known as a basic block.
● We need to rewrite the code in single-assignment form, in which a variable appears only once on the
left side.
● The single-assignment form is important because it allows us to identify a unique location in the
code where each named location is computed.
● We use two types of nodes in the graph—round nodes denote operators and square nodes represent
values. The value nodes may be either inputs to the basic block, such as a and b, or variables
assigned to within the block.
● The data flow graph for the code makes the order in which the operations are performed in the C
code much less obvious. This is one of the advantages of the data flow graph. We can use it to
determine feasible reorderings of the operations, which may help us to reduce pipeline or cache
conflicts.
Control/data flow graphs
● A CDFG uses a data flow graph as an element, adding constructs to describe control.In a basic
CDFG, we have two types of nodes: decision nodes and data flow nodes.
● A data flow node encapsulates a complete data flow graph to represent a basic block.
● We can use one type of decision node to describe all the types of control in a sequential program.
● An execution model for a CDFG is very much like the execution of the program it represents. The
CDFG does not require explicit declaration of variables but we assume that the implementation has
sufficient memory for all the variables.
● Even though the data flow nodes may specify only a partial ordering on the data flow computations,
the CDFG is a sequential representation of the program.
● The CDFG is not necessarily tied to high-level language control structures. We can also build a CDFG
for an assembly language program.

S-ar putea să vă placă și