Refcommsm
Refcommsm
Module
REFCommSM.c
Revision
2.0.1
Description
This is a template for the top level Hierarchical state machine
Notes
History
When Who What/Why
-------------- --- --------
02/20/17 14:30 jec updated to remove sample of consuming an event. We
always want to return ES_NO_EVENT at the top level
unless there is a non-recoverable error at the
framework level
02/03/16 15:27 jec updated comments to reflect small changes made in '14 & '15
converted unsigned char to bool where appropriate
spelling changes on true (was True) to match standard
removed local var used for debugger visibility in 'C32
removed Microwave specific code and replaced with generic
02/08/12 01:39 jec converted from MW_MasterMachine.c
02/06/12 22:02 jec converted to Gen 2 Events and Services Framework
02/13/10 11:54 jec converted During functions to return Event_t
so that they match the template
02/21/07 17:04 jec converted to pass Event_t to Start...()
02/20/07 21:37 jec converted to use enumerated type for events
02/21/05 15:03 jec Began Coding
****************************************************************************/
/*----------------------------- Include Files -----------------------------*/
/* include header files for this state machine as well as any machines at the
next lower level in the hierarchy that are sub-machines to this machine
*/
#include "ES_Configure.h"
#include "ES_Framework.h"
#include "inc/hw_types.h"
#include "inc/hw_nvic.h"
#include "inc/hw_gpio.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ssi.h"
#define BitsPerNibble 4
#define CPSDVSR 50
#define SCR 55
//protocols defines
#define BIT02MASK 0x7 //0b111
#define BIT45MASK 0x30 //0b00110000
#define BIT2MASK 0x4 //0b100
#define W4S 0x0
#define FO 0x1 //0b001
#define PL 0x2 //0b010
#define TIE 0x3 //0b011
#define GO 0x4 //0b100
#define NOPO 0x00
#define REDPO 0x10 //0b00010000
#define BLUEPO 0x20 //0b00100000
Parameters
uint8_t : the priorty of this service
Returns
boolean, False if error in initialization, True otherwise
Description
Saves away the priority, and starts
the top level state machine
Notes
Author
J. Edward Carryer, 02/06/12, 22:06
****************************************************************************/
bool InitREFCommSM ( uint8_t Priority )
{
ES_Event_t ThisEvent;
ThisEvent.EventType = ES_ENTRY;
// Start the Master State machine
StartREFCommSM( ThisEvent );
return true;
}
/****************************************************************************
Function
PostMasterSM
Parameters
ES_Event ThisEvent , the event to post to the queue
Returns
boolean False if the post operation failed, True otherwise
Description
Posts an event to this state machine's queue
Notes
Author
J. Edward Carryer, 10/23/11, 19:25
****************************************************************************/
bool PostREFCommSM( ES_Event_t ThisEvent )
{
return ES_PostToService( MyPriority, ThisEvent);
}
/****************************************************************************
Function
RunMasterSM
Parameters
ES_Event: the event to process
Returns
ES_Event: an event to return
Description
the run function for the top level state machine
Notes
uses nested switch/case to implement the machine.
Author
J. Edward Carryer, 02/06/12, 22:09
****************************************************************************/
ES_Event_t RunREFCommSM( ES_Event_t CurrentEvent )
{
bool MakeTransition = false;/* are we making a state transition? */
REFCommState_t NextState = CurrentState;
ES_Event_t EntryEventKind = { ES_ENTRY, 0 };// default to normal entry to new
state
ES_Event_t ReturnEvent = { ES_NO_EVENT, 0 }; // assume no error
switch ( CurrentState )
{
case Wait2Send : // If current state is state one
// Execute During function for state one. ES_ENTRY & ES_EXIT are
// processed here allow the lowere level state machines to re-map
// or consume the event
CurrentEvent = DuringWait2Send(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
switch (CurrentEvent.EventType)
{
case ES_TIMEOUT : //If event is ES_TIMEOUT
// Execute action function for Wait2Send: ES_TIMEOUT
NextState = SPISendByte;//Decide the next state to be SPISendByte
// for internal transitions, skip changing MakeTransition
MakeTransition = true; //mark that we are taking a transition
// if transitioning to a state with history change kind of entry
EntryEventKind.EventType = ES_ENTRY_HISTORY;
break;
// repeat cases as required for relevant events
default:;
}
}
break;
// repeat state pattern as required for other states
case SPISendByte : // If current state is state one
// Execute During function for state one. ES_ENTRY & ES_EXIT are
// processed here allow the lowere level state machines to re-map
// or consume the event
CurrentEvent = DuringSPISendByte(CurrentEvent);
//process any events
if ( CurrentEvent.EventType != ES_NO_EVENT ) //If an event is active
{
switch (CurrentEvent.EventType)
{
case SSI_EOT : //If event is SSI_EOT
// Execute action function for SPISendByte: SSI_EOT
//decode CurrentGameState and save the return event in CurrentEvent
CurrentEvent = DecodeGameState(CurrentGameState);
//if CurrentEvent is different from LastEvent
if(CurrentEvent.EventType != LastEvent.EventType){
//post CurrentEvent to GameMasterSM
PostGameMasterSM(CurrentEvent);
//update LastEvent to CurrentEvent
LastEvent.EventType = CurrentEvent.EventType;
}
//set NextState to Wait2Send
NextState = Wait2Send;//Decide what the next state will be
// for internal transitions, skip changing MakeTransition
MakeTransition = true; //mark that we are taking a transition
// if transitioning to a state with history change kind of entry
EntryEventKind.EventType = ES_ENTRY_HISTORY;
break;
default:
;
// repeat cases as required for relevant events
}
}
break;
}
// If we are making a state transition
if (MakeTransition == true)
{
//Execute exit function for current state
CurrentEvent.EventType = ES_EXIT;
RunREFCommSM(CurrentEvent);
Parameters
ES_Event CurrentEvent
Returns
nothing
Description
Does any required initialization for this state machine
Notes
Author
J. Edward Carryer, 02/06/12, 22:15
****************************************************************************/
void StartREFCommSM ( ES_Event_t CurrentEvent )
{
// if there is more than 1 state to the top level machine you will need
// to initialize the state variable
//init CurrentState to Wait2Send
CurrentState = Wait2Send;
// now we need to let the Run function init the lower level state machines
// use LocalEvent to keep the compiler from complaining about unused var
RunREFCommSM(CurrentEvent);
return;
}
/***************************************************************************
private functions
***************************************************************************/
// after that start any lower level machines that run in this state
//StartLowerLevelSM( Event );
// repeat the StartxxxSM() functions for concurrent state machines
// on the lower level
}
else if ( Event.EventType == ES_EXIT )
{
// on exit, give the lower levels a chance to clean up first
//RunLowerLevelSM(Event);
// repeat for any concurrently running state machines
// now do any local exit functionality
}else
// do the 'during' function for this state
{
// run any lower level state machine
// ReturnEvent = RunLowerLevelSM(Event);
/*******************************************************************************
************************
* My private functions
********************************************************************************
***********************/