You are on page 1of 3

Pseudo-code for the SPI_Follower module

Data private to the module: MyPriority, CurrentState, GoodToShoot

Enum spi_comms

Functions private to the module: SPIReceiveInterrupt

InitSPIFollowerService
Takes a priority number, returns True.

Sets MyPriority

Disable SPI interrupt flags

Initialization Sequence for SPI1 through BasicConfig


Map SCK1 to RB14
Disable analog function on SPI pins
Set SPI as follower
Disable SPI
Map SDO1 to RA1
Map SS to RA0
Map SDI1 to RB11

Clear receive buffer


Set ENHBUF bit to false
Clear SPI interrupt flags
Set interrupt priority for SPI
Set interrupt when receive buffer full
Enable SPI interrupts
Clear SPIROV (Receive overflow flag bit)
Set BRG to get SCK frequency of 78.13 kHz (random choice)
Set Clock Idle State high
Set active edge to latch on second edge
Set transfer width to 8 bits

Enable Interrupts
Turn SPI on

Post Event ES_Init to this service

Set CurrentState to InitPState

End of InitSPIFollowerService

PostSPIFollowerService
Takes an ES_Event_t, returns boolean outcome of ES_PostToService

Call ES_PostToService with MyPriority and ThisEvent

End of PostSPIFollowerService

RunSPIFollowerService
The EventType field of ThisEvent will be one of: ES_INIT,
TEAM_A_SELECTED, TEAM_B_SELECTED, AR1_ALIGN, NOT_ALIGNED

Set ReturnEvent to ES_NO_EVENT

CurrentState is:
InitPState
ThisEvent is ES_INIT
Set CurrentState to Idle

Idle
ThisEvent is TEAM_A_SELECTED
Set GoodToShoot to true

ThisEvent is TEAM_B_SELECTED
Set GoodToShoot to true

ThisEvent is AR1_ALIGN
Set GoodToShoot to true

ThisEvent is NOT_ALIGNED
Set GoodToShoot to false

Return ES_NO_EVENT
End of RunSPIFollowerService

__ISR(_SPI_1_VECTOR, IPL7SOFT) SPIReceiveInterrupt


Takes _SPI_1_VECTOR, IPL7SOFT as parameter. Returns nothing.

Create NewEvent variable

Store SPI1BUF in LeaderRequest variable

Clear SPI1RXIF flag

if LeaderRequest is ALIGN_QUERY
Set SPI1BUF to GoodToShoot
Else if LeaderRequest is CLOCK_BITS
Do nothing
else if LeaderRequest is LAUNCH_OFF
Set NewEvent to ES_LAUNCH_OFF
Post to Launcher Service
else if LeaderRequest is ES_LAUNCH_20
Set NewEvent to ES_LAUNCH_20
Post to Launcher Service
else if LeaderRequest is ES_LAUNCH_40
Set NewEvent to ES_LAUNCH_40
Post to Launcher Service
else if LeaderRequest is ES_LAUNCH_60
Set NewEvent to ES_LAUNCH_60
Post to Launcher Service
else if LeaderRequest is ES_LAUNCH_80
Set NewEvent to ES_LAUNCH_80
Post to Launcher Service
else if LeaderRequest is ES_LAUNCH_100
Set NewEvent to ES_LAUNCH_100
Post to Launcher Service
Else
Do nothing

End of SPIReceiveInterrupt

You might also like