Você está na página 1de 5

Pseudo code for SPI state machine

*********************************************************
STATES
SPI_GameStatus
SPI_Kart1
SPI_Kart2
SPI_Kart3
*********************************************************

*********************************************************
POSSIBLE EVENTS
ES_TIMEOUT
ES_QUERYDRS
ES_ENTRY
ES_EXIT
*********************************************************

*********************************************************
TIMERS
TIMER_SSInterval
*********************************************************
*********************************************************
MODULE VARIABLES
CurrentState
GameStatus
Kart1Status
Kart2Status
Kart3Status
*********************************************************
*********************************************************
PUBLIC FUNCTIONS
StartSPISM
RunSPISM
QueryGameStatus
QueryKart1Status
QueryKart2Status
QueryKart3Status
*********************************************************
*********************************************************
PRIVATE FUNCTIONS
DuringSPIGameStatus
DuringSPIKart1

DuringSPIKart2
DuringSPIKart3
*********************************************************
/////////////////////////////////////////////////////////
PUBLIC FUNCTION StartSPISM
Receive: ES_Event ES_ENTRY
Return: No return
Initialize the status of GameStatus, Kart1Status, Kart2STatus and Kart3Status
Set CurrentState to SPI_GameStatus
Call RunSPISM with ES_ENTRY to do entry functions
Set CurrentState to SPI_GameStatus
ENDStartSPISM
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PUBLIC FUNCTION RunSPISM
Receive CurrentEvent: ES_Event: ES_ENTRY, ES_EXIT, ES_TIMEOUT and ES_QUERYDRS
Return: ES_Event: ES_EXIT, ES_ENTRY
Local Variable: SPIState: NextState
ES_Event ReturnEvent
boolean MakeTransition
switch CurrentState
if CurrentState is SPI_GameStatus
Call DuringSPIGameStatus
if there is still an active event
switch CurrentEvent
if CurrentEvent is ES_TIMEOUT
set MakeTransition to true
set NextState to SPI_Kart1
endif
endswitch
endif
endif
if CurrentState is SPI_Kart1
Call DuringSPIKart1 to let lower level state machine deal with CurrentEv
ent
if there is still an active event
switch CurrentEvent
if CurrentEvent is ES_TIMEOUT
set MakeTransition to true
set NextState to SPI_Kart2
endif
endswitch
endif
endif
if CurrentState is SPI_Kart2
Call DuringSPIKart2 to let lower level state machine deal with CurrentEv
ent
if there is still an active event
switch CurrentEvent
if CurrentEvent is ES_TIMEOUT
set MakeTransition to true
set NextState to SPI_Kart2

endif
endswitch
endif
endif
if CurrentState is SPI_Kart3
Call DuringSPIKart3 to let lower level state machine deal with CurrentEv
ent
if there is still an active event
switch CurrentEvent
if CurrentEvent is ES_TIMEOUT
set MakeTransition to true
set NextState to SPI_GameStatus
endif
endswitch
endif
endif
if MakeTransition is true
Execute exit function for current state
Set CurrentState to NextState
Execute entry function for new state
endif
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PRIVATE FUNCTION DuringSPIGameStatus
Receive: ES_Event CurrentEvent
Return: ES_Event
if CurrentEvent is ES_ENTRY
set SS line to low to activate the DRS slave
do the data transfer for GameStatus (0x3F) and update the GameStatus
Start TIMER_SSInterval (4ms)
endif
else if CurrentEvent is ES_EXIT
update GameStatus
endif
else
endif
return CurrentEvent
endDuringSPIGameStatus
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PRIVATE FUNCTION DuringSPIKart1
Receive: ES_Event CurrentEvent
Return: ES_Event
if CurrentEvent is ES_ENTRY
set SS line to low to activate the DRS slave
do the data transfer for Kart1Status (0xC3) and update the Kart1Status
Start TIMER_SSInterval (4ms)
endif
else if CurrentEvent is ES_EXIT
update Kart1 Status
endif
else
endif

return CurrentEvent
endDuringSPIKart1
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PRIVATE FUNCTION DuringSPIKart2
Receive: ES_Event CurrentEvent
Return: ES_Event
if CurrentEvent is ES_ENTRY
set SS line to low to activate the DRS slave
do the data transfer for Kart2Status (0x5A) and update the Kart2Status
set SS line to high
Start TIMER_SSInterval (4ms)
endif
else if CurrentEvent is ES_EXIT
update Kart2 Status
endif
else
endif
return CurrentEvent
endDuringSPIKart2
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PRIVATE FUNCTION DuringSPIKart3
Receive: ES_Event CurrentEvent
Return: ES_Event
if CurrentEvent is ES_ENTRY
set SS line to low to activate the DRS slave
do the data transfer for Kart3Status (0x7E) and update the Kart3Status
set SS line to high
Start TIMER_SSInterval (4ms)
endif
else if CurrentEvent is ES_EXIT
update Kart3 Status
endif
else
endif
return CurrentEvent
endDuringSPIKart1
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PUBLIC FUNCTION QueryGameStatus
Receive: No arguments
Return: Structure GameStatus
return GameStatus
endQueryGameStatus
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PUBLIC FUNCTION QueryKart1Status
Receive: No arguments
Return: Structure Kart1Status
return Kart1Status

endQueryKart2Status
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PUBLIC FUNCTION QueryKart2Status
Receive: No arguments
Return: Structure Kart2Status
return Kart2Status
endQueryKart2Status
/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////
PUBLIC FUNCTION QueryKart3Status
Receive: No arguments
Return: Structure Kart3Status
return Kart3Status
endQueryKart3Status
/////////////////////////////////////////////////////////

Você também pode gostar