Index: firmware/App/Services/Download.c =================================================================== diff -u -r09e6320e3f3c3c973de0b66e5799d710db8cc427 -r5a58fd72ededee8a718098b53448a2c921c57f49 --- firmware/App/Services/Download.c (.../Download.c) (revision 09e6320e3f3c3c973de0b66e5799d710db8cc427) +++ firmware/App/Services/Download.c (.../Download.c) (revision 5a58fd72ededee8a718098b53448a2c921c57f49) @@ -18,52 +18,56 @@ // ********** private definitions ********** -#define SHIFT_BITS_TO_GET_TARGET 4 ///< Shift bits by 4 to get the update target. - static const U32 NUM_OF_CAN_BYTES_FOR_UPDATE = SW_UPDATE_FLASH_BUFFER_SIZE + CAN_MESSAGE_FRAME_SIZE; ///< Number of CAN bytes for update. // ********** private data ********** /// Software update response status structure typedef struct { - U08 msgID; ///< Message ID. + U16 msgID; ///< Message ID. U08 msgAckNackStatus; ///< Message ack or nack status. - U16 cyberRandom; ///< Message cyber random. + U08 spareSpace; ///< Message spare byte. U32 msgCRC; ///< Message CRC. } SW_UPDATE_RESP_STATUS_T; /// Software update command status structure typedef struct { - U08 msgID; ///< Message ID. - U08 updateCmd; ///< Update command. - U16 cyberRandom; ///< Cyber random. + U16 msgID; ///< Message ID. + U08 updateCmdAndStack; ///< Update command and stack. + U08 destination; ///< Update destination. U32 msgCRC; ///< Message CRC. } SW_UPDATE_CMD_STATUS_T; /// Software update receive update status structure typedef struct { - U08 msgID; ///< Message ID. - U08 updateDest; ///< Update destination (FW, FPGA). + U16 msgID; ///< Message ID. U16 updatePayloadLen; ///< Update payload length in bytes. U32 msgCRC; ///< Message CRC. U08 SWUpdateBuffer[ SW_UPDATE_FLASH_BUFFER_SIZE ]; ///< Software update buffer. } SW_UPDATE_RCV_STATUS_T; +/// Software update specification structure +typedef struct +{ + SW_UPDATE_STACK_T stack; ///< Software update stack (TD, DD). + SW_UPDATE_DESINTATION_T dest; ///< Software update destination (FW, FPGA). + SW_UPDATE_CMD_T cmd; ///< Software update command (e.g. update, verify) +} SW_UPDATE_SPECS_T; + static SW_UPDATE_RCV_STATUS_T SWUpdateRCVStatus; ///< Software update receive status. -static SW_UPDATE_CMD_T SWUpdateCommandState; ///< Software update command state. -static SW_UPDATE_CAN_MAIL_BOX_T thisStackMailBox; ///< The mailbox of this stack. -static U32 sizeToWrite; ///< Size to write to destination. +static U32 fpgaPayloadLengthBytes; ///< FPGA payload lenght in bytes. +static SW_UPDATE_SPECS_T SWUpdateSpecs; ///< Software update specifications. static U32 REMOVETHEVAR = 0; // TODO remove // ********** private function prototypes ********** static void handleIncomingCmdMessage( SW_UPDATE_CAN_MAIL_BOX_T mailBox ); static void handleIncomingUpdateMessage( SW_UPDATE_CAN_MAIL_BOX_T mailBox ); -static void prepareAndSendFWResponseMessage( U08 respOfMsgID, ACK_NACK_STATUS_T ackNack ); +static void prepareAndSendFWResponseMessage( U16 respOfMsgID, ACK_NACK_STATUS_T ackNack ); static void clearSWUpdateBuffer( void ); static ACK_NACK_STATUS_T handleFirmwareUpdate( void ); static ACK_NACK_STATUS_T handleFPGAUpdate( void ); @@ -77,10 +81,8 @@ *************************************************************************/ void initDownload( void ) { - thisStackMailBox = RECEIVE_MSG_ID[ BL_STACK_ID ][ FW_STACKS_RCV_MAIL_BOX_INDEX ]; - clearSWUpdateBuffer(); - clearSWUpdateCommandState(); + clearSWUpdateSpecs(); } /*********************************************************************//** @@ -92,6 +94,8 @@ *************************************************************************/ void execDownload( void ) { + SW_UPDATE_CAN_MAIL_BOX_T thisStackMailBox = RECEIVE_MSG_ID[ BL_STACK_ID ][ FW_STACKS_RCV_MAIL_BOX_INDEX ]; + handleIncomingCmdMessage( SW_UPDATE_COMMAD ); handleIncomingUpdateMessage( thisStackMailBox ); } @@ -106,33 +110,35 @@ *************************************************************************/ SW_UPDATE_CMD_T getSWUpdateCommandState( void ) { - return SWUpdateCommandState; + return SWUpdateSpecs.cmd; } /*********************************************************************//** * @brief * The getSWUpdateDestination function returns the current software update * destination (firmware or FPGA). - * @details \b Inputs: SWUpdateRCVStatus + * @details \b Inputs: SWUpdateSpecs * @details \b Outputs: none * @return current software update destination *************************************************************************/ SW_UPDATE_DESINTATION_T getSWUpdateDestination( void ) { - return (SW_UPDATE_DESINTATION_T)SWUpdateRCVStatus.updateDest; + return SWUpdateSpecs.dest; } /*********************************************************************//** * @brief - * The clearSWUpdateCommandState function clears the software update command - * state to idle state. + * The clearSWUpdateSpecs function clears the software update specifications + * structure. * @details \b Inputs: none - * @details \b Outputs: SWUpdateCommandState + * @details \b Outputs: SWUpdateSpecs * @return none *************************************************************************/ -void clearSWUpdateCommandState( void ) +void clearSWUpdateSpecs( void ) { - SWUpdateCommandState = UPDATE_CMD_IDLE; + memset( &SWUpdateSpecs, 0x0, sizeof( SW_UPDATE_SPECS_T ) ); + + SWUpdateSpecs.cmd = UPDATE_CMD_IDLE; } /*********************************************************************//** @@ -146,6 +152,8 @@ *************************************************************************/ void sendFPGAAckNackStatus( ACK_NACK_STATUS_T ackNackStatus ) { + SW_UPDATE_CAN_MAIL_BOX_T thisStackMailBox = RECEIVE_MSG_ID[ BL_STACK_ID ][ FW_STACKS_RCV_MAIL_BOX_INDEX ]; + prepareAndSendFWResponseMessage( SWUpdateRCVStatus.msgID, ackNackStatus ); clearCommBuffer( thisStackMailBox ); } @@ -176,22 +184,23 @@ getCommBuffer( mailBox, (U08*)&SWUpdateCmdStatus, sizeof( SW_UPDATE_CMD_STATUS_T ) ); - BOOL hasCRCPassed = FALSE; - ACK_NACK_STATUS_T ackStatus = NACK; - U32 calcCRC = 0; - U08 msgID = SWUpdateCmdStatus.msgID; - SW_UPDATE_DESINTATION_T dest = (SW_UPDATE_DESINTATION_T)( SWUpdateCmdStatus.updateCmd >> SHIFT_BITS_TO_GET_TARGET ); // TODO add more logic for other stacks + BOOL hasCRCPassed = FALSE; + ACK_NACK_STATUS_T ackStatus = NACK; + U32 calcCRC = 0; + U16 msgID = SWUpdateCmdStatus.msgID; // Calculate the CRC of the message and ack or nack based on the matching of the CRCs calcCRC = crc32( calcCRC, (U08*)&SWUpdateCmdStatus, sizeof( SW_UPDATE_CMD_STATUS_T ) - sizeof( U32 ) ); hasCRCPassed = ( SWUpdateCmdStatus.msgCRC == calcCRC ? TRUE : FALSE ); if ( TRUE == hasCRCPassed ) { - ackStatus = ACK; - SWUpdateCommandState = (SW_UPDATE_CMD_T)( SWUpdateCmdStatus.updateCmd & MASK_OFF_NIBBLE_MSB ); + SWUpdateSpecs.stack = (SW_UPDATE_STACK_T)( SWUpdateCmdStatus.updateCmdAndStack >> SHIFT_4_BITS_FOR_NIBBLE ); + SWUpdateSpecs.cmd = (SW_UPDATE_CMD_T)( SWUpdateCmdStatus.updateCmdAndStack & MASK_OFF_NIBBLE_MSB ); + SWUpdateSpecs.dest = (SW_UPDATE_DESINTATION_T)SWUpdateCmdStatus.destination; + ackStatus = ACK; - if ( UPDATE_FPGA == dest ) // TODO once verify command is received for FPGA, make sure the prepare signal is not sent again + if ( UPDATE_FPGA == SWUpdateSpecs.dest ) // TODO once verify command is received for FPGA, make sure the prepare signal is not sent again { // If the update destination is FPGA, signal FPGA to prepare for the update. signalFPGAToPrepareForUpdate(); @@ -209,7 +218,7 @@ * The handleIncomingUpdateMessage function handles the incoming update message * from the CAN bus. Once the entire update message has been received, it then * is sent to the corresponding destination that is under update. - * @details \b Inputs: SWUpdateRCVStatus + * @details \b Inputs: SWUpdateRCVStatus, SWUpdateSpecs * @details \b Outputs: SWUpdateRCVStatus * @param mailbox of the buffer that has been received * @return none @@ -223,7 +232,7 @@ U32 calcCRC = 0; BOOL hasCRCPassed = FALSE; ACK_NACK_STATUS_T ackStatus = NACK; - U08 msgID = 0; + U16 msgID = 0; U16 payloadLength = 0; getCommBuffer( mailBox, (U08*)&SWUpdateRCVStatus, NUM_OF_CAN_BYTES_FOR_UPDATE ); @@ -241,14 +250,14 @@ if ( FALSE == hasCRCPassed ) { - BOOL test = TRUE; // TODO remove + BOOL test = TRUE; // TODO remove for testing } if ( TRUE == hasCRCPassed ) { // CRC passed, call the corresponding the handlers to update either FPGA or firmware - switch ( SWUpdateRCVStatus.updateDest ) + switch ( SWUpdateSpecs.dest ) { case UPDATE_FIRMWARE: ackStatus = handleFirmwareUpdate(); @@ -280,14 +289,14 @@ * @param ack nack status * @return none *************************************************************************/ -static void prepareAndSendFWResponseMessage( U08 respOfMsgID, ACK_NACK_STATUS_T ackNack ) +static void prepareAndSendFWResponseMessage( U16 respOfMsgID, ACK_NACK_STATUS_T ackNack ) { SW_UPDATE_RESP_STATUS_T resp; U32 calcCRC = 0; resp.msgID = respOfMsgID; resp.msgAckNackStatus = ackNack; - resp.cyberRandom = 0; + resp.spareSpace = 0; resp.msgCRC = crc32( calcCRC, (U08*)&resp, sizeof( SW_UPDATE_RESP_STATUS_T ) - sizeof( U32 ) ); sendAckNackStatusFromFirmware( (U08*)&resp ); // TODO do we have to retry if send failed? @@ -346,12 +355,12 @@ if ( SWUpdateRCVStatus.updatePayloadLen != SW_UPDATE_FINAL_MSG_INDEX ) { - sizeToWrite = SWUpdateRCVStatus.updatePayloadLen; - REMOVETHEVAR += sizeToWrite; + fpgaPayloadLengthBytes = SWUpdateRCVStatus.updatePayloadLen; + REMOVETHEVAR += fpgaPayloadLengthBytes; - signalFPGAToWriteToFlash( SWUpdateRCVStatus.SWUpdateBuffer, sizeToWrite ); + signalFPGAToWriteToFlash( SWUpdateRCVStatus.SWUpdateBuffer, fpgaPayloadLengthBytes ); - if ( sizeToWrite < SW_UPDATE_FLASH_BUFFER_SIZE ) + if ( fpgaPayloadLengthBytes < SW_UPDATE_FLASH_BUFFER_SIZE ) { signalFPGAToSelfConfigure(); }