DOGRxSM - XBee Receive Pseudocode States WaitFor0x7E, WaitForMSBLen, WaitForLSBLen, ReadDataPacket /**********************************************************/ InitDOGRxSM Save away priority of service (InitUART is called elsewhere in code, cannot double call init) Enable UART receive interrupt via the RXIM bit in UARTIM Set the CurrentState to WaitFor0x7E /**********************************************************/ PostDOGRxSM Just the usual post to this service /**********************************************************/ RunDOGRxSM (all receive state machine code is in the ISR) If EventType of posted ThisEvent is ES_UART_ERROR_FLAG Call PrintUARTErrors to print the active error(s) Set CurrentState to WaitFor0x7E, the start delimiter End of RunDOGRxSM /**********************************************************/ DOGRxISR Take nothing, returns nothing Poll the Rx interrupt If the RxInterruptBit is set Clear the source of the interrupt in UARTICR Clear the RxInterruptBit Read the data in UARTDR into NewRxByte Read OverRun bit in UARTDR into OverRunBit Read BreakError bit in UARTDR into BreakErrorBit Read ParityError bit in UARTDR into ParityErrorBit Read FramingError bit in UARTDR into FramingErrorBit If OverRunFlag OR BreakErrorFlag OR ParityErrorFlag OR FramingError is true Write to UARTECR register to clear error flags Post ES_UART_ERROR_FLAG event to FARMERRxSM Else data is good Switch based on CurrentState CurrentState is WaitFor0x7E If EventType of ThisEvent is 0x7E received Change CurrentState to WaitForMSBLen Call ClearRxVars to clear receive variables place RxDataByte into RxDataPacket and increment RxArrayIndex Endif Break (End of WaitFor0x7E case) CurrentState is WaitForMSBLen Set MSB of Length to the value event parameter sent from the ISR place RxDataByte into RxDataPacket and increment RxArrayIndex Change CurrentState to WaitForLSBLen Break (End of WaitForMSBLen case) CurrentState is WaitForLSBLen Set LSB of Length to the value event parameter sent from the ISR Place RxDataByte into RxDataPacket and increment RxArrayIndex Combine MSB and LSB into BytesLeft, then calculate a PacketLength Change CurrentState to ReadDataPacket Break (End of WaitForLSBLen case) CurrentState is ReadDataPacket If BytesLeft NOT EQUAL to zero place RxDataByte into RxDataPacket increment RxArrayIndex Decrement BytesLeft Add DataByte to ChkSum Else if BytesLeft EQUAL to zero place RxDataByte into RxDataPacket Pull XbeeChkSum out of the last index of RxDataPacket Subtract running checksum from 0xFF to get the final checksum If XbeeChkSum isn’t equal to Rx ChkSum Change states to Waitfor0x7E Else if ChkSum is good Call SaveRFData Set CurrentState to WaitFor0x7E End if End if Break End of switch on CurrentState End of DOGRxISR /**********************************************************/ QueryDOGRxSM Take nothing, returns the current state of the DOGRxSM state machine Return CurrentState of DOGRxSM End of QueryDOGRxSM /**********************************************************/ GetDogTagRequested Takes nothing, returns DogTagRequested, The databyte that the farmer sent indicating the DOG it wants to pair with (0x01, 0x02, or 0x03) return DogTagRequested End of GetDogTagRequested /**********************************************************/ IncEncryptIndex Takes nothing, returns nothing we want it to rollover to 0 after 31 so set EncryptIndex equal to EncryptIndex plus one then take modulo 32 to get remainder End of IncEncryptIndex /**********************************************************/ ResetEncryptIndex Takes nothing, returns nothing Sets EncryptIndex to 0 Prints “EncryptIndex = 0” End of ResetEncryptIndex /**********************************************************/ GetFARMERAddressLSB Takes nothing, returns uint8_t of FARMERAddressLSB Return FARMERAddressLSB /**********************************************************/ GetFARMERAddressMSB Takes nothing, returns uint8_t of FARMERAddressMSB Return FARMERAddressMSB /**********************************************************/ SaveRFData Takes nothing, returns nothing Public function to save message receive as Request to Pair, Encryption key, or Control message If the API_ID_INDEX of RxDataPacket array is equal to 0x81 (the Incoming API ID) If HEADER_BYTE index of RxDataPacket is REQ_2_PAIR Set DogTagRequested to index HEADER_BYTE + 1 of RxDataPacket If the DogTagRequested is the dog tage we read from our bot Save FARMERAddressMSB/LSB from SOURCE_ADDR_MSB/LSB bytes of RxDataPacket Create and post ES_PACKET_RX EventType with the Param REQ_2_PAIR to DOG_FSM End if (DogTag) Else ( the Rx'd packet is not a REQ_2_PAIR byte) If source of RxDataPacket is from our FARMER Post PacketReceived event to DOG_FSM If length of RxDataPacket is CTRL Loop through packet and… save the encrypted control packet decrypt the control packet and save it to array so DOG_FSM can access it using getter function Increment encryption index Else if length of packet indicates encryption key Loop through packet and save the encryption key End if End if End if Endif End of SaveRFData /**********************************************************/ GetDecryptedCtrl Takes uint8_t describing WhichCtrlByte, returns uint8_t decrypted control byte Retun WhichCtrlByte index of the array DecrytedCtrl End of GetDecrytedCtrl /**********************************************************/ ClearDecryptionCtrl Takes nothing, returns nothing Public function; clears out decrypted ctrl array For increment i from 0 to less than CTRL_PACKET_LENGTH Set index i of DecryptedCtrl array to 0 End of loop End of ClearDecryptedCtrl /********************************Private Functions ****************************/ printUARTErrors Takes nothing, returns nothing a simple private function to print error messages identifying UART Rx messages If OverRun error bit is set Print “OverRun Error in UART Rx : Connection Lost” Else if break error bit is set Print “Break Error in UART Rx : Connection Lost” Else if parity error bit is set Print “Parity Error in UART Rx : Connection Lost” Else if framing error bit is set Print “Framing Error in UART Rx : Connection Lost” Endif clear call error bits Return End of printUARTErrors /**********************************************************/ ClearRxDataPacket Takes nothing, returns nothing Helper function to clear all data out of RxDataPacket For RxDataPacket array position 0 to LONGEST_PACKET_LENGTH Set value to zero End for End of ClearRxDataPacket /**********************************************************/ ClearRxVars Takes nothing, returns nothing Helper function to set all Rx vars to zero Clears FrameLengthMSB, FrameLengthLSB, PacketLength, ChkSum, XbeeChkSum, RxArryIndex Calls ClearRxDataPacket End of ClearRxVars