Vous êtes sur la page 1sur 4

Module level functions:

DuringWait2Send
DuringSPISendByte
InitHW4SPI
ReadFromREF
WriteToREF
DecodeGameState
PrintCurrentCommand

Module level variables:


CurrentState, MyPriority, QueryCommand, CurrentGameState, LastEvent

InitREFCommSM
init MyPriority using the passed-in parameter
init hardware for SPI

Start the Master State machine


End of InitREFCommSM

PostREFCommSM
Post an event to service with saved priority
End of PostREFCommSM

RunREFCommSM
set MakeTransition to false
Set NextState to CurrentState;
default to normal entry to new state
assume no error

If current state is Wiat2Send


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

process any events


If an event is active
If event is ES_TIMEOUT
Execute action function for Wait2Send: ES_TIMEOUT
Decide the next state to be SPISendByte
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
end of this block

end of this block

If current state is SPISendByte


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

process any events


If an event is active
If event is SSI_EOT
Execute action function for SPISendByte: SSI_EOT
decode CurrentGameState and save the return event in
CurrentEvent
if CurrentEvent is different from LastEvent
post CurrentEvent to GameMasterSM
update LastEvent to CurrentEvent
end if
set NextState to Wait2Send
for internal transitions, skip changing MakeTransition
mark that we are taking a transition
if transitioning to a state with history change kind of entry
end of this block

end of this block

If we are making a state transition


Execute exit function for current state
Modify state variable
Execute entry function for new state
end if

in the absence of an error the top level state machine should


always return ES_NO_EVENT, which we initialized at the top of func
End of RunREFCommSM

StartREFCommSM
if there is more than 1 state to the top level machine you will need
to initialize the state variable
init CurrentState to 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
End of StartREFCommSM

EOTISR
Disable interrupt
read received bytes from REF
post SSI_EOT event to REFCommSM to handle it
End of EOTISR

/***************************************************************************
private functions
***************************************************************************/

DuringWait2Send
assme no re-mapping or comsumption

process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events


if ES_ENTRY
implement any entry actions required for this state machine
init QUERY_TIMER, the timer will time out after QUERY_TIME
after that start any lower level machines that run in this state
else if ES_EXIT
on exit, give the lower levels a chance to clean up first
repeat for any concurrently running state machines
now do any local exit functionality
write QueryCommand to register
enable interrupt
else
do the 'during' function for this state
run any lower level state machine

return either Event, if you don't want to allow the lower level machine
to remap the current event, or ReturnEvent if you do want to allow it.
End of DuringWait2Send

DuringSPISendByte
assme no re-mapping or comsumption

process ES_ENTRY, ES_ENTRY_HISTORY & ES_EXIT events


if ES_ENTRY
implement any entry actions required for this state machine
after that start any lower level machines that run in this state
else if ES_EXIT
on exit, give the lower levels a chance to clean up first
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

return either Event, if you don't want to allow the lower level machine
to remap the current event, or ReturnEvent if you do want to allow it.
End of DuringSPISendByte

InitHW4SPI
Enable the clock to the GPIO port (PA 2-5)
Enable the clock to SSI module (SSI Module 0 Run Mode Clock)
Wait for the GPIO port to be ready
Program the GPIO to use the alternate functions on the SSI pins
Set mux position in GPIOPCTL to select the SSI use of the pins
Program the Port A lines for digital I/O
If using SPI mode 3, program the pull-up on the clock line (PA2)
Wait for the SSI0 to be ready
Make sure that the SSI is disabled before programming mode bits
Select master mode (MS) & TXRIS indicating End of Transmit (EOT)
Configure the SSI clock source to the system clock
Configure the clock pre-scaler to get 14KHz frequency (max: 961kHz)
Configure clock rate (SCR), phase & polarity (SPH, SPO), mode (FRF), data
size (DSS)
freescale SPI Frame Format
data size is 8 bits
Locally enable interrupts (TXIM in SSIIM)
Make sure that the SSI is enabled for operation
Enable globally
End of InitHW4SPI

ReadFromREF
read four bytes from SSI data register into CurrentGameState[4]
End of ReadFromREF
WriteToREF
write four bytes in QueryCommand into SSI data register
End of WriteToREF

DecodeGameState
create a new event with event type as ES_NO_EVENT

grab the third byte, save it into a varaible named SS


grab the second byte, save it into a variable named SC

if bits0-2 are 0x0, set event type to EV_WAIT2START

if bits0-2 are 0x1, set event type to EV_FACEOFF

if bits0-2 are 0x3, set event type to EV_FACEOFF

if bits0-2 are 0x4, set event type to EV_GAMEOVER

if bits0-2 are 0x2


calculate shot clock time using SC
assign event parameter to half of shot clock time
if bits45 are 0x10, set event type to EV_REDPO
if bits45 are 0x20, set event type to EV_BLUEPO
if bits45 are 0x0, set event type to EV_NOPO
end if

return the event


End of DecodeGameState

Vous aimerez peut-être aussi