Index: RTC.c =================================================================== diff -u -r7d81e8ddfd35cd917c7baaf031ca3ad3e1c6f466 -rc0e7b9287da8b31bcb7d74c937f60ad263eeb710 --- RTC.c (.../RTC.c) (revision 7d81e8ddfd35cd917c7baaf031ca3ad3e1c6f466) +++ RTC.c (.../RTC.c) (revision c0e7b9287da8b31bcb7d74c937f60ad263eeb710) @@ -1,22 +1,23 @@ /************************************************************************** * -* Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +* Copyright (c) 2019-2021 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * -* @file RTC.c +* @file RTC.c * -* @author (last) Sean Nash -* @date (last) 01-Oct-2020 +* @author (last) Dara Navaei +* @date (last) 09-Nov-2021 * -* @author (original) Dara Navaei -* @date (original) 11-Jan-2020 +* @author (original) Dara Navaei +* @date (original) 11-Jan-2020 * ***************************************************************************/ #include "mibspi.h" #include "FPGA.h" +#include "MessageSupport.h" #include "OperationModes.h" #include "RTC.h" #include "SystemCommMessages.h" @@ -44,6 +45,8 @@ #define RTC_REG_3_BF_MASK 0x0008 ///< Battery status interrupt flag (0x0008) #define RTC_REG_3_BLF_MASK 0x0004 ///< Battery status low flag (0x0004) +#define RTC_STOP_CLK_COMMAND 0x0020 ///< RTC stop clock command. + // Indices used to check values read from RTC #define RTC_REG_1_INDEX 1U ///< RTC control register 1 index #define RTC_REG_2_INDEX 2U ///< RTC control register 2 index @@ -215,7 +218,7 @@ static BOOL isRTCFunctional( void ); static U08 convertBCD2Decimal( U08 bcd ); static U08 convertDecimal2BCD( U08 decimal ); -static U32 convertDateTime2Epoch( RTC_TIMESTAMP_T dateTime ); +static U32 convertDateTime2Epoch( RTC_TIMESTAMP_T dateTime ); static BOOL convertEpoch2DateTime( U32 epoch ); static void updateReadTimestampStruct( void ); static BOOL setMibSPIBufferLength( U16 length ); @@ -237,7 +240,7 @@ /*********************************************************************//** * @brief * The initRTC initializes the RTC module. - * @details Inputs: RTCSelfTestState, RTCSelfTestState + * @details Inputs: none * @details Outputs: RTCSelfTestState, RTCSelfTestState * @return none *************************************************************************/ @@ -291,8 +294,8 @@ } else { - hasWriteToRTCRequested = TRUE; - isTimestampBufferReady = FALSE; + hasWriteToRTCRequested = TRUE; + isTimestampBufferReady = FALSE; RTCNewTimestampStruct.seconds = secs; RTCNewTimestampStruct.minutes = mins; RTCNewTimestampStruct.hours = hours; @@ -1041,19 +1044,19 @@ { RTC_EXEC_STATE_T result = RTC_EXEC_STATE_IDLE; - if ( hasWriteToRTCRequested ) + if ( TRUE == hasWriteToRTCRequested ) { result = RTC_EXEC_STATE_WRITE; } - else if ( hasWriteToRAMRequested ) + else if ( TRUE == hasWriteToRAMRequested ) { result = RTC_EXEC_STATE_PREP_RAM; } - else if ( hasReadFromRAMRequested ) + else if ( TRUE == hasReadFromRAMRequested ) { result = RTC_EXEC_STATE_PREP_RAM; } - else if ( timeCounter == TIMER_COUNTER_TO_REQUEST_READ ) + else if ( TIMER_COUNTER_TO_REQUEST_READ == timeCounter ) { prepBufferForReadCommand( RTC_GENERAL_BUFFER_LENGTH ); result = RTC_EXEC_STATE_READ; @@ -1077,7 +1080,7 @@ *************************************************************************/ static RTC_EXEC_STATE_T handleExecWriteState( void ) { - if ( !isTimestampBufferReady ) + if ( FALSE == isTimestampBufferReady ) { txBuffer[ 0 ] = RTC_WRITE_TO_REG0; txBuffer[ 1 ] = 0x0000; @@ -1227,14 +1230,20 @@ { if ( isRTCFunctional() ) { + RTC_DATA_T data; + updateReadTimestampStruct(); lastEpochTime = convertDateTime2Epoch( RTCTimestampStruct ); timeCounter = 1; + data.epochTime = lastEpochTime; + #ifdef _DG_ - broadcastData( MSG_ID_RTC_EPOCH, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&lastEpochTime, sizeof( U32 ) ); -#else - broadcastData( MSG_ID_RTC_EPOCH, COMM_BUFFER_OUT_CAN_HD_BROADCAST, (U08*)&lastEpochTime, sizeof( U32 ) ); + broadcastData( MSG_ID_RTC_EPOCH, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( RTC_DATA_T ) ); #endif + +#ifdef _HD_ + broadcastData( MSG_ID_RTC_EPOCH, COMM_BUFFER_OUT_CAN_HD_BROADCAST, (U08*)&data, sizeof( RTC_DATA_T ) ); +#endif } result = RTC_EXEC_STATE_IDLE; @@ -1282,9 +1291,9 @@ RTC_SELF_TEST_STATE_T result = RTC_SELF_TEST_STATE_CHECK_CTRL_REGS; BOOL isStatusOk = serviceRTC( txBuffer, rxBuffer, RTC_GENERAL_BUFFER_LENGTH ); - if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) + if ( ( RTC_SERVICE_COMPLETE == RTCServiceState ) && ( TRUE == isStatusOk ) ) { - if ( isRTCFunctional() ) + if ( TRUE == isRTCFunctional() ) { U32 RTCCurrentSecond = rxBuffer[ RTC_SECONDS_INDEX ]; RTCPreviousSecond = RTCCurrentSecond; @@ -1320,9 +1329,9 @@ 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 ) && ( TRUE == isStatusOk ) ) + if ( ( RTC_SERVICE_COMPLETE == RTCServiceState ) && ( TRUE == isStatusOk ) ) { - if ( isRTCFunctional() ) + if ( TRUE == isRTCFunctional() ) { U32 RTCCurrentSecond = rxBuffer[ RTC_SECONDS_INDEX ]; @@ -1389,4 +1398,50 @@ return result; } + +/************************************************************************* + * TEST SUPPORT FUNCTIONS + *************************************************************************/ + + +/*********************************************************************//** + * @brief + * The testSetStopRTC sets the 5th bit of the first control register and + * sets its bit to 1 to stop the RTC clock. + * @details Inputs: none + * @details Outputs: hasWriteToRTCRequested, isTimestampBufferReady, txBuffer + * @return TRUE if the set was successful otherwise, FALSE + *************************************************************************/ +BOOL testSetStopRTC( void ) +{ + BOOL result = FALSE; + + if ( TRUE == isTestingActivated() ) + { + // Set write request so the exec state machine will set it + hasWriteToRTCRequested = TRUE; + isTimestampBufferReady = TRUE; + + // Set the latest time stamp to be written to the RTC since the stop command must be written + // to RTC anyways + txBuffer[ 0 ] = RTC_WRITE_TO_REG0; + // Read the RX buffer and get the first register. Mask off the MSB and or it with 0x0020 to set the + // start stop RTC bit to a 1 to stop the RTC clock. + txBuffer[ 1 ] = ( rxBuffer[ RTC_REG_1_INDEX ] & MASK_OFF_MSB ) | RTC_STOP_CLK_COMMAND; + txBuffer[ 2 ] = 0x0000; + txBuffer[ 3 ] = 0x0000; + txBuffer[ 4 ] = convertDecimal2BCD( RTCNewTimestampStruct.seconds ); + txBuffer[ 5 ] = convertDecimal2BCD( RTCNewTimestampStruct.minutes ); + txBuffer[ 6 ] = convertDecimal2BCD( RTCNewTimestampStruct.hours ); + txBuffer[ 7 ] = convertDecimal2BCD( RTCNewTimestampStruct.days ); + txBuffer[ 8 ] = convertDecimal2BCD( RTCNewTimestampStruct.weekdays ); // Weekdays will not be used + txBuffer[ 9 ] = convertDecimal2BCD( RTCNewTimestampStruct.months ); + txBuffer[ 10 ] = convertDecimal2BCD( ( RTCNewTimestampStruct.years - YEAR_2000 ) ); + + result = TRUE; + } + + return result; +} + /**@}*/