Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Pseudo-code for the SPI_Leader module

Data private to the module: MyPriority, CurrentState, query_sent,


query_requested, peripheral_request, FOLLOWER_PIN, PERIPHERAL_PIN

Enum spi_comms

Functions private to the module: SPIReceiveInterrupt, PIC_to_Send_to

InitSPILeaderService
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 leader
Disable SPI
Map SDO1 to RA1
Set SS lines for peripheral and follower to digital outputs
Raise both SS lines high
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 InitSPILeaderService
PostSPILeaderService
Takes an ES_Event_t, returns boolean outcome of ES_PostToService

Call ES_PostToService with MyPriority and ThisEvent

End of PostSPILeaderService

PIC_to_Send_to
Given a PIC pin, return nothing
if FOLLOWER_PIN
lower the follower SS pin
Set peripheral_request to false
else
lower the peripheral pin
Set peripheral_request to true

RunSPILeaderService
The EventType field of ThisEvent will be one of: ES_INIT,
GAME_STARTED, ES_ALIGN_QUERY, ES_FIRE, ES_INDEX_OPEN, ES_INDEX_CLOSE,
ES_FIRE_QUERY, ES_LIMIT_QUERY, GE_FOLLOWER_RESPONSE,
GET_PERIPHERAL_RESPONSE, ES_LAUNCH_OFF, ES_LAUNCH_20, ES_LAUNCH_40,
ES_LAUNCH_60, ES_LAUNCH_80, ES_LAUNCH_100, GAME_ENDED

Set ReturnEvent to ES_NO_EVENT

CurrentState is:
InitPState
ThisEvent is ES_INIT
Set CurrentState to Idle

Idle
ThisEvent is GAME_STARTED
Set PIC_to_Send_to to PERIPHERAL_PIN
Write GAME_START to SPI1BUF

ThisEvent is ES_ALIGN_QUERY
Set PIC_to_Send_to to FOLLOWER_PIN
Set query_sent to true
Write ALIGN_QUERY to SPI1BUF

ThisEvent is ES_FIRE
Set PIC_to_Send_to to PERIPHERAL_PIN
Write FIRE to SPI1BUF
ThisEvent is ES_INDEX_OPEN
Set PIC_to_Send_to to PERIPHERAL_PIN
Write INDEX_OPEN to SPI1BUF

ThisEvent is ES_INDEX_CLOSE
Set PIC_to_Send_to to PERIPHERAL_PIN
Write INDEX_CLOSE to SPI1BUF

ThisEvent is ES_FIRE_QUERY:
Set PIC_to_Send_to to FOLLOWER_PIN
Set query_sent to true
Write FIRE_QUERY to SPI1BUF

ThisEvent is ES_LIMIT_QUERY:
Set PIC_to_Send_to to PERIPHERAL_PIN
Set query_sent to true
Write LIMIT_QUERY to SPI1BUF

ThisEvent is GET_FOLLOWER_RESPONSE:
Set PIC_to_Send_to to FOLLOWER_PIN
Write CLOCK_BITS to SPI1BUF

ThisEvent is GET_PERIPHERAL_RESPONSE:
Set PIC_to_Send_to to PERIPHERAL_PIN
Write CLOCK_BITS to SPI1BUF

ThisEvent is ES_LAUNCH_OFF:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_OFF to SPI1BUF

ThisEvent is ES_LAUNCH_20:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_20 to SPI1BUF

ThisEvent is ES_LAUNCH_40:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_40 to SPI1BUF

ThisEvent is ES_LAUNCH_60:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_60 to SPI1BUF

ThisEvent is ES_LAUNCH_80:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_80 to SPI1BUF
ThisEvent is ES_LAUNCH_100:
Set PIC_to_Send_to to FOLLOWER_PIN
Write LAUNCH_100 to SPI1BUF

ThisEvent is GAME_ENDED:
Set PIC_to_Send_to to PERIPHERAL_PIN
Write GAME_END to SPI1BUF

Return ES_NO_EVENT
End of RunSPILeaderService

__ISR(_SPI_1_VECTOR, IPL7SOFT) SPIReceiveInterrupt


Takes _SPI_1_VECTOR, IPL7SOFT as parameter. Returns nothing.

Create NewEvent variable

Set both follower and peripheral SS lines high

Store SPI1BUF in FollowerResponse variable

Clear SPI1RXIF flag

if query_requested
if not a peripheral_request and the response is that robot is
aligned
Set NewEvent to AR1_ALIGN
Post AR1_ALIGN to LineFollowingSM
else if not peripheral_request
Set NewEvent to NOT_ALIGNED
Post NOT_ALIGNED to LineFollowingSM
else if peripheral_request and the response is that limit
switch is pressed
Set NewEvent to LIMIT_CHANGE
Set NewEvent.EventParam to 1
else if peripheral_request and the response is that limit
switch is unpressed
Set NewEvent to LIMIT_CHANGE
Set NewEvent.EventParam to 0

Post to MasterSM NewEvent


Set query_requested to false
else if query_sent
if not peripheral_request
Post to self GET_FOLLOWER_RESPONSE
Else
Post to self GET_PERIPHERAL_RESPONSE
Set query_sent to false
Set query_requested to true

End of SPIReceiveInterrupt

You might also like