Index: RTC.c =================================================================== diff -u -rd352b607eeb20a7f06c32d776e5db2e35932ede2 -r63f1a64ffe1e8e98f6bde950a3f6542a053d32a3 --- RTC.c (.../RTC.c) (revision d352b607eeb20a7f06c32d776e5db2e35932ede2) +++ RTC.c (.../RTC.c) (revision 63f1a64ffe1e8e98f6bde950a3f6542a053d32a3) @@ -50,6 +50,16 @@ #define RTC_MONTHS_INDEX 9U #define RTC_YEARS_INDEX 10U +// Time and date acceptable ranges +#define MAX_ALLOWED_SECONDS 59U +#define MAX_ALLOWED_MINUTES 59U +#define MAX_ALLWOED_HOURS 23U +#define MAX_ALLOWED_DAYS 31U +#define MIN_ALLOWED_DAYS 1U +#define MAX_ALLOWED_MONTHS 12U +#define MIN_ALLOWED_MONTHS 1U +#define MAX_ALLOWED_YEARS 99U + #define BUFFER_INDEX_0 0U #define BUFFER_INDEX_1 1U #define BUFFER_INDEX_2 2U @@ -90,18 +100,18 @@ #define RTC_ACCURACY_TIMEOUT 1000U // ms #define RTC_ACCURACY_TIMEOUT_TOLERANCE 1050U // ms +#define RTC_PUBLISH_INTERVAL 18U // Task general counts #define TIMER_COUNTER_TO_REQUEST_READ 18U #define MAX_ALLOWED_FAILED_RTC_TRANSFERS 3U #define MAX_ALLOWED_RTC_RAM_BYTES 100U #define MAX_ALLOWED_RTC_RAM_ADDRESS 512U #define TEN 10U #define YEAR_2000 2000U - -#ifndef _VECTORCAST_ -#define EPOCH_YEAR 1970U -#else #define EPOCH_YEAR 1900U +#define YEAR_1900_TO_1970_SECONDS_DIFF 2208988800U + +#ifdef _VECTORCAST_ #define LOCAL_TO_GTM_TIME_CONVERSION 8U #endif @@ -159,6 +169,7 @@ static RTC_RAM_STATUS_T RTCRAMStatus = RTC_RAM_STATUS_IDLE; static RTC_RAM_STATE_T RTCRAMState = RTC_RAM_STATE_READY; static RTC_TIMESTAMP_T RTCTimestampStruct; +static RTC_TIMESTAMP_T RTCNewTimestampStruct; static U32 RTCSelfTestTimer = 0; static U32 RTCPreviousSecond = 0; // Previous second is used to compare seconds in POST @@ -173,6 +184,7 @@ static BOOL hasWriteToRAMRequested = FALSE; static BOOL hasReadFromRAMRequested = FALSE; static BOOL isRTCServiceOnEntry = FALSE; +static BOOL isTimestampBufferReady = FALSE; static U16 rxBuffer[ MIBSPI_MAX_BUFFER_LENGTH + 1 ]; static U16 txBuffer[ MIBSPI_MAX_BUFFER_LENGTH + 1 ]; @@ -232,20 +244,46 @@ * @param none * @return none *************************************************************************/ -void setRTCTimestamp( U08 secs, U08 mins, U08 hours, U08 days, U08 months, U32 years ) +BOOL setRTCTimestamp( U08 secs, U08 mins, U08 hours, U08 days, U08 months, U32 years ) { - hasWriteToRTCRequested = TRUE; - txBuffer[ 0 ] = RTC_WRITE_TO_REG0; - txBuffer[ 1 ] = 0x0000; - txBuffer[ 2 ] = 0x0000; - txBuffer[ 3 ] = 0x0000; - txBuffer[ 4 ] = convertDecimal2BCD( secs ); - txBuffer[ 5 ] = convertDecimal2BCD( mins ); - txBuffer[ 6 ] = convertDecimal2BCD( hours ); - txBuffer[ 7 ] = convertDecimal2BCD( days ); - txBuffer[ 8 ] = convertDecimal2BCD( 0 ); // Weekdays will not be used - txBuffer[ 9 ] = convertDecimal2BCD( months ); - txBuffer[ 10 ] = convertDecimal2BCD( years - YEAR_2000 ); + BOOL isDataOk = TRUE; + if ( secs > MAX_ALLOWED_SECONDS ) + { + isDataOk = FALSE; + } + else if ( mins > MAX_ALLOWED_MINUTES ) + { + isDataOk = FALSE; + } + else if ( hours > MAX_ALLWOED_HOURS ) + { + isDataOk = FALSE; + } + else if ( days < MIN_ALLOWED_DAYS || days > MAX_ALLOWED_DAYS ) + { + isDataOk = FALSE; + } + else if ( months < MIN_ALLOWED_MONTHS || months > MAX_ALLOWED_MONTHS ) + { + isDataOk = FALSE; + } + else if ( ( years - YEAR_2000 ) > MAX_ALLOWED_YEARS ) + { + isDataOk = FALSE; + } + else + { + hasWriteToRTCRequested = TRUE; + isTimestampBufferReady = FALSE; + RTCNewTimestampStruct.seconds = secs; + RTCNewTimestampStruct.minutes = mins; + RTCNewTimestampStruct.hours = hours; + RTCNewTimestampStruct.days = days; + RTCNewTimestampStruct.weekdays = 0; // Weekdays will not be used + RTCNewTimestampStruct.months = months; + RTCNewTimestampStruct.years = years; + } + return isDataOk; } /************************************************************************* @@ -755,7 +793,6 @@ { struct tm t; time_t epochTime; - t.tm_sec = RTCTimestampStruct.seconds; t.tm_min = RTCTimestampStruct.minutes; t.tm_hour = RTCTimestampStruct.hours; @@ -774,10 +811,11 @@ // the time is in GMT so 8 hours is subtracted from the hour // to simulate GMT for VectorCAST t.tm_hour = t.tm_hour - LOCAL_TO_GTM_TIME_CONVERSION; -#endif - epochTime = mktime(&t); - +#else + epochTime = mktime(&t); + epochTime = epochTime - YEAR_1900_TO_1970_SECONDS_DIFF; +#endif return (U32)epochTime; } @@ -973,8 +1011,24 @@ *************************************************************************/ static RTC_EXEC_STATE_T handleExecWriteState( void ) { + if ( !isTimestampBufferReady ) + { + txBuffer[ 0 ] = RTC_WRITE_TO_REG0; + txBuffer[ 1 ] = 0x0000; + 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 ) ); + isTimestampBufferReady = TRUE; + } + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WRITE; - BOOL isStatusOk = serviceRTC( txBuffer, rxBuffer, RTC_TIMESTAMP_BUFFER_LENGTH ); + BOOL isStatusOk = serviceRTC( txBuffer, rxBuffer, RTC_GENERAL_BUFFER_LENGTH ); if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) { @@ -1167,7 +1221,6 @@ { U32 RTCCurrentSecond = rxBuffer[ RTC_SECONDS_INDEX ]; RTCPreviousSecond = RTCCurrentSecond; - result = RTC_SELF_TEST_STATE_WAIT_FOR_FIRST_SECOND; } else