Index: NVDataMgmt.c =================================================================== diff -u -r7c78ed49a0f1368084c40b62217fca09c6775b6c -ree4a1386feff846854f3ec80593f129b1ac2af43 --- NVDataMgmt.c (.../NVDataMgmt.c) (revision 7c78ed49a0f1368084c40b62217fca09c6775b6c) +++ NVDataMgmt.c (.../NVDataMgmt.c) (revision ee4a1386feff846854f3ec80593f129b1ac2af43) @@ -127,10 +127,11 @@ NVDATAMGMT_EXEC_STATE_WAIT_FOR_POST = 0, ///< Exec state wait for POST. NVDATAMGMT_EXEC_STATE_IDLE, ///< Exec state Idle. NVDATAMGMT_EXEC_STATE_WRITE_TO_EEPROM, ///< Exec state write to EEPROM. - NVDATAMGMT_EXEC_STATE_VERIFY_WRITE, ///< Exec state verify write (EEPROM and RTC RAM). + NVDATAMGMT_EXEC_STATE_VERIFY_EEPROM_WRITE, ///< Exec state verify EEPROM write. NVDATAMGMT_EXEC_STATE_READ_FROM_EEPROM, ///< Exec state read from EEPROM. NVDATAMGMT_EXEC_STATE_ERASE_EEPROM, ///< Exec state erase EEPROM. NVDATAMGMT_EXEC_STATE_WRITE_TO_RTC, ///< Exec state write to RTC. + NVDATAMGMT_EXEC_STATE_VERIFY_RTC_WRITE, ///< Exec state verify RTC write. NVDATAMGMT_EXEC_STATE_READ_FROM_RTC, ///< Exec state read from RTC. NUM_OF_NVDATAMGMT_EXEC_STATES ///< Total number of exec states. } NVDATAMGMT_EXEC_STATE_T; @@ -447,9 +448,10 @@ static NVDATAMGMT_EXEC_STATE_T handleExecIdleState ( void ); static NVDATAMGMT_EXEC_STATE_T handleExecEraseState ( void ); static NVDATAMGMT_EXEC_STATE_T handleExecWriteToEEPROMState ( void ); -static NVDATAMGMT_EXEC_STATE_T handleExecVerifyWriteState ( void ); +static NVDATAMGMT_EXEC_STATE_T handleExecVerifyEEPROMWriteState ( void ); static NVDATAMGMT_EXEC_STATE_T handleExecReadFromEEPROMState ( void ); static NVDATAMGMT_EXEC_STATE_T handleExecWriteToRAMState ( void ); +static NVDATAMGMT_EXEC_STATE_T handleExecVerifyRTCWriteState( void ); static NVDATAMGMT_EXEC_STATE_T handleExecReadFromRAMState ( void ); static PROCESS_RECORD_STATE_T handleExecProcessRecordIdleState( void ); @@ -580,8 +582,8 @@ nvDataMgmtExecState = handleExecWriteToEEPROMState(); break; - case NVDATAMGMT_EXEC_STATE_VERIFY_WRITE: - nvDataMgmtExecState = handleExecVerifyWriteState(); + case NVDATAMGMT_EXEC_STATE_VERIFY_EEPROM_WRITE: + nvDataMgmtExecState = handleExecVerifyEEPROMWriteState(); break; case NVDATAMGMT_EXEC_STATE_READ_FROM_EEPROM: @@ -596,6 +598,10 @@ nvDataMgmtExecState = handleExecWriteToRAMState(); break; + case NVDATAMGMT_EXEC_STATE_VERIFY_RTC_WRITE: + nvDataMgmtExecState = handleExecVerifyRTCWriteState(); + break; + case NVDATAMGMT_EXEC_STATE_READ_FROM_RTC: nvDataMgmtExecState = handleExecReadFromRAMState(); break; @@ -2002,12 +2008,12 @@ { if ( ops == NVDATAMGMT_ERASE_SECTOR ) { + tempTest = getMSTimerCount(); Fapi_issueAsyncCommandWithAddress ( Fapi_EraseSector, (U32*)startAddress ); state = NVDATAMGMT_EXEC_STATE_ERASE_EEPROM; } else if ( ops == NVDATAMGMT_WRITE && location == NVDATAMGMT_EEPROM ) { - tempTest = getMSTimerCount(); Fapi_issueProgrammingCommand ( (U32*)startAddress, (U08*)bufferAddress, maxBufferLength, 0x00, 0, Fapi_DataOnly ); state = NVDATAMGMT_EXEC_STATE_WRITE_TO_EEPROM; } @@ -2072,7 +2078,7 @@ // Issue a FAPI read command but only the bytes that were written, so use maxWriteBufferSize Fapi_doMarginRead ( (U32*)startAddress, (U32*)writtenRecordCheckBuffer, jobSpecs.maxWriteBufferSize, Fapi_NormalRead ); - state = NVDATAMGMT_EXEC_STATE_VERIFY_WRITE; + state = NVDATAMGMT_EXEC_STATE_VERIFY_EEPROM_WRITE; } else if ( writtenRecordStatus == NVDATAMGMT_RECORD_VALID ) { @@ -2109,15 +2115,15 @@ /*********************************************************************//** * @brief - * The handleExecVerifyWriteState checks all the bytes that were written - * to either EEPROM or RTC RAM to ensure they match the data in the RAM. + * The handleExecVerifyEEPROMWriteState checks all the bytes that were + * written to EEPROM to ensure they match the data in the NV memory. * @details Inputs: recordAddressOffset * @details Outputs: recordAddressOffset * @return next state of the state machine *************************************************************************/ -static NVDATAMGMT_EXEC_STATE_T handleExecVerifyWriteState ( void ) +static NVDATAMGMT_EXEC_STATE_T handleExecVerifyEEPROMWriteState ( void ) { - NVDATAMGMT_EXEC_STATE_T state = NVDATAMGMT_EXEC_STATE_VERIFY_WRITE; + NVDATAMGMT_EXEC_STATE_T state = NVDATAMGMT_EXEC_STATE_VERIFY_EEPROM_WRITE; BOOL timeoutStatus = didCommandTimeout ( ALARM_ID_NVDATA_EEPROM_OPS_FAILURE, (U08*)&state ); RECORD_JOBS_STATE_T job = recordCurrentJob.recordJob; PROCESS_RECORD_SPECS_T jobSpecs = RECORDS_SPECS [ job ]; @@ -2221,10 +2227,112 @@ { NVDATAMGMT_EXEC_STATE_T state = NVDATAMGMT_EXEC_STATE_WRITE_TO_RTC; BOOL timeoutStatus = didCommandTimeout ( ALARM_ID_RTC_RAM_OPS_ERROR, (U08*)&state ); + RECORD_JOBS_STATE_T job = recordCurrentJob.recordJob; + PROCESS_RECORD_SPECS_T jobSpecs = RECORDS_SPECS [ job ]; + // Check if the RTC RAM has finished + if ( getRTCRAMStatus() == RTC_RAM_STATUS_IDLE ) + { + // Check the integrity of data that was written + if ( writtenRecordStatus == NVDATAMGMT_RECORD_NOT_CHECKED ) + { + currentTime = getMSTimerCount(); + U32 startAddress = jobSpecs.startAddress + recordAddressOffset; + + readFromRAM( startAddress, jobSpecs.maxWriteBufferSize ); + + state = NVDATAMGMT_EXEC_STATE_VERIFY_RTC_WRITE; + } + // The written record was not valid. rewrite it again. + else if ( writtenRecordStatus == NVDATAMGMT_RECORD_NOT_VALID ) + { + U32 memoryPtr = jobSpecs.startAddress + recordAddressOffset; + U08* structPtr = jobSpecs.structAddressPtr + recordAddressOffset; + // Set the check status to not checked to check the data again + writtenRecordStatus = NVDATAMGMT_RECORD_NOT_CHECKED; + // Issue the write command + writeToRAM ( memoryPtr, structPtr, jobSpecs.maxWriteBufferSize ); + } + else if ( writtenRecordStatus == NVDATAMGMT_RECORD_VALID ) + { + // If the data is valid, and if it is at the end of the write, or the size of job is equal to + // max write buffer, go to idle + if ( jobSpecs.sizeofJob == recordAddressOffset || jobSpecs.sizeofJob == jobSpecs.maxWriteBufferSize ) + { + writtenRecordStatus = NVDATAMGMT_RECORD_NOT_CHECKED; + state = NVDATAMGMT_EXEC_STATE_IDLE; + } + else + { + // Update the variables and issue the next write command + currentTime = getMSTimerCount(); + recordAddressOffset += jobSpecs.maxWriteBufferSize; + U32 memoryPtr = jobSpecs.startAddress + recordAddressOffset; + U08* structPtr = jobSpecs.structAddressPtr + recordAddressOffset; + + writtenRecordStatus = NVDATAMGMT_RECORD_NOT_CHECKED; + // Issue the write command + writeToRAM ( memoryPtr, structPtr, jobSpecs.maxWriteBufferSize ); + } + } + } + // If timed out, get back to Idle + else if ( timeoutStatus == TRUE ) + { + // TODO schedule another write? + recordAddressOffset = 0; + state = NVDATAMGMT_EXEC_STATE_IDLE; + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleExecVerifyRTCWriteState checks all the bytes that were + * written to RTC RAM to ensure they match the data in the NV memory. + * @details Inputs: recordAddressOffset + * @details Outputs: recordAddressOffset + * @return next state of the state machine + *************************************************************************/ +static NVDATAMGMT_EXEC_STATE_T handleExecVerifyRTCWriteState( void ) +{ + NVDATAMGMT_EXEC_STATE_T state = NVDATAMGMT_EXEC_STATE_VERIFY_RTC_WRITE; + BOOL timeoutStatus = didCommandTimeout ( ALARM_ID_NVDATA_EEPROM_OPS_FAILURE, (U08*)&state ); + RECORD_JOBS_STATE_T job = recordCurrentJob.recordJob; + PROCESS_RECORD_SPECS_T jobSpecs = RECORDS_SPECS [ job ]; + + // Check if the write job is EEPROM or RTC RAM if ( getRTCRAMStatus() == RTC_RAM_STATUS_IDLE || timeoutStatus == TRUE ) { - state = NVDATAMGMT_EXEC_STATE_IDLE; + U32 i; + U08* bufferPtr = jobSpecs.structAddressPtr + recordAddressOffset; + U08 tempBufferToCheckData [ MAX_RTC_RAM_OPS_BUFFER_BYTES ]; + getDataFromRAM( tempBufferToCheckData, jobSpecs.maxWriteBufferSize ); + + // Loop through the bytes in the buffer + for ( i = 0; i < jobSpecs.maxWriteBufferSize; i++ ) + { + // Check if data in the buffer is not the same as the data in the structure + if ( tempBufferToCheckData[ i ] != *bufferPtr ) + { + // Data is not valid. Go back to write to RTC state and rewrite the portion that was written + // but it is correct. + writtenRecordStatus = NVDATAMGMT_RECORD_NOT_VALID; + state = NVDATAMGMT_EXEC_STATE_WRITE_TO_RTC; + // Exit the loop since there is no point to check the rest of data + break; + } + else + { + // Everything is good increment the pointer to check the next byte. + bufferPtr++; + // Record data is valid so far + writtenRecordStatus = NVDATAMGMT_RECORD_VALID; + // Go back write to RTC state to continue writing the record data + state = NVDATAMGMT_EXEC_STATE_WRITE_TO_RTC; + } + } } return state;