===============================================================================
JSR MODULE
This module provides JSR SPI communication functionality for Sir Robot.
===============================================================================
PRIVATE FUNCTIONS
static ES_Event DuringWaiting( ES_Event Event);
static ES_Event DuringStatusCommandByte( ES_Event Event);
static ES_Event DuringReadByte( ES_Event Event);
static ES_Event DuringTransferByte( ES_Event Event);
static ES_Event DuringScoreCommandByte( ES_Event Event);
static void InitSPI(void);
static unsigned char readJSR(void);
static void MapCommandToEvent(unsigned char);
PUBLIC FUNCTIONS
unsigned char RedKnightScore(void);
unsigned char DarkKnightScore(void);
MODULE VARIABLES
static JSRState_t CurrentState;
static unsigned char receivedByte;
static bool ReadSuccessful;// = false;
static bool TransferSuccessful;// = false;
static unsigned char darkKnightScore;
static unsigned char redKnightScore;
static int knight;
static bool reloadAllowed;
===============================================================================
FUNCTION InitJSR
Read dark/red knight toggle switch
Set knight variable
Call InitSPI()
===============================================================================
FUNCTION RunJSR
Switch on CurrentState
case if ST_Waiting
call during function for this state
if QUERY_TIMER timesout
Set NextState to ST_PausingForSS
Start TRANSFER_TIMER
Set transition flag
case if ST_PausingForSS
if QUERY_TIMER timesout
Set next state to SendingByte1
Set transition flag
case if ST_SendingByte1
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte2
Set transition flag
case if ST_SendingByte2
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to ST_SendingByte3
Set transition flag
case if ST_SendingByte3
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte4
Set transition flag
case if ST_SendingByte4
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte5
mark that we are taking a transition
case if ST_ReadByte4
call during function for this state
if event is a timeout
mark that we are taking a transition
if read was successful
reset transfer success flag
Set next state to SendingByte5
Remap the event to the received game status
case if ST_SendingByte5
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte6
mark that we are taking a transition
case if ST_SendingByte6 :
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte7
mark that we are taking a transition
case if ST_SendingByte7 :
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte8
mark that we are taking a transition
case if ST_SendingByte8
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
update red knight score
Set next state to ReadByte8
mark that we are taking a transition
case if ST_ReadByte8
call during function for this state
if event is a timeout
if read was successful
reset transfer success flag
update dark knight score
raise SS line
transition to waiting state
else
make an external self transition
end switch on current state
If we are making a state transition
Execute exit function for current state
Modify state variable
Execute entry function for new state
===============================================================================
FUNCTION StartJSR
Set entry state to ST_Waiting
Initialize transfer and read success flags
call the entry function for the entry state
===============================================================================
FUNCTION sendCommand
Read SPISR with SPTEF Hi
Write to SPIDR
===============================================================================
FUNCTION readJSR
Read SPISR with SPIF Hi,
Read SPIDR
===============================================================================
FUNCTION DuringWaiting
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
Start Query Timer
else if ES_EXIT
start transfer timer
===============================================================================
FUNCTION DuringStatusCommandByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
send status command
start transfer timer
===============================================================================
FUNCTION DuringScoreCommandByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
send score query command
start transfer timer
===============================================================================
FUNCTION DuringTransferByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
if byte has not been read
read byte
else
send waiting command
if current state is ST_SendingByte8
set red knight score variable
if transfer successful
reset read success flag
start transfer timer
===============================================================================
FUNCTION DuringReadByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
read last byte of this query
start Transfer timer
===============================================================================
FUNCTION InitSPI
Set to Master (SPICR1)
SPI Enable (SPICR1)
MSB First (SPICR1)
Set Clock Polarity (SPICR1)
Set Clock Phase (SPICR1)
Set to Mode 3 - SSOE LO, MODFEN HI
Set SPI Bit Rate - Divisor of 32
JSR MODULE
This module provides JSR SPI communication functionality for Sir Robot.
===============================================================================
PRIVATE FUNCTIONS
static ES_Event DuringWaiting( ES_Event Event);
static ES_Event DuringStatusCommandByte( ES_Event Event);
static ES_Event DuringReadByte( ES_Event Event);
static ES_Event DuringTransferByte( ES_Event Event);
static ES_Event DuringScoreCommandByte( ES_Event Event);
static void InitSPI(void);
static unsigned char readJSR(void);
static void MapCommandToEvent(unsigned char);
PUBLIC FUNCTIONS
unsigned char RedKnightScore(void);
unsigned char DarkKnightScore(void);
MODULE VARIABLES
static JSRState_t CurrentState;
static unsigned char receivedByte;
static bool ReadSuccessful;// = false;
static bool TransferSuccessful;// = false;
static unsigned char darkKnightScore;
static unsigned char redKnightScore;
static int knight;
static bool reloadAllowed;
===============================================================================
FUNCTION InitJSR
Read dark/red knight toggle switch
Set knight variable
Call InitSPI()
===============================================================================
FUNCTION RunJSR
Switch on CurrentState
case if ST_Waiting
call during function for this state
if QUERY_TIMER timesout
Set NextState to ST_PausingForSS
Start TRANSFER_TIMER
Set transition flag
case if ST_PausingForSS
if QUERY_TIMER timesout
Set next state to SendingByte1
Set transition flag
case if ST_SendingByte1
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte2
Set transition flag
case if ST_SendingByte2
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to ST_SendingByte3
Set transition flag
case if ST_SendingByte3
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte4
Set transition flag
case if ST_SendingByte4
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte5
mark that we are taking a transition
case if ST_ReadByte4
call during function for this state
if event is a timeout
mark that we are taking a transition
if read was successful
reset transfer success flag
Set next state to SendingByte5
Remap the event to the received game status
case if ST_SendingByte5
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte6
mark that we are taking a transition
case if ST_SendingByte6 :
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte7
mark that we are taking a transition
case if ST_SendingByte7 :
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
Set next state to SendingByte8
mark that we are taking a transition
case if ST_SendingByte8
call during function for this state
if event is a timeout
if transfer was successful
reset transfer success flag
update red knight score
Set next state to ReadByte8
mark that we are taking a transition
case if ST_ReadByte8
call during function for this state
if event is a timeout
if read was successful
reset transfer success flag
update dark knight score
raise SS line
transition to waiting state
else
make an external self transition
end switch on current state
If we are making a state transition
Execute exit function for current state
Modify state variable
Execute entry function for new state
===============================================================================
FUNCTION StartJSR
Set entry state to ST_Waiting
Initialize transfer and read success flags
call the entry function for the entry state
===============================================================================
FUNCTION sendCommand
Read SPISR with SPTEF Hi
Write to SPIDR
===============================================================================
FUNCTION readJSR
Read SPISR with SPIF Hi,
Read SPIDR
===============================================================================
FUNCTION DuringWaiting
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
Start Query Timer
else if ES_EXIT
start transfer timer
===============================================================================
FUNCTION DuringStatusCommandByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
send status command
start transfer timer
===============================================================================
FUNCTION DuringScoreCommandByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
send score query command
start transfer timer
===============================================================================
FUNCTION DuringTransferByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
if byte has not been read
read byte
else
send waiting command
if current state is ST_SendingByte8
set red knight score variable
if transfer successful
reset read success flag
start transfer timer
===============================================================================
FUNCTION DuringReadByte
process ES_ENTRY and ES_EXIT events
if ES_ENTRY
read last byte of this query
start Transfer timer
===============================================================================
FUNCTION InitSPI
Set to Master (SPICR1)
SPI Enable (SPICR1)
MSB First (SPICR1)
Set Clock Polarity (SPICR1)
Set Clock Phase (SPICR1)
Set to Mode 3 - SSOE LO, MODFEN HI
Set SPI Bit Rate - Divisor of 32