Finite State Machines and Micro Threads Massimo Manca











































































- Slides: 75
Finite State Machines and Micro Threads Massimo Manca Micron Engineering Copyright Micron Engineering 2007 1
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 Micro. FSM Copyright Micron Engineering 2007 2
Finite state machines A finite state machine (FSM) or finite state automaton is a model of behavior made of a finite number of states, transitions, and actions. Copyright Micron Engineering 2007 3
Finite state machines A finite state machine (FSM) or finite state automaton is a model of behavior made of a finite number of states, transitions, and actions. A state stores information about the past. . . Copyright Micron Engineering 2007 4
Finite state machines A finite state machine (FSM) or finite state automaton is a model of behavior made of a finite number of states, transitions, and actions. A state stores information about the past, a transition indicates a state change due to a fulfilled condition. . . Copyright Micron Engineering 2007 5
Finite state machines A finite state machine (FSM) or finite state automaton is a model of behavior made of a finite number of states, 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. Copyright Micron Engineering 2007 6
Finite state machines Action types: Entry action: performed entering a state Exit action: performed exiting a state Input action: performed depending on present state and input conditions NOTE: Mealy and Moore FSM don't have exit actions. Copyright Micron Engineering 2007 7
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 including the full actions information is possible using state tables. Copyright Micron Engineering 2007 8
FSM sequence detectors Acceptors 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. Copyright Micron Engineering 2007 9
FSM mathematical model An acceptor FSM is a quintuple (Σ, S, s 0, �, F), where: Σ is the input alphabet (a finite non empty set of symbols). S is a finite non empty set of states. s is an initial state, an element of S. In a non deterministic 0 finite state machine, s 0 is a set of initial states. �is the state transition function: �= S S. F is the set of final states a (possibly empty) subset of S. Copyright Micron Engineering 2007 10
FSM transducer models Transducers generate output based on a given input and/or a state 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 Mealy machine The next state of the FSM depends only on the present state, it uses only entry actions. The main advantage of the Moore model is a simplification of the behaviour. The next state of the FSM depends on inputs and present state, it uses only input actions. The use of a Mealy FSM leads often to a reduction of the number of states. Copyright Micron Engineering 2007 11
FSM mathematical model A transducer FSM is a sextuple (Σ, Γ, S, s 0, δ, ω) 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. s is the initial state, an element of S. In a non 0 deterministic finite state machine, s 0 is a set of initial states. �is the state transition function: �= S S. ω is the output function. If the output function is a function of a state and input alphabet S that definition corresponds to the Mealy model. If the output function depends only on a state S that definition corresponds to the Moore model. Copyright Micron Engineering 2007 12
UML State Diagrams The Unified Modeling Language (UML) state diagram is essentially 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 to UML 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 full presentation of the UML state chart notation, see the UML 2. 0 specification, available at: www. omg. org/technology/documents/modeling_spec_catalog. htm Copyright Micron Engineering 2007 13
UML state notation Graphically, UML shows states as boxes with rounded corners Copyright Micron Engineering 2007 14
UML transitions notation Graphically, UML shows states as boxes with rounded corners and transitions as arrows lines. Copyright Micron Engineering 2007 15
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. Copyright Micron Engineering 2007 16
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. . . Copyright Micron Engineering 2007 17
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. Copyright Micron Engineering 2007 18
UML states detailed A state can be divided in 2 areas: the upper for its name Copyright Micron Engineering 2007 19
UML states detailed A 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 Copyright Micron Engineering 2007 20
UML states detailed A 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 Copyright Micron Engineering 2007 21
UML states detailed A 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 Copyright Micron Engineering 2007 22
UML states detailed A 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 event actions: executed due to an event (specified inside a transition) Copyright Micron Engineering 2007 23
UML pseudo states The UML notation for state carts introduces new symbols: a pseudo state to mark the initial state Copyright Micron Engineering 2007 24
UML pseudo states The 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. Copyright Micron Engineering 2007 25
UML pseudo states The 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 be completed with the notation seen before with event, guard and action fields. Copyright Micron Engineering 2007 26
UML junction point To simplify the graphical design of the state charts UML notation introduces junction points and choice points. Junction points are used to merge and split several transition paths in a state chart diagram because they accept several input and output transitions. Copyright Micron Engineering 2007 27
UML junction point To simplify the graphical design of the state charts UML notation introduces junction points and choice points. Junction points are used to merge and split several transition paths in a state chart diagram because they accept several input and output transitions. A junction point always is pointed by transitions of opposite directions that if include split or merge variables may depend on disjoint event parameters. Copyright Micron Engineering 2007 28
UML junction point NOTE: it is not mandatory that a junction point has transitions to provide every possible conditions to change state. In the above example there isn't a transition for 3<a<0. What does it mean? Simply that for 3<a<0 there will be not a transition to LEFT or RIGHT states and IDLE will remain the active state. Copyright Micron Engineering 2007 29
UML choice point A choice point always has 1 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. Copyright Micron Engineering 2007 30
State chart example 1 This FSM describes the procedure to start an engine using 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's needs; the user is responsible to balance the diagram detail and the information contained in the diagram itself. Copyright Micron Engineering 2007 31
State chart example 2 This is a more detailed example used to understand the order 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”) Copyright Micron Engineering 2007 32
State chart symbols 1 Reassuming: these are the basic notational elements that can be used to make up a diagram: Note: use of flow final is deprecated, use final state instead. Copyright Micron Engineering 2007 33
State chart symbols 2 The following are the remaining notational elements that can be used to make up a state diagram. Their use is limited to concurrent states and their synchronization. Copyright Micron Engineering 2007 34
UML state charts to C code There are 2 main well established methods to convert an UML 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 of transition 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 the programmer skills. I will propose Micro. FSM a set of constructs giving a simple method to translate a state chart to an equivalent C code using simple templates. Copyright Micron Engineering 2007 35
Micro. FSM implementation I will propose a simple method to realize a state chart using C code and simple templates. Micro. FSM 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 Copyright Micron Engineering 2007 36
Micro. FSM implementation In Micro. FSM a state chart have to be entirely contained inside a C function. . . Fsm. Declare(Light. Fsm(char off)); Copyright Micron Engineering 2007 37
Micro. FSM implementation In Micro. FSM a state chart have to be entirely contained inside a C function. . . Fsm. Declare(Light. Fsm(char off)); that may have any number of parameters of any type. . . Fsm. Declare(Engine. Fsm(int reset, char *err)); Copyright Micron Engineering 2007 38
Micro. FSM implementation In Micro. FSM a state chart have to be entirely contained inside a C function. . . Fsm. Declare(Light. Fsm(char off)); that may have any number of parameters of any type. . . Fsm. Declare(Engine. Fsm(int reset, char *err)); and returns an int representing its running state. . . int s = Engine. Fsm(0, str. Err); Copyright Micron Engineering 2007 39
Micro. FSM implementation In Micro. FSM a state chart have to be entirely contained inside a C function. . . Fsm. Declare(Light. Fsm(char off)); that may have any number of parameters of any type. . . Fsm. Declare(Engine. Fsm(int reset, char *err)); and returns an int representing its running state. . . int s = Engine. Fsm(0, str. Err); Micro. FSM running state can be one of these: ENDED the fsm ended its execution cycle, at next invocation it will restart from its starting state EXITED the fsm terminated its executions for ever WAITING the execution point is inside a state Copyright Micron Engineering 2007 40
Micro. FSM implementation In Micro. FSM a state chart is made of a set of states. . . Fsm. Declare(Light. Fsm(char off)) { FSM_STATES ON=FIRST_STATE, OFF // states declared. . . } Copyright Micron Engineering 2007 41
Micro. FSM implementation In Micro. FSM a state chart is made of a set of states. . . the fsm has its entry point and its block of code. . . Fsm. Declare(Light. Fsm(char off)) { FSM_STATES ON=FIRST_STATE, OFF // states declar. FSM_BEGIN // fsm entry point. . . FSM_END // fsm end } Copyright Micron Engineering 2007 42
Micro. FSM implementation In Micro. FSM 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. . . Fsm. Declare(Light. Fsm(char off)) { FSM_STATES ON=FIRST_STATE, OFF // states declar. FSM_BEGIN // fsm entry point. . . FSM_STATE(ON) //. . . FSM_STATE_END // FSM_STATE(OFF) //. . . FSM_STATE_END // FSM_END // fsm end this is state ON end state ON this is state OFF end state ON } Copyright Micron Engineering 2007 43
Micro. FSM implementation In Micro. FSM 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. . . Fsm. Declare(Light. Fsm(char off)) { FSM_STATES ON=FIRST_STATE, OFF // states declar. FSM_BEGIN // fsm entry point. . . FSM_STATE(ON) // if(off) Fsm. Next. State(OFF); FSM_STATE_END // FSM_STATE(OFF) //. . . FSM_STATE_END // FSM_END // fsm end this is state ON end state ON this is state OFF end state ON } Copyright Micron Engineering 2007 44
Micro. FSM implementation In Micro. FSM a transitions consist in a guard condition to test . . . FSM_STATE(ON) if(off). . . // this is state ON Copyright Micron Engineering 2007 45
Micro. FSM implementation In Micro. FSM a transitions consist in a guard condition to test followed by an action (a block of code) to do . . . FSM_STATE(ON) // this is state ON if(off) print(“exiting ON”); . . . Copyright Micron Engineering 2007 46
Micro. FSM implementation In Micro. FSM a transitions consist in a guard condition to test followed by an action (a block of code) to do and the jump to the new state. . . FSM_STATE(ON) // this is state ON if(off) { THIS IS A print(“exiting ON”); TRANSITION Fsm. Next. State(OFF); }. . . Copyright Micron Engineering 2007 47
Micro. FSM implementation To manage the transitions there are more constructs, Fsm. Transition(guard, exec, nextstate) is the more complete but to conditionally change state without execute any action Fsm. Change. State(guard, nextstate) is the best one. // these 2 blocks are equivalent. . . FSM_STATE(ON) Fsm. Change. State(off, OFF); THESE ARE. . . FSM_STATE(ON) ALL THE SAME if(off); TRANSITION Fsm. Next. State(OFF); . . . COMPLETE TRANSITION FSM_STATE(OFF) Fsm. Transition(off, printf(“needless”), OFF); . . . Copyright Micron Engineering 2007 48
Micro. FSM under the hood The complete implementation of Micro. Fsm 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: #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 e. Fsm. State { #define END_STATES } e. State. Cnt=FIRST_STATE; char c. Suspend=1; #define FSM_BEGIN END_STATES FSM_START #define FSM_STATE(a) case a : #define FSM_STATE_END break; #define Fsm. Change. State(cond, nextstate) if(cond) e. State. Cnt=nextstate #define Fsm. Next. State(nextstate) e. State. Cnt=nextstate #define Fsm. Succ. State() e. State. Cnt=e. State. Cnt + 1 #define Fsm. Prev. State() e. State. Cnt=e. State. Cnt - 1 Copyright Micron Engineering 2007 49
Micro. FSM under the hood What are the difference between a FSM implemented with normal switch() construct and Micro. FSM? Micro. FSM syntax give prominence to states and transitions Micro. FSM details are hidden on the function containing it Micro. Fsm flexible syntax permits to realize a function with FSM and mixed normal procedural code Micro. FSM has pre defined constructs able to solve common situations as suspend and restart its execution Micro. FSM has constructs able to handle pseudo states Micro. FSM execution can continue across 2 or more states until there is something to wait for Micro. FSM syntax masks a more efficient C code Micro. FSM can be expanded and adapted to a lof of situations Copyright Micron Engineering 2007 50
Micro. FSM under the hood What are pseudo states in UML? The UML state chart notation defines pseudo state the initial and last state symbols because they aren't real states but just a graphic sign evidencing 2 states. What are pseudo states in Micro. FSM? 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 Micro. FSM? It is a construct that evaluates a guard condition, if true it will continue the execution flow of the FSM code, if false it will exit from the FSM and next time the execution flow of the FSM will start from the blocking wait construct. Copyright Micron Engineering 2007 51
Micro. FSM under the hood What blocking wait constructs are provided in Micro. FSM? They are: Fsm. Wait. Until(condition) Fsm. Wait. While(condition) Fsm. Do. Wait. Until(exec, condition) Fsm. Do. Wait. While(exec, condition) Fsm. Suspend() Fsm. Suspend. Until(condition) and how are implemented blocking wait constructs? They are: based on local continuations inspired 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) Copyright Micron Engineering 2007 52
Micro. FSM under the hood This is the implementation of blocking wait constructs implemented in Micro. Fsm: page 1 #define Fsm. Wait. Until(condition) do { FSM_SET(e. State. Cnt); if(!(condition)) return(FSM_WAITING); } while(0) #define Fsm. Suspend() do { c. Suspend = 0; Fsm. Wait. Until(c. Suspend); } while(0) #define Fsm. Suspend. Until(cond) do { c. Suspend = 0; Fsm. Wait. Until(c. Suspend || !(cond)); c. Suspend = 1; } while(0) Copyright Micron Engineering 2007 53
Micro. FSM under the hood This is the implementation of blocking wait constructs implemented in Micro. Fsm: page 2 #define Fsm. Wait. While(condition) Fsm. Wait. Until(!(condition)) #define Fsm. Do. Wait. Until(exec, condition) do { FSM_SET(e. State. Cnt); exec; if(!(condition)) return(FSM_WAITING); } while(0) #define Fsm. Do. Wait. While(exec, condition) Fsm. Do. Wait. Until(!(condition)) Copyright Micron Engineering 2007 54
Micro. FSM under the hood All the magic is contained in a single row: #define FSM_SET(s) s = (int)__LINE__; case __LINE__: Copyright Micron Engineering 2007 55
Micro. FSM under the hood All 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 Micro. FSM because __LINE__ constant can't be equal with any state label that are defined inside FSM_STATES. Copyright Micron Engineering 2007 56
Micro. FSM under the hood All 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 Micro. FSM because __LINE__ constant can't be equal with any state label that are defined inside FSM_STATES. The following example. . . FSM_STATES ON=FIRST_STATE, OFF // states declaration FSM_BEGIN. . . after preprocessing will be expanded as: Copyright Micron Engineering 2007 57
Micro. FSM under the hood All 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 Micro. FSM because __LINE__ constant can't be equal with any state label that are defined inside FSM_STATES. The following example. . . FSM_STATES ON=FIRST_STATE, OFF // states declaration FSM_BEGIN. . . after preprocessing will be expanded as: static enum e. Fsm. State{ ON=-127, OFF }e. State. Cnt=-127; char c. Suspend=1; switch(e. State. Cnt) {. . . Copyright Micron Engineering 2007 58
Micro. FSM under the hood So notations for transitions and blocking wait constructs can be intermixed; what is the advantage? Using Micro. FSM 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 “building blocks” and convert them to C code, then use them as a set of templates. Copyright Micron Engineering 2007 59
Micro. FSM templates This is a sequence: . . . Fsm. Transition(guard, event(), B); . . . // or if(guard) { event(); Fsm. Next. State(B); }. . . // or if(guard) { event(); Fsm. Succ. State(); }. . . // or Fsm. Wait. Until(guard); event(); Copyright Micron Engineering 2007 60
Micro. FSM templates This is an iteration: while(g 1) { if(g 2) { e 2(); Fsm. Next. State(B); } e 1(); }. . . // or while(g 1) { Fsm. Wait. Until(!g 1 || g 2); Fsm. Transition(g 2, e 2(), B); } e 1(); } B is supposed to be the next state. Pay attention to choose g 1 to be sure that the only way to exit from the while loop is when g 2 is evaluated true. Copyright Micron Engineering 2007 61
Micro. FSM templates This is a selection: Fsm. Transition(g 1, e 1(), B); Fsm. Transition(g 2, e 2(), C); . . . // or Fsm. Wait. Until(g 1 || g 2); if(g 1) { Fsm. Next. State(B); e 1(); } else if(g 2) { Fsm. Next. State(C); e 2(); }. . . // or Fsm. Wait. Until(g 1 || g 2); Fsm. Change. State(g 1, B); Fsm. Change. State(g 2, C); . . . Copyright Micron Engineering 2007 62
Micro. FSM templates This is always a selection: Fsm. Transition(g 1, e 1(), B); Fsm. Transition(g 2, e 2(), C); . . . // or Fsm. Wait. Until(g 1 || g 2); if(g 1) { Fsm. Next. State(B); e 1(); } else if(g 2) { Fsm. Next. State(C); e 2(); }. . . // or Fsm. Wait. Until(g 1 || g 2); Fsm. Change. State(g 1, B); Fsm. Change. State(g 2, C); . . . Copyright Micron Engineering 2007 63
Micro. FSM templates Let's try to add a do action: FSM_STATE(A) c++; Fsm. Transition(guard, event, B); FSM_STATE_END. . . // or Fsm. Do. Wait. Until(c++, guard); event(); . . . if the action is more complex it may be a block of instructions or a function call for example: Fsm. Do. Wait. Until(b=Gain. Get(), b>10); Copyright Micron Engineering 2007 64
Micro. FSM templates Let's try to add an exit action: FSM_STATE(A). . . if(g 1 || g 2) ex(); Fsm. Transition(g 1, e 1(), B); Fsm. Transition(g 2, e 2(), C); FSM_STATE_END. . . // or Fsm. Wait. Until(g 1 || g 2); ex(); Fsm. Transition(g 1, e 1(), B); Fsm. Transition(g 2, e 2(), C); . . . Copyright Micron Engineering 2007 65
Micro. FSM templates Let's try to add an entry action: FSM_STATE(Aentry) entry(); Fsm. Succ. State(); FSM_STATE(A) Fsm. Transition(g 1, e 1, B); FSM_STATE_END. . . // or entry(); Fsm. Wait. Until(g 1); e 1(); . . . Copyright Micron Engineering 2007 66
Micro. FSM examples 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: Copyright Micron Engineering 2007 67
Micro. FSM examples 1: Fsm. Declare(Light. Fsm(char off, int gpo)) { 2: FSM_STATES ONentry=FIRST_STATE, ON, OFFentry, OFF // states decl. 3: FSM_BEGIN 4: FSM_STATE(ONentry) // this is entry ON 5: lamp. on(); // entry action 6: Fsm. Succ. State(); 7: FSM_STATE(ON) 8: if(off) { // transition guard test 9: printf(“exiting ON”); // exit action 10: Fsm. Succ. State(); // change state 11: printf(“to OFF”); // transition action 12: } 13: FSM_STATE_END // end state 14: FSM_STATE(OFFentry) 15: lamp. off(); // entry action 16: Fsm. Succ. State(); 17: FSM_STATE(OFF) 18: while(off && !gpo) { // stay here conditions 19: if(off || gpo) // transitions guards cond. 20: printf(“exiting OFF”); // exit action 21: if(gpo) Fsm. Exit(); // test final state guard 22: else if(off) printf(“needless”); // auto transition. . 23: } //. . . guard test 24: FSM_STATE_END 25: FSM_END 26: } Copyright Micron Engineering 2007 68
Micro. FSM examples 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. Fsm. Declare(Light. Fsm(char off)) is the C function prototype specialized for FSMs, it is important to say that the function may have an arbitrary number of parameters without requiring any change to te macro. So I may write: FSM_DECLARE(Modem. Fsm(char input)) or: FSM_DECLARE(Modem. Fsm(char input, int timer)) as required from the specific application. Copyright Micron Engineering 2007 69
Micro. FSM examples All states are listed on a specific line: FSM_STATES ON=FIRST_STATE, OFF the first state must be initialized with the required label FIRST_STATE and variable declarations must came before this line. FSM_BEGIN Identifies FSM starting point; no instruction can be interposed between FSM_STATES and FSM_BEGIN, FSM must end with FSM_END. FSM_STATE(ON) Identifies ON state entry point as reported in the UML state chart. Fsm. Succ. State() Implements the transition between ON and OFF states. Copyright Micron Engineering 2007 70
Micro. FSM examples This is an alternative solution using pseudo states constructs: 1: Fsm. Declare(Light. Fsm(char off, int gpo)) { 2: FSM_STATES ONentry=FIRST_STATE, ON, OFFentry, OFF // states decl. 3: FSM_BEGIN 4: FSM_STATE(ON) // this is state ON 5: lamp. on(); // entry action 6: Fsm. Wait. Until(off); // transition guard test 7: printf(“exiting ON”); // exit action 8: printf(“to OFF”); // transition action 9: // state OFF 10: lamp. off(); // entry action 11: while(off && !gpo) { // stay here conditions 12: Fsm. Wait. Until(off || gpo); // transitions guards cond. 13: printf(“exiting OFF”); // exit action 14: if(gpo) // test final state guard 15: Fsm. Exit(); 15: else { 16: if(off) //. . . guard test 17: printf(“needless”); // auto transition. . 18: } 19: } 20: FSM_STATE_END 21: FSM_END 22: } Copyright Micron Engineering 2007 71
Micro. FSM examples Fsm. Wait. Until(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 Fsm. Wait. Until(. . . ) row. Fsm. Exit() 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. Copyright Micron Engineering 2007 72
Micro. FSM templates This is a template useful to convert a state in a pseudo state when possible. At the end there is the complete code of the FSM template. FSM_STATE(State). . FSM_STATE_END Pseudo states have no label. In next row C is a pseudo state: //T B->C = Fsm. Wait. Until(n <= 3); // T C->A Fsm. Wait. Until(k!=0); //C entry point Copyright Micron Engineering 2007 73
Micro. FSM templates FSM_STATES A=FIRST_STATE, B // C is a pseudo-state so doesn't need to be listed. // T A->B = Fsm. Change. State(Is. Bit. True(GUARD), B); . . // T B->A = Fsm. Change. State(n<3, A); //T B->C = Fsm. Wait. Until(n > 3); // T C->A Fsm. Wait. Until(k==0); Copyright Micron Engineering 2007 74
Micro. FSM templates 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: #define BYTE_OF(a, b) (a) #define BIT_OF(a, b) (1 << (b)) #define Is. Bit. True(a) istrue(BYTE_OF(a), BIT_OF(a)) static BYTE s_b. Status; #define GUARD s_b. Status, 6 Fsm. Declare(Template. Fsm(BYTE bbyte, BYTE bbit, int k)) { FSM_STATES A=FIRST_STATE, B // states decl. FSM_BEGIN FSM_STATE(A) // this is entry ON Fsm. Change. State(Is. Bit. True(GUARD), B); FSM_STATE_END FSM_STATE(B) Fsm. Wait. While(n==3); // transition guard test Fsm. Change. State(n<3, A); // exit action // pseudo state C Fsm. Wait. Until(k==0); Fsm. Change. State(1, A); FSM_STATE_END // end state FSM_END } void main(void) { int n, r; for(n=5; n>=-5; n--) r = Template. Fsm(GUARD, n); } Copyright Micron Engineering 2007 75