FARMERRxSM - XBee Receive Pseudocode States WaitFor0x7E, WaitForMSBLen, WaitForLSBLen, ReadDataPacket /**********************************************************/ InitFARMERRxSM 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 ClearRxVars to start Set the CurrentState to WaitFor0x7E /**********************************************************/ PostFARMERRxSM Just the usual post to this service /**********************************************************/ RunFARMERRxSM (all receive state machine code is in the ISR) If EventType of posted ThisEvent is ES_UART_ERROR_FLAG Set CurrentState to WaitFor0x7E Switch based on CurrentState CurrentState is WaitFor0x7E (empty) End of case CurrentState is WaitForMSBLen (empty) End of case CurrentState is WaitForLSBLen (empty) End of case CurrentState is ReadDataPacket (empty) End of case End of switch Return ReturnEvent End of RunFARMERRxSM /**********************************************************/ QueryFARMERRxSM Take nothing, returns the current state of theFARMERRxSM state machine Return CurrentState of FARMERRxSM End of QueryFARMERRxSM /**********************************************************/ FARMERRxISR 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 FARMERRxISR /**********************************************************/ GetDOGAddressMSB Takes nothing, returns uint8_t DOGAddressMSB return DOGAddressMSB End of GetDOGAddressMSB /**********************************************************/ GetDOGAddressLSB Takes nothing, returns uint8_t DOGAddressLSB return DOGAddressLSB End of GetDOGAddressLSB /**********************************************************/ GetGyroZStatus Takes nothing, returns uint16_t gyroStatus Set uint16_t variable gyroStatus equal to Status message array index GYROZ_MSB left shifted by 8. gyroStatus equals gyroStatus OR’d with the Status message array index GYROZ_LSB return gyroStatus End of GetGyroZStatus /**********************************************************/ SaveRFData Takes nothing, returns nothing Public function to save message receive as PAIR_ACK, ENCR_RESET, STATUS messages If the API_ID_INDEX of RxDataPacket array is equal to 0x81 (the Incoming API ID) If HEADER_BYTE index of RxDataPacket is PAIR_ACK Save the DOGAddressMSB/LSB (source address) to use for checking all future incoming packets Create and post ES_PACKET_RX EventType with the Param PAIR_ACK to FARMER_FSM End if (PAIR_ACK) Else ( the Rx'd packet is not a PAIR_ACK byte) If source of RxDataPacket is from our DOG Post PacketReceived event to FARMER_FSM with HEADER_BYTE of RxDataPacket as the EventParam If length of RxDataPacket is STATUS Loop through packet and save RxDataPacket bytes into Status array End if End if End if Endif End of SaveRFData /********************************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