Index: firmware/App/Services/CommBuffers.c =================================================================== diff -u -r0c085209bea23f66011059a7c19796c1e4b246fa -rfc9a9244cf4288ff0623c3e02455ac565bf60cdd --- firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision 0c085209bea23f66011059a7c19796c1e4b246fa) +++ firmware/App/Services/CommBuffers.c (.../CommBuffers.c) (revision fc9a9244cf4288ff0623c3e02455ac565bf60cdd) @@ -11,11 +11,12 @@ // ********** private definitions ********** -#define UPDATE_PACKET_SIZE_BYTES ( SW_UPDATE_FLASH_BUFFER_SIZE + CAN_MESSAGE_PAYLOAD_SIZE ) ///< Software update packet size in bytes. +#define UPDATE_PACKET_SIZE_BYTES ( SW_UPDATE_FLASH_BUFFER_SIZE + CAN_MESSAGE_FRAME_SIZE ) ///< Software update packet size in bytes. /// Software update buffer structure typedef struct { + U32 SWUpdateNumOfFramesCount; ///< Software update number of frame count. U32 SWUpdateFrameCount; ///< Software update frame count. U08 SWUpdateBuffer[ UPDATE_PACKET_SIZE_BYTES ]; ///< Software update buffer. } SW_UPDATE_BUFFER_T; @@ -66,13 +67,42 @@ U32 currentFrameCount = SWUpdateBuffer[ mailBox ].SWUpdateFrameCount; // Check if the number of bytes is less than the allowed bytes in the mailbox buffer. - if ( ( currentFrameCount * CAN_MESSAGE_PAYLOAD_SIZE ) <= UPDATE_PACKET_SIZE_BYTES ) + if ( ( currentFrameCount * CAN_MESSAGE_FRAME_SIZE ) <= UPDATE_PACKET_SIZE_BYTES ) { U32 currentBufferIndex = currentFrameCount * len; // Copy the received can frame into the buffer memcpy( SWUpdateBuffer[ mailBox ].SWUpdateBuffer + currentBufferIndex, data, len ); + switch ( mailBox ) + { + case SW_UPDATE_COMMAD: + // A command is only 1 frame of 8 bytes. + SWUpdateBuffer[ mailBox ].SWUpdateNumOfFramesCount = NUM_OF_CMD_CAN_FRAMES; + break; + + case SW_UPDATE_TD_UPDATE: + case SW_UPDATE_DD_UPDATE: + if ( 0 == SWUpdateBuffer[ mailBox ].SWUpdateFrameCount ) + { + // If the received command is a software update mailbox and if the frame count is 0, so it is the overhead frame. + // In the overhead frame the 2nd and 3rd bytes are payload length and the two payloads are bitwised to get the payload length. + U08 updatePayloadLenMSB = SWUpdateBuffer[ mailBox ].SWUpdateBuffer[ SW_UPDATE_LEN_MSB_INDEX ]; + U08 updatePayloadLenLSB = SWUpdateBuffer[ mailBox ].SWUpdateBuffer[ SW_UPDATE_LEN_LSB_INDEX ]; + U16 updatePayloadLength = updatePayloadLenMSB << SHIFT_8_BITS_FOR_BYTE_SHIFT | updatePayloadLenLSB; + // Per the payload length, calculate the number of frames of 8 bytes that are needed to receive the entire update message. + // Round up the number of frames with (payload length + 1 can frame (8 bytes) - 1) / 1 can frame (8 bytes) + // After calculating the number of frames needed to receive the entire update payload add one more frame for the overhead frame. + SWUpdateBuffer[ mailBox ].SWUpdateNumOfFramesCount = ( updatePayloadLength + CAN_MESSAGE_FRAME_SIZE - 1 ) / CAN_MESSAGE_FRAME_SIZE; + SWUpdateBuffer[ mailBox ].SWUpdateNumOfFramesCount += 1; + } + break; + + default: + // Do nothing. There are other mailboxes that do not need to be processed. + break; + } + // Increment the current frame count SWUpdateBuffer[ mailBox ].SWUpdateFrameCount += 1; status = TRUE; @@ -115,23 +145,44 @@ /*********************************************************************//** * @brief - * The getNumberOfBytesInBuffer function determines how many bytes - * are currently contained in a given comm buffer. + * The isMessageComplete function checks whether a message (command or update) + * has been fully received. * @details Inputs: SWUpdateBuffer[] * @details Outputs: none - * @param mailbox ID of buffer to get byte count for - * @return the number of bytes currently in the given comm buffer. + * @param mailbox ID of buffer to check the completeness of the message + * @return TRUE if the messages has been fully received, otherwise FALSE *************************************************************************/ -S32 getNumberOfBytesInBuffer( SW_UPDATE_CAN_MAIL_BOX_T mailBox ) +BOOL isMessageComplete( SW_UPDATE_CAN_MAIL_BOX_T mailBox ) { - S32 bytes = -1; + BOOL status = FALSE; + U32 frameCnt = 0; + U32 numOfFramesCnt = 0; - if ( mailBox < NUM_OF_SW_UPDATE_MBOXES ) + switch ( mailBox ) { - bytes = SWUpdateBuffer[ mailBox ].SWUpdateFrameCount * CAN_MESSAGE_PAYLOAD_SIZE; + case SW_UPDATE_COMMAD: + frameCnt = SWUpdateBuffer[ mailBox ].SWUpdateFrameCount; + numOfFramesCnt = SWUpdateBuffer[ mailBox ].SWUpdateNumOfFramesCount; + break; + + case SW_UPDATE_TD_UPDATE: + case SW_UPDATE_DD_UPDATE: + frameCnt = SWUpdateBuffer[ mailBox ].SWUpdateFrameCount; + numOfFramesCnt = SWUpdateBuffer[ mailBox ].SWUpdateNumOfFramesCount; + break; + + default: + // Do nothing. There are other mailboxes that do not need to be processed. + break; } - return bytes; + // Check if the frame count is not 0 meaning that at least one frame is received prior to checking for the completion. + if ( ( frameCnt > 0 ) && ( frameCnt == numOfFramesCnt ) ) + { + status = TRUE; + } + + return status; } /*********************************************************************//**