Index: RTC.c =================================================================== diff -u -r88f02ec7299938cc99d60c357f663b88a82da91a -rd8454083670704629941c95c1c57dee1a198784e --- RTC.c (.../RTC.c) (revision 88f02ec7299938cc99d60c357f663b88a82da91a) +++ RTC.c (.../RTC.c) (revision d8454083670704629941c95c1c57dee1a198784e) @@ -15,10 +15,13 @@ * ***************************************************************************/ #include "mibspi.h" + +#include "FPGA.h" #include "OperationModes.h" #include "RTC.h" #include "SystemCommMessages.h" #include "Timers.h" +#include "Utilities.h" #ifdef USE_LIBRARY_TIME_FUNCTIONS #include // For epoch conversion functions mktime and gmtime @@ -105,8 +108,8 @@ #define RTC_WRITE_TO_RAM 0x003C ///< RTC write to RAM command (0x003C) #define RTC_READ_FROM_RAM 0x00BD ///< RTC read from RAM command (0x00BD) -#define RTC_ACCURACY_TIMEOUT 1050U ///< RTC accuracy timeout in ms (1050) // TODO - fix test so expecting 1000 ms -#define RTC_ACCURACY_TIMEOUT_TOLERANCE 50U ///< RTC accuracy timeout tolerance in ms (1050) +#define RTC_ACCURACY_TOLERANCE 75U ///< RTC accuracy tolerance in ms +#define FPGA_ACCURACY_TOLERANCE 12U ///< FPGA accuracy tolerance in ms #define RTC_PUBLISH_INTERVAL 18U ///< RTC publish interval in counts (18) #define TIMER_COUNTER_TO_REQUEST_READ 18U ///< Timer counter for reading time from RTC (18) @@ -144,7 +147,6 @@ RTC_SELF_TEST_STATE_START = 0, ///< Self test start RTC_SELF_TEST_STATE_CHECK_CTRL_REGS, ///< Self test check control registers RTC_SELF_TEST_STATE_WAIT_FOR_FIRST_SECOND, ///< Self test wait for first second - RTC_SELF_TEST_STATE_WAIT_FOR_SECOND_SECOND, ///< Self test wait for second second RTC_SELF_TEST_STATE_CHECK_ACCURACY, ///< Self test check time accuracy (compare 1st to 2nd second vs. timer counter) RTC_SELF_TEST_STATE_COMPLETE, ///< Self test complete NUM_OF_RTC_SELF_TEST_STATES ///< Total number of self-test states @@ -200,7 +202,7 @@ static U32 RTCSelfTestTimer = 0; ///< Self test timer. static U32 RTCPreviousSecond = 0; ///< Previous second for self-test comparison. static U32 RAMBufferLength = 0; ///< RAM buffer length for RAM operations. -static U32 lastEpochTime = 0; ///< last epoch time. +static U32 lastEpochTime = 0; ///< Last epoch time. static U32 previousTransferLength = 0; ///< Previous transfer length. static U32 timeCounter = 1; ///< Initial time counter (1). @@ -218,6 +220,7 @@ static U16 prepRAMBuffer[ RTC_RAM_PREP_BUFFER_LENGTH ]; ///< Buffer to send prep read/write commands to RTC RAM. static U16 RAMBuffer[ MIBSPI_MAX_BUFFER_LENGTH ]; ///< Buffer to read RTC RAM data. +static U16 previousFPGATimerCount = 0; ///< Previous FPGA timer count; /// Array of days in each month. Assumes non-leap year. Must adjust days in February if leap year. static U32 daysInMonth[ 12 ] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; @@ -240,7 +243,6 @@ static RTC_SELF_TEST_STATE_T handleSelfTestStart( void ); static RTC_SELF_TEST_STATE_T handleSelfTestCheckCtrlRegs( void ); static RTC_SELF_TEST_STATE_T handleSelfTestWaitForFirstSecond( void ); -static RTC_SELF_TEST_STATE_T handleSelfTestWaitForSecondSecond( void ); static RTC_SELF_TEST_STATE_T handleSelfTestCheckAccuracy( void ); static RTC_EXEC_STATE_T handleExecWaitForPostState( void ); @@ -372,10 +374,6 @@ RTCSelfTestState = handleSelfTestWaitForFirstSecond(); break; - case RTC_SELF_TEST_STATE_WAIT_FOR_SECOND_SECOND: - RTCSelfTestState = handleSelfTestWaitForSecondSecond(); - break; - case RTC_SELF_TEST_STATE_CHECK_ACCURACY: RTCSelfTestState = handleSelfTestCheckAccuracy(); break; @@ -501,8 +499,6 @@ hasWriteToRAMRequested = TRUE; RAMBufferLength = length; prepRAMBuffer [ RTC_PREP_RAM_INDEX ] = RTC_PREP_RAM_READ_WRITE; - //prepRAMBuffer[ RTC_RAM_HIGH_ADDRESS_INDEX ] = ( address >> SHIFT_8_BITS_FOR_BYTE_SHIFT ); - //prepRAMBuffer[ RTC_RAM_LOW_ADDRESS_INDEX ] = ( address & MASK_OFF_MSB ); prepRAMBuffer [ RTC_RAM_HIGH_ADDRESS_INDEX ] = 0x0000; prepRAMBuffer [ RTC_RAM_LOW_ADDRESS_INDEX ] = castedAddress; txBuffer [ BUFFER_INDEX_0 ] = RTC_WRITE_TO_RAM; @@ -563,6 +559,7 @@ } } } + return status; } @@ -830,6 +827,7 @@ decimalLow = decimal % TEN; bcd = ( decimalHigh << SHIFT_BITS_BY_4 ) + decimalLow; } + return bcd; } @@ -1416,7 +1414,7 @@ RTC_SELF_TEST_STATE_T result = RTC_SELF_TEST_STATE_WAIT_FOR_FIRST_SECOND; BOOL isStatusOk = serviceRTC( txBuffer, rxBuffer, RTC_GENERAL_BUFFER_LENGTH ); - if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) + if ( ( RTCServiceState == RTC_SERVICE_COMPLETE ) && ( TRUE == isStatusOk ) ) { if ( isRTCFunctional() ) { @@ -1425,48 +1423,8 @@ if ( RTCCurrentSecond != RTCPreviousSecond ) { RTCPreviousSecond = RTCCurrentSecond; + previousFPGATimerCount = getFPGATimerCount(); RTCSelfTestTimer = getMSTimerCount(); - - result = RTC_SELF_TEST_STATE_WAIT_FOR_SECOND_SECOND; - } - } - else - { - RTCSelfTestResult = SELF_TEST_STATUS_FAILED; - result = RTC_SELF_TEST_STATE_COMPLETE; - } - } - else if ( RTCServiceState == RTC_SERVICE_COMPLETE ) - { - RTCSelfTestResult = SELF_TEST_STATUS_FAILED; - result = RTC_SELF_TEST_STATE_COMPLETE; - } - - return result; -} - -/*********************************************************************//** - * @brief - * The handleSelfTestWaitForSecondSecond continuously reads the RTC and compares - * latest second from RTC to the previous second and if it has changed, it will - * set the state machine to the next state. - * @details Inputs: RTCServiceState, txBuffer, rxBuffer, RTCCurrentSecond - * @details Outputs: RTCCurrentSecond - * @return next state - *************************************************************************/ -static RTC_SELF_TEST_STATE_T handleSelfTestWaitForSecondSecond( void ) -{ - RTC_SELF_TEST_STATE_T result = RTC_SELF_TEST_STATE_WAIT_FOR_SECOND_SECOND; - BOOL isStatusOk = serviceRTC( txBuffer, rxBuffer, RTC_GENERAL_BUFFER_LENGTH ); - - if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) // TODO - should probably have a timeout on these waits - { - if ( isRTCFunctional() ) - { - U32 RTCCurrentSecond = rxBuffer[ RTC_SECONDS_INDEX ]; - - if ( RTCCurrentSecond != RTCPreviousSecond ) - { result = RTC_SELF_TEST_STATE_CHECK_ACCURACY; } } @@ -1479,7 +1437,7 @@ else if ( RTCServiceState == RTC_SERVICE_COMPLETE ) { RTCSelfTestResult = SELF_TEST_STATUS_FAILED; - result = RTC_SELF_TEST_STATE_COMPLETE; + result = RTC_SELF_TEST_STATE_COMPLETE; } return result; @@ -1499,23 +1457,27 @@ static RTC_SELF_TEST_STATE_T handleSelfTestCheckAccuracy( void ) { RTC_SELF_TEST_STATE_T result = RTC_SELF_TEST_STATE_CHECK_ACCURACY; + BOOL isStatusOk = serviceRTC( txBuffer, rxBuffer, RTC_GENERAL_BUFFER_LENGTH ); - if ( didTimeout( RTCSelfTestTimer, RTC_ACCURACY_TIMEOUT ) ) + if ( ( TRUE == isStatusOk ) && ( rxBuffer[ RTC_SECONDS_INDEX ] != RTCPreviousSecond ) ) { - U32 elapsedTime = calcTimeSince( RTCSelfTestTimer ); - S32 deltaTime = (S32)elapsedTime - (S32)RTC_ACCURACY_TIMEOUT; + S32 const elapsedTime = (S32)calcTimeSince( RTCSelfTestTimer ); + S32 const deltaTime = elapsedTime - (S32)MS_PER_SECOND; + S32 const elapsedFPGATime = (S32)u16DiffWithWrap( previousFPGATimerCount, getFPGATimerCount() ); + result = RTC_SELF_TEST_STATE_COMPLETE; + RTCSelfTestResult = SELF_TEST_STATUS_PASSED; - if ( abs( deltaTime ) > RTC_ACCURACY_TIMEOUT_TOLERANCE ) + if ( abs( deltaTime ) > RTC_ACCURACY_TOLERANCE ) { RTCSelfTestResult = SELF_TEST_STATUS_FAILED; - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RTC_OR_TIMER_ACCURACY_FAILURE, (U32)deltaTime, RTC_ACCURACY_TIMEOUT_TOLERANCE ); + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RTC_OR_TIMER_ACCURACY_FAILURE, (U32)deltaTime, RTC_ACCURACY_TOLERANCE ); } - else + + if ( abs( elapsedFPGATime - elapsedTime ) > FPGA_ACCURACY_TOLERANCE ) { - RTCSelfTestResult = SELF_TEST_STATUS_PASSED; + RTCSelfTestResult = SELF_TEST_STATUS_FAILED; + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RTC_OR_TIMER_ACCURACY_FAILURE, (U32)elapsedFPGATime, (U32)elapsedTime ); } - - result = RTC_SELF_TEST_STATE_COMPLETE; } return result;