+ All Categories
Home > Documents > Finite State Machines and Micro Threds

Finite State Machines and Micro Threds

Date post: 02-Apr-2018
Category:
Upload: neethusubrahmanyan
View: 218 times
Download: 0 times
Share this document with a friend

of 75

Transcript
  • 7/27/2019 Finite State Machines and Micro Threds

    1/75

    1Copyright Micron Engineering 2007

    Finite State Machines and

    Micro Threads

    Massimo Manca

    Micron Engineering

  • 7/27/2019 Finite State Machines and Micro Threds

    2/75

    2Copyright Micron Engineering 2007

    What is this talk about?

    Finite state machines

    UML state chart as FSM notation

    UML state chart to C code

    UML state charts to C code using MicroFSM

  • 7/27/2019 Finite State Machines and Micro Threds

    3/75

    3Copyright Micron Engineering 2007

    Finite state machines

    A finite state machine (FSM)

    or finite state automaton is

    a model of behavior made

    of a finite number ofstates,

    transitions, and actions.

  • 7/27/2019 Finite State Machines and Micro Threds

    4/75

  • 7/27/2019 Finite State Machines and Micro Threds

    5/755Copyright Micron Engineering 2007

    Finite state machines

    A finite state machine (FSM)

    or finite state automaton is

    a model of behavior made

    of a finite number ofstates,

    transitions, and actions.

    A state stores information

    about the past, a transition

    indicates a state change due

    to a fulfilled condition...

  • 7/27/2019 Finite State Machines and Micro Threds

    6/756Copyright Micron Engineering 2007

    Finite state machines

    A finite state machine (FSM)

    or finite state automaton is

    a model of behavior made

    of a finite number ofstates,

    transitions, and actions.

    A state stores information

    about the past, a transition

    indicates a state change due

    to a fulfilled condition and

    an action is a description of

    an activity that is to be

    performed at a given

    moment.

  • 7/27/2019 Finite State Machines and Micro Threds

    7/757Copyright Micron Engineering 2007

    Finite state machines

    Action types:

    Entry action: performed entering a state

    Exit action: performed exiting a state

    Input action: performed depending on presentstate and input conditions

    NOTE: Mealy and Moore FSM don't have exit actions.

  • 7/27/2019 Finite State Machines and Micro Threds

    8/75

    8Copyright Micron Engineering 2007

    FSM representation

    FSM can be represented using a state diagram as above.Besides this, several state transition table types are

    used. The most common representation is shown below:

    the combination of current state and condition shows

    the next state. The complete actions information can be

    added only using footnotes. An FSM definition includingthe full actions information is possible using state tables.

  • 7/27/2019 Finite State Machines and Micro Threds

    9/75

    9Copyright Micron Engineering 2007

    FSM sequence detectorsAcceptors and recognizers (sequence detectors)

    produce a binary output, saying either yes or no to

    answer whether the input is accepted by the machine or

    not. All states of the FSM are said to be either accepting

    or not accepting. If when all input is processed the

    current state is an accepting state, the input is

    accepted; otherwise it is rejected. As a rule the input

    are symbols (characters); actions are not used.

    Above FSM recognize the word sos.

  • 7/27/2019 Finite State Machines and Micro Threds

    10/75

    10Copyright Micron Engineering 2007

    FSM mathematical model

    An acceptor FSM is a quintuple (,S,s0, ,F), where:

    is the input alphabet (a finite non empty set of symbols).

    S is a finite non empty set of states.

    s0

    is an initial state, an element ofS. In a non deterministic

    finite state machine, s0

    is a set of initial states.

    is the state transition function: = SS. F is the set of final states a (possibly empty) subset of S.

  • 7/27/2019 Finite State Machines and Micro Threds

    11/75

    11Copyright Micron Engineering 2007

    FSM transducer models

    Transducers generate output based on a given input and/or astate using actions. They are used for control applications.

    Here two types are distinguished also if in practice mixed

    models are often used. Using exit actions (unknown in Moore

    and Mealy models) inside FSM's states allows to achieve

    optimal solutions minimizing the number of the states easy to

    code as in the Moore model.

    Moore machine

    The next state of the FSM

    depends only on the present

    state, it uses only entryactions. The main advantage

    of the Moore model is a

    simplification of the

    behaviour.

    Mealy machine

    The next state of the FSM

    depends on inputs and present

    state, it uses only inputactions. The use of a Mealy

    FSM leads often to a reduction

    of the number of states.

  • 7/27/2019 Finite State Machines and Micro Threds

    12/75

    12Copyright Micron Engineering 2007

    FSM mathematical model

    A transducer FSM is a sextuple (,,S,s0,,) where:

    is the input alphabet (a finite non empty set of symbols).

    is the output alphabet (a finite non empty set of symbols).

    S is a finite non empty set of states.

    s0 is the initial state, an element of S. In a non deterministic

    finite state machine, s0 is a set of initial states. is the state transition function: = SS. is the output function.

    If the output function is a function of a state and input

    alphabet S that definition corresponds to the Mealymodel. If the output function depends only on a state S that definition corresponds to the Moore model.

  • 7/27/2019 Finite State Machines and Micro Threds

    13/75

    13Copyright Micron Engineering 2007

    UML State Diagrams

    The Unified Modeling Language (UML) state diagram isessentially a state diagram with standardized notation

    that can describe a lot of things, from computer

    programs to business processes. State charts were

    developed by David Harel, to add nesting and other

    features to flat state machines and were later added toUML and standardized. They are an excellent tool for

    modeling modules (or classes), sub-systems and

    interfaces that have many distinct states and complex

    transitions among them. The following is a brief summary

    of the notation and behavior of state charts. For a fullpresentation of the UML state chart notation, see the

    UML 2.0 specification, available at:

    www.omg.org/technology/documents/modeling_spec_catalog.htm

  • 7/27/2019 Finite State Machines and Micro Threds

    14/75

    14Copyright Micron Engineering 2007

    UML state notation

    Graphically, UML shows

    states as boxes with rounded

    corners

  • 7/27/2019 Finite State Machines and Micro Threds

    15/75

    15Copyright Micron Engineering 2007

    UML transitions notation

    Graphically, UML shows

    states as boxes with rounded

    corners and transitions as

    arrows lines.

  • 7/27/2019 Finite State Machines and Micro Threds

    16/75

    16Copyright Micron Engineering 2007

    UML transitions notation

    Graphically, UML shows

    states as boxes with rounded

    corners and transitions as

    arrows lines. The transitions

    are labeled with the event

    that causes the transition.

  • 7/27/2019 Finite State Machines and Micro Threds

    17/75

    17Copyright Micron Engineering 2007

    UML transitions notation

    Graphically, UML shows

    states as boxes with rounded

    corners and transitions as

    arrows lines. The transitions

    are labeled with the event

    that causes the transition. A

    condition called guard can

    be indicated in square

    brackets...

  • 7/27/2019 Finite State Machines and Micro Threds

    18/75

    18Copyright Micron Engineering 2007

    UML transitions notation

    Graphically, UML shows

    states as boxes with rounded

    corners and transitions as

    arrows lines. The transitions

    are labeled with the event

    that causes the transition. A

    condition called guard can

    be indicated in square

    brackets followed by the

    action(s) that will be taken

    upon transition.

  • 7/27/2019 Finite State Machines and Micro Threds

    19/75

    19Copyright Micron Engineering 2007

    UML states detailedA state can be divided in 2

    areas: the upper for its name

  • 7/27/2019 Finite State Machines and Micro Threds

    20/75

    20Copyright Micron Engineering 2007

    UML states detailedA state can be divided in 2

    areas: the upper for its name

    and the lower for its actions.

    Actions are divided in:

    entry actions:

    executed entering the state

  • 7/27/2019 Finite State Machines and Micro Threds

    21/75

    21Copyright Micron Engineering 2007

    UML states detailedA state can be divided in 2

    areas: the upper for its name

    and the lower for its actions.

    Actions are divided in:

    entry actions:

    executed entering the state

    do actions:

    executed inside the state

  • 7/27/2019 Finite State Machines and Micro Threds

    22/75

    22Copyright Micron Engineering 2007

    UML states detailedA state can be divided in 2

    areas: the upper for its name

    and the lower for its actions.

    Actions are divided in:

    entry actions:

    executed entering the state

    do actions:

    executed inside the state

    exit actions:

    executed leaving the state

  • 7/27/2019 Finite State Machines and Micro Threds

    23/75

    23Copyright Micron Engineering 2007

    UML states detailedA state can be divided in 2

    areas: the upper for its nameand the lower for its actions.

    Actions are divided in:

    entry actions:

    executed entering the state

    do actions:

    executed inside the state

    exit actions:

    executed leaving the state

    event actions:executed due to an event

    (specified inside a transition)

  • 7/27/2019 Finite State Machines and Micro Threds

    24/75

  • 7/27/2019 Finite State Machines and Micro Threds

    25/75

    25Copyright Micron Engineering 2007

    UML pseudo statesThe UML notation for state

    carts introduces new symbols:a pseudo state to mark the

    initial state and a pseudo

    state to mark the final state.

  • 7/27/2019 Finite State Machines and Micro Threds

    26/75

    26Copyright Micron Engineering 2007

    UML pseudo statesThe UML notation for state

    carts introduces new symbols:a pseudo state to mark the

    initial state and a pseudo

    state to mark the final state.

    Either connects to the states

    by a transition that may becompleted with the notation

    seen before with event, guard

    and action fields.

  • 7/27/2019 Finite State Machines and Micro Threds

    27/75

  • 7/27/2019 Finite State Machines and Micro Threds

    28/75

  • 7/27/2019 Finite State Machines and Micro Threds

    29/75

    29Copyright Micron Engineering 2007

    UML junction point

    NOTE: it is not mandatory that a junction point hastransitions to provide every possible conditions to

    change state. In the above example there isn't a

    transition for 3

  • 7/27/2019 Finite State Machines and Micro Threds

    30/75

    30Copyright Micron Engineering 2007

    UML choice point

    A choice point always has1 entering transition and 2

    or more exiting transitions

    implementing a dynamic

    choice based on the event

    on the entering transition.The guard conditions are

    evaluated reaching the

    choice point so transitions

    outgoing from choice point

    have to provide guards to

    cover all possible

    conditions.

    Suggestion: always

    provide an else condition.

  • 7/27/2019 Finite State Machines and Micro Threds

    31/75

    31Copyright Micron Engineering 2007

    This FSM describes the procedure to start an engineusing a minimum notation:

    The power of UML notation is due to its flexibility, it may

    be used at different level of detail depending by user'sneeds; the user is responsible to balance the diagram

    detail and the information contained in the diagram

    itself.

    State chart example 1

  • 7/27/2019 Finite State Machines and Micro Threds

    32/75

    32Copyright Micron Engineering 2007

    State chart example 2

    This is a more detailed example used to understand theorder of the execution for all the actions inside a state

    chart:

    the execution order from left to right is:

    lamp.on() printf(exiting ON) printf(to OFF)lamp.off() printf(exiting OFF)

    printf(needless)

  • 7/27/2019 Finite State Machines and Micro Threds

    33/75

  • 7/27/2019 Finite State Machines and Micro Threds

    34/75

    34Copyright Micron Engineering 2007

    State chart symbols 2

    The following are the remaining notational elements thatcan be used to make up a state diagram. Their use is

    limited to concurrent states and their synchronization.

  • 7/27/2019 Finite State Machines and Micro Threds

    35/75

    35Copyright Micron Engineering 2007

    UML state charts to C code

    There are 2 main well established methods to convert anUML state chart diagram to C:

    A nested switch(..) statement with a scalar state

    variable used as a discriminator in one level of the

    switch and the event-type in the other.

    A state table containing an (typically sparse) array oftransition for each state. The table lists event types

    (triggers) along one dimension and the states along the

    other.

    Both methods have pros and cons, aren't well defined

    and the implementation robustness depends by theprogrammer skills. I will propose MicroFSM a set of

    constructs giving a simple method to translate a state

    chart to an equivalent C code using simple templates.

  • 7/27/2019 Finite State Machines and Micro Threds

    36/75

    36Copyright Micron Engineering 2007

    MicroFSM implementation

    I will propose a simple method to realize a state chart

    using C code and simple templates.

    MicroFSM proof of concept

    Very portable, only pure ANSI C

    Use the C preprocessor

    None assembler required

    Realized using the switch() statement under the hood

    None library required, only a file to include

    Short learning curve

    Printed templates to manually code a state chart

  • 7/27/2019 Finite State Machines and Micro Threds

    37/75

    37Copyright Micron Engineering 2007

    MicroFSM implementation

    In MicroFSM a state chart have to be entirely contained

    inside a C function...

    FsmDeclare(LightFsm(char off));

  • 7/27/2019 Finite State Machines and Micro Threds

    38/75

    38Copyright Micron Engineering 2007

    MicroFSM implementation

    In MicroFSM a state chart have to be entirely contained

    inside a C function...

    FsmDeclare(LightFsm(char off));

    that may have any number of parameters of any type...

    FsmDeclare(EngineFsm(int reset, char*err));

  • 7/27/2019 Finite State Machines and Micro Threds

    39/75

    39Copyright Micron Engineering 2007

    MicroFSM implementation

    In MicroFSM a state chart have to be entirely containedinside a C function...

    FsmDeclare(LightFsm(char off));

    that may have any number of parameters of any type...

    FsmDeclare(EngineFsm(int reset, char*err));

    and returns an int representing its running state...

    int s = EngineFsm(0, strErr);

  • 7/27/2019 Finite State Machines and Micro Threds

    40/75

    40Copyright Micron Engineering 2007

    MicroFSM implementation

    In MicroFSM a state chart have to be entirely containedinside a C function...

    FsmDeclare(LightFsm(char off));

    that may have any number of parameters of any type...

    FsmDeclare(EngineFsm(int reset, char*err));

    and returns an int representing its running state...

    int s = EngineFsm(0, strErr);

    MicroFSM running state can be one of these:

    ENDEDthe fsm ended its execution cycle, at next invocationit will restart from its starting state

    EXITEDthe fsm terminated its executions for ever

    WAITINGthe execution point is inside a state

  • 7/27/2019 Finite State Machines and Micro Threds

    41/75

    41Copyright Micron Engineering 2007

    MicroFSM implementation

    FsmDeclare(LightFsm(char off))

    {

    FSM_STATES ON=FIRST_STATE, OFF // states declared

    ...

    }

    In MicroFSM a state chart is made of a set of states...

    Mi FSM i l i

  • 7/27/2019 Finite State Machines and Micro Threds

    42/75

    42Copyright Micron Engineering 2007

    MicroFSM implementation

    FsmDeclare(LightFsm(char off))

    {

    FSM_STATES ON=FIRST_STATE, OFF // states declar.

    FSM_BEGIN // fsm entry point

    ...FSM_END // fsm end

    }

    In MicroFSM a state chart is made of a set of states...

    the fsm has its entry point and its block of code...

    Mi FSM i l i

  • 7/27/2019 Finite State Machines and Micro Threds

    43/75

    43Copyright Micron Engineering 2007

    MicroFSM implementation

    FsmDeclare(LightFsm(char off))

    {

    FSM_STATES ON=FIRST_STATE, OFF // states declar.

    FSM_BEGIN // fsm entry point

    ...FSM_STATE(ON) // this is

    state ON...

    FSM_STATE_END // end state ONFSM_STATE(OFF) // this is state OFF

    ...FSM_STATE_END // end state ON

    FSM_END // fsm end}

    In MicroFSM a state chart is made of a set of states...

    the fsm has its entry point and its block of code...

    every state has its block of code...

    Mi FSM i l t ti

  • 7/27/2019 Finite State Machines and Micro Threds

    44/75

    44Copyright Micron Engineering 2007

    MicroFSM implementation

    FsmDeclare(LightFsm(char off))

    {

    FSM_STATES ON=FIRST_STATE, OFF // states declar.

    FSM_BEGIN // fsm entry point

    ...FSM_STATE(ON) // this is

    state ONif(off)

    FsmNextState(OFF);FSM_STATE_END // end state ONFSM_STATE(OFF) // this is state OFF

    ...FSM_STATE_END // end state ON

    FSM_END // fsm end

    }

    In MicroFSM basically a state chart is a set of states...

    the fsm has its entry point and its block of code...

    every state has its block of code...

    transitions are embedded in the source state...

  • 7/27/2019 Finite State Machines and Micro Threds

    45/75

    Mi FSM i l t ti

  • 7/27/2019 Finite State Machines and Micro Threds

    46/75

    46Copyright Micron Engineering 2007

    MicroFSM implementation

    ...

    FSM_STATE(ON) // this is

    state ONif(off)print(exitingON);...

    In MicroFSM a transitions consist in a guard condition to test

    followed by an action (a block of code) to do

    Mi FSM i l t ti

  • 7/27/2019 Finite State Machines and Micro Threds

    47/75

    47Copyright Micron Engineering 2007

    MicroFSM implementation

    ...

    FSM_STATE(ON) // this is

    state ONif(off) {print(exitingON);FsmNextState(OFF);

    }...

    In MicroFSM a transitions consist in a guard condition to test

    followed by an action (a block of code) to do and thejump to

    the new state

    THIS IS ATRANSITION

    MicroFSM implementation

  • 7/27/2019 Finite State Machines and Micro Threds

    48/75

    48Copyright Micron Engineering 2007

    MicroFSM implementation

    // these 2 blocks are equivalent

    ...FSM_STATE(ON)FsmChangeState(off, OFF);...

    FSM_STATE(ON)if(off);

    FsmNextState(OFF);......

    FSM_STATE(OFF)FsmTransition(off, printf(needless), OFF);

    ...

    To manage the transitions there are more constructs,

    FsmTransition(guard, exec, nextstate) is the more complete

    but to conditionally change state without execute any action

    FsmChangeState(guard, nextstate) is the best one.

    THESE AREALL THE SAME

    TRANSITION

    COMPLETE TRANSITION

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    49/75

    49Copyright Micron Engineering 2007

    MicroFSM under the hood

    #define FIRST_STATE -127

    #define FSM_EXITED 0

    #define FSM_WAITING 1#define FSM_ENDED 2

    #define FSM_DECLARE(name_args) int name_args

    #define FSM_STATES static enum eFsmState {

    #define END_STATES } eStateCnt=FIRST_STATE; char cSuspend=1;

    #define FSM_BEGIN END_STATES FSM_START

    #define FSM_STATE(a) case a :

    #define FSM_STATE_END break;#define FsmChangeState(cond, nextstate) \

    if(cond) eStateCnt=nextstate

    #define FsmNextState(nextstate) eStateCnt=nextstate

    #define FsmSuccState() eStateCnt=eStateCnt + 1

    #define FsmPrevState() eStateCnt=eStateCnt - 1

    The complete implementation of MicroFsm is contained in

    about 60 lines of C preprocessor directives based on switch()

    instruction and local continuations, these are those discussed

    in the previous pages:

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    50/75

    50Copyright Micron Engineering 2007

    MicroFSM under the hoodWhat are the difference between a FSM implemented with

    normal switch() construct and MicroFSM?

    MicroFSM syntax give prominence to states and transitions

    MicroFSM details are hidden on the function containing it

    MicroFsm flexible syntax permits to realize a function with

    FSM and mixed normal procedural code

    MicroFSM has pre defined constructs able to solve commonsituations as suspend and restart its execution

    MicroFSM has constructs able to handlepseudo statesMicroFSM execution can continue across 2 or more states until

    there is something to wait for

    MicroFSM syntax masks a more efficient C code

    MicroFSM can be expanded and adapted to a lof of situations

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    51/75

    51Copyright Micron Engineering 2007

    MicroFSM under the hoodWhat arepseudo states in UML?

    The UML state chart notation defines pseudo state the

    initial and last state symbols because they aren't realstates but just a graphic sign evidencing 2 states.

    What arepseudo states in MicroFSM?

    A pseudo state is every state that can be realized with a

    blocking wait construct without altering the original

    behaviour.

    What is a blocking wait in MicroFSM?

    It is a construct that evaluates a guard condition, if true it

    will continue the execution flow of the FSM code, if false itwill exit from the FSM and next time the execution flow of

    the FSM will start from the blocking wait construct.

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    52/75

    52Copyright Micron Engineering 2007

    What blocking wait constructs are provided in MicroFSM?

    They are:

    FsmWaitUntil(condition)

    FsmWaitWhile(condition)

    FsmDoWaitUntil(exec, condition)

    FsmDoWaitWhile(exec, condition)

    FsmSuspend()

    FsmSuspendUntil(condition)

    and how are implemented blocking wait constructs?

    They are:

    based on local continuationsinspired to proto threads created by Adam Dunkels

    done with switch()...case and enum ANSI C instructions

    contained on a C function (also the state variable)

    MicroFSM under the hood

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    53/75

    53Copyright Micron Engineering 2007

    MicroFSM under the hood

    #define FsmWaitUntil(condition)\do { \

    FSM_SET(eStateCnt); \if(!(condition)) \

    return(FSM_WAITING); \

    } while(0)

    #define FsmSuspend() \

    do { \

    cSuspend = 0; \

    FsmWaitUntil(cSuspend); \

    } while(0)

    #define FsmSuspendUntil(cond) \

    do { \

    cSuspend = 0; \

    FsmWaitUntil(cSuspend || !(cond)); \

    cSuspend = 1; \

    } while(0)

    This is the implementation of blocking wait constructs

    implemented in MicroFsm: page 1

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    54/75

    54Copyright Micron Engineering 2007

    MicroFSM under the hood

    #define FsmWaitWhile(condition) \

    FsmWaitUntil(!(condition))

    #define FsmDoWaitUntil(exec, condition) \

    do { \

    FSM_SET(eStateCnt); \exec; \

    if(!(condition)) \

    return(FSM_WAITING); \

    } while(0)

    #define FsmDoWaitWhile(exec, condition) \

    FsmDoWaitUntil(!(condition))

    This is the implementation of blocking wait constructs

    implemented in MicroFsm: page 2

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    55/75

    55Copyright Micron Engineering 2007

    MicroFSM under the hoodAll the magic is contained in a single row:

    #define FSM_SET(s) s = (int)__LINE__; case __LINE__:

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    56/75

    56Copyright Micron Engineering 2007

    MicroFSM under the hoodAll the magic is contained in a single row:

    #define FSM_SET(s) s = (int)__LINE__; case __LINE__:

    this is the implementation of a local continuation; the

    __LINE__ macro during compilation is replaced with the

    current line number as an integer constant. This

    implementation works inside a MicroFSM because __LINE__

    constant can't be equal with any state label that are definedinside FSM_STATES.

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    57/75

    57Copyright Micron Engineering 2007

    MicroFSM under the hoodAll the magic is contained in a single row:

    #define FSM_SET(s) s = (int)__LINE__; case __LINE__:

    this is the implementation of a local continuation; the

    __LINE__ macro during compilation is replaced with the

    current line number as an integer constant. This

    implementation works inside a MicroFSM because __LINE__

    constant can't be equal with any state label that are definedinside FSM_STATES. The following example...

    FSM_STATES ON=FIRST_STATE, OFF // states declaration

    FSM_BEGIN

    ...

    after preprocessing will be expanded as:

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    58/75

    58Copyright Micron Engineering 2007

    MicroFSM under the hoodAll the magic is contained in a single row:

    #define FSM_SET(s) s = (int)__LINE__; case __LINE__:

    this is the implementation of a local continuation; the

    __LINE__ macro during compilation is replaced with the

    current line number as an integer constant. This

    implementation works inside a MicroFSM because __LINE__

    constant can't be equal with any state label that are definedinside FSM_STATES. The following example...

    FSM_STATES ON=FIRST_STATE, OFF // states declaration

    FSM_BEGIN

    ...

    after preprocessing will be expanded as:static enum eFsmState{ ON=-127, OFF }eStateCnt=-127;char cSuspend=1;

    switch(eStateCnt) {

    ...

    MicroFSM under the hood

  • 7/27/2019 Finite State Machines and Micro Threds

    59/75

    59Copyright Micron Engineering 2007

    MicroFSM under the hoodSo notations for transitions and blocking wait constructs

    can be intermixed; what is the advantage?Using MicroFSM a lot of states collapses in pseudo states

    because in FSMs a lot of states require only one transition

    Code is more compact

    To convert a FSM to C code we can identify the simpler

    FSM buildingblocks and convert them to C code, then use

    them as a set of templates.

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    60/75

    60Copyright Micron Engineering 2007

    MicroFSM templates

    This is a sequence:...

    FsmTransition(guard,event(),B);

    ... // or

    if(guard) {

    event();

    FsmNextState(B);

    }

    ... // or

    if(guard) {

    event();

    FsmSuccState();}

    ... // or

    FsmWaitUntil(guard);

    event();

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    61/75

    61Copyright Micron Engineering 2007

    MicroFSM templates

    This is an iteration: while(g1) {

    if(g2) {

    e2();

    FsmNextState(B); }

    e1();

    }

    ... // or

    while(g1) {

    FsmWaitUntil(!g1 || g2);

    FsmTransition(g2,e2(),B);

    }

    e1();}

    B is supposed to be the next state. Pay attention to chooseg1 to be sure that the only way to exit from the while loop

    is when g2 is evaluated true.

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    62/75

    62Copyright Micron Engineering 2007

    MicroFSM templates

    This is a selection: FsmTransition(g1,e1(),B);FsmTransition(g2,e2(),C);... // orFsmWaitUntil(g1 || g2);if(g1) {

    FsmNextState(B);e1();

    }else if(g2) {

    FsmNextState(C);e2();

    }... // or

    FsmWaitUntil(g1 || g2);FsmChangeState(g1,B);FsmChangeState(g2,C);...

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    63/75

    63Copyright Micron Engineering 2007

    M c o SM te plates

    This is always a selection: FsmTransition(g1,e1(),B);FsmTransition(g2,e2(),C);... // orFsmWaitUntil(g1 || g2);if(g1) {

    FsmNextState(B);e1();

    }else if(g2) {

    FsmNextState(C);e2();

    }... // or

    FsmWaitUntil(g1 || g2);FsmChangeState(g1,B);FsmChangeState(g2,C);...

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    64/75

    64Copyright Micron Engineering 2007

    p

    Let's try to add a do action:FSM_STATE(A)

    c++;

    FsmTransition(guard, \

    event, B);

    FSM_STATE_END

    ... // or

    FsmDoWaitUntil(c++,guard);

    event();

    ...

    if the action is more complex it may be a block of

    instructions or a function call for example:

    FsmDoWaitUntil(b=GainGet(), b>10);

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    65/75

    65Copyright Micron Engineering 2007

    p

    Let's try to add an exit action:FSM_STATE(A)

    ...

    if(g1 || g2)

    ex();

    FsmTransition(g1,e1(), B);

    FsmTransition(g2,e2(), C);

    FSM_STATE_END

    ... // or

    FsmWaitUntil(g1 || g2);

    ex();

    FsmTransition(g1,e1(), B);

    FsmTransition(g2,e2(), C);

    ...

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    66/75

    66Copyright Micron Engineering 2007

    p

    Let's try to add an entry action:FSM_STATE(Aentry)

    entry();

    FsmSuccState();

    FSM_STATE(A)

    FsmTransition(g1,e1,B);

    FSM_STATE_END

    ... // or

    entry();

    FsmWaitUntil(g1);

    e1();...

    MicroFSM examples

  • 7/27/2019 Finite State Machines and Micro Threds

    67/75

    67Copyright Micron Engineering 2007

    p

    Ok, it is time to try a complete simple exercise, we can

    get the lamp state chart and transform to a complete

    piece of C code:

    MicroFSM examples

  • 7/27/2019 Finite State Machines and Micro Threds

    68/75

    68Copyright Micron Engineering 2007

    1: FsmDeclare(LightFsm(char off, int gpo)) {2: FSM_STATES ONentry=FIRST_STATE,ON,OFFentry,OFF // statesdecl.3: FSM_BEGIN4: FSM_STATE(ONentry) // this is entry ON5: lamp.on(); // entry action6: FsmSuccState();7: FSM_STATE(ON)8: if(off) { // transition guard test

    9: printf(exiting ON); // exit action10: FsmSuccState(); // change state

    11: printf(to OFF); // transition action12: }13: FSM_STATE_END // end state14: FSM_STATE(OFFentry)15: lamp.off(); // entry action16: FsmSuccState();17: FSM_STATE(OFF)18: while(off && !gpo) { // stay hereconditions19: if(off || gpo) // transitions guards cond.

    20: printf(exiting OFF); // exit action21: if(gpo) FsmExit(); // test final state guard

    22: else if(off) printf(needless); // auto transition..23: } //... guardtest24: FSM_STATE_END25: FSM_END

    26:}

    p

    MicroFSM examples

  • 7/27/2019 Finite State Machines and Micro Threds

    69/75

    69Copyright Micron Engineering 2007

    The code is compact and shows from the first line that it

    implements a FSM; its dedicated syntax give prominence to

    states and transitions making possible associations label-state

    and function-transition. So I give a complete description of

    the code and the implementation.

    FsmDeclare(LightFsm(char off)) is the C function

    prototype specialized for FSMs, it is important to say that thefunction may have an arbitrary number of parameterswithout requiring any change to te macro. So I may write:

    FSM_DECLARE(ModemFsm(char input))

    or:

    FSM_DECLARE(ModemFsm(char input, int timer))

    as required from the specific application.

  • 7/27/2019 Finite State Machines and Micro Threds

    70/75

    MicroFSM examples

  • 7/27/2019 Finite State Machines and Micro Threds

    71/75

    71Copyright Micron Engineering 2007

    This is an alternative solution using pseudo states constructs:

    1: FsmDeclare(LightFsm(char off, int gpo)) {2: FSM_STATES ONentry=FIRST_STATE,ON,OFFentry,OFF // statesdecl.

    3: FSM_BEGIN4: FSM_STATE(ON) // this is state ON5: lamp.on(); // entry action

    6: FsmWaitUntil(off); // transition guard test7: printf(exiting ON); // exit action

    8: printf(to OFF); // transition action9: // state OFF10: lamp.off(); // entry action11: while(off && !gpo) { // stay hereconditions

    12: FsmWaitUntil(off || gpo); // transitions guards cond.13: printf(exiting OFF); // exit action14: if(gpo) // test final state guard15: FsmExit();

    15: else {16: if(off) //... guard test17: printf(needless); // auto transition..

    18: }19: }20: FSM_STATE_END21: FSM_END22:}

    MicroFSM examples

  • 7/27/2019 Finite State Machines and Micro Threds

    72/75

    72Copyright Micron Engineering 2007

    FsmWaitUntil(off)

    If off is true the execution will continue on the next

    instruction otherwise it will exit the FSM and next FSM entry

    point will be the FsmWaitUntil(...) row.

    FsmExit()

    It will exit from the FSM; next state will be FIRST_STATE.

    The main difference with the previous version is that the

    code is shorter and there isn't FSM_STATE_END for state ON

    so the execution can pass trough from state A to state B

    without returning to the calling function.

  • 7/27/2019 Finite State Machines and Micro Threds

    73/75

  • 7/27/2019 Finite State Machines and Micro Threds

    74/75

    MicroFSM templates

  • 7/27/2019 Finite State Machines and Micro Threds

    75/75

    75Copyright Micron Engineering 2007

    1: #define BYTE_OF(a, b) (a)2: #define BIT_OF(a,b) (1


Recommended