Communication MODULE PSEUDOCODE
Communication Module
static local currentState stores CommunicationState_t
//Possible states include: IDLE, WAIT_STR_FALL, WAIT_TO_REC, WAIT_TO_SEND, WAIT_ACK,
static local data2Send
define STR 6 ACK 7 (pin number)
initCommunication takes nothing, returns true
set communication pins as input
initialize the deferral queue.
set currentState to IDLE
return true
End of initCommunication
runCommunication takes EventTypes ES_STR_RISE, ES_STR_FALL, ES_ACK_RISE, ES_SEND_REQ and ES_TIMEOUT and returns ES_NO_EVENT
If currentState is IDLE // Doing nothing
If EventType is ES_STR_RISE //Opponent is sending data
read DATA lines and save to receivedMessage
post Event ES_DATA_RECEIVED and receivedMessage to Message Module
set currentState to WAIT_STR_FALL
assert(ACK)
Else if EventType is ES_SEND_REQ //I want to send data
If Strobe is LOW // and opponent is doing nothing
set DATA lines to output and load EventParam to the lines
set currentState to WAIT_ACK // wait until he replies
start ACK_Timer
assert(STR)
Else (Strobe is HIGH) // but opponent was faster
save EventParam to data2Send
set currentState to WAIT_TO_REC // receive message first
End if
End if
End if
If currentState is WAIT_STR_FALL // He got my reply
If EventType is ES_STR_FALL
deassert(ACK)
set currentState to IDLE // I’m going back to rest
Else
defer event
End if
Else if currentState is WAIT_TO_REC // receive message first
If EventType is ES_STR_RISE // opponent re-sends message
read DATA lines and save to receivedMessage
assert(ACK)
set currentState to WAIT_TO_SEND // wait until he is doing nothing
Else
defer event
End if
Else if currentState is WAIT_TO_SEND // now, send MY message
If EventType is ES_STR_FALL
deassert(ACK)
set DATA lines to output and load data2Send to the lines
set currentState to WAIT_ACK // wait until he replies
start ACK_Timer
assert(STR)
Else
defer event
End if
Else if currentState is WAIT_ACK // waiting for reply
If EventType is ES_ACK_RISE // got it
set DATA lines to input
deassert(STR)
set currentState to IDLE // going back to rest
Else if EventType is ES_TIMEOUT // out of signal
post Event ES_SEND_REQ to Communication Module // send message again
Else
defer event
End if
End if
If currentState is IDLE, recall events from the deferral queue.
return ES_NO_EVENT
End if
End of runCommunication
check4StrobeRise takes nothing, returns true if event occurs
check4StrobeFall takes nothing, returns true if event occurs
check4AckRise takes nothing, returns true if event occurs
assert(STR or ACK) takes nothing, returns nothing. Sets STR/ACK as output, sets high.
deassert(STR or ACK) takes nothing, returns nothing. Sets STR/ACK as low, sets input.
static local currentState stores CommunicationState_t
//Possible states include: IDLE, WAIT_STR_FALL, WAIT_TO_REC, WAIT_TO_SEND, WAIT_ACK,
static local data2Send
define STR 6 ACK 7 (pin number)
initCommunication takes nothing, returns true
set communication pins as input
initialize the deferral queue.
set currentState to IDLE
return true
End of initCommunication
runCommunication takes EventTypes ES_STR_RISE, ES_STR_FALL, ES_ACK_RISE, ES_SEND_REQ and ES_TIMEOUT and returns ES_NO_EVENT
If currentState is IDLE // Doing nothing
If EventType is ES_STR_RISE //Opponent is sending data
read DATA lines and save to receivedMessage
post Event ES_DATA_RECEIVED and receivedMessage to Message Module
set currentState to WAIT_STR_FALL
assert(ACK)
Else if EventType is ES_SEND_REQ //I want to send data
If Strobe is LOW // and opponent is doing nothing
set DATA lines to output and load EventParam to the lines
set currentState to WAIT_ACK // wait until he replies
start ACK_Timer
assert(STR)
Else (Strobe is HIGH) // but opponent was faster
save EventParam to data2Send
set currentState to WAIT_TO_REC // receive message first
End if
End if
End if
If currentState is WAIT_STR_FALL // He got my reply
If EventType is ES_STR_FALL
deassert(ACK)
set currentState to IDLE // I’m going back to rest
Else
defer event
End if
Else if currentState is WAIT_TO_REC // receive message first
If EventType is ES_STR_RISE // opponent re-sends message
read DATA lines and save to receivedMessage
assert(ACK)
set currentState to WAIT_TO_SEND // wait until he is doing nothing
Else
defer event
End if
Else if currentState is WAIT_TO_SEND // now, send MY message
If EventType is ES_STR_FALL
deassert(ACK)
set DATA lines to output and load data2Send to the lines
set currentState to WAIT_ACK // wait until he replies
start ACK_Timer
assert(STR)
Else
defer event
End if
Else if currentState is WAIT_ACK // waiting for reply
If EventType is ES_ACK_RISE // got it
set DATA lines to input
deassert(STR)
set currentState to IDLE // going back to rest
Else if EventType is ES_TIMEOUT // out of signal
post Event ES_SEND_REQ to Communication Module // send message again
Else
defer event
End if
End if
If currentState is IDLE, recall events from the deferral queue.
return ES_NO_EVENT
End if
End of runCommunication
check4StrobeRise takes nothing, returns true if event occurs
check4StrobeFall takes nothing, returns true if event occurs
check4AckRise takes nothing, returns true if event occurs
assert(STR or ACK) takes nothing, returns nothing. Sets STR/ACK as output, sets high.
deassert(STR or ACK) takes nothing, returns nothing. Sets STR/ACK as low, sets input.