Index: RTC.c =================================================================== diff -u -rd7988c07f520940859b019645f7bb29d3202197d -r19a5e3f400a939c2d186fac59c89960088f2a4d1 --- RTC.c (.../RTC.c) (revision d7988c07f520940859b019645f7bb29d3202197d) +++ RTC.c (.../RTC.c) (revision 19a5e3f400a939c2d186fac59c89960088f2a4d1) @@ -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; } /************************************************************************* @@ -756,7 +794,6 @@ { struct tm t; time_t epochTime; - t.tm_sec = RTCTimestampStruct.seconds; t.tm_min = RTCTimestampStruct.minutes; t.tm_hour = RTCTimestampStruct.hours; @@ -775,10 +812,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; } @@ -857,14 +895,14 @@ | (uint16)( (uint16)MIBSPI_NO_WDELAY << MIBSPI_NO_WDELAY_BIT_SHIFT_10 ) /* enable WDELAY */ | (uint16)( (uint16)MIBSPI_LOCK_TG << MIBSPI_LOCK_TRANS_BIT_SHIFT_11 ) /* lock transmission */ | (uint16)( (uint16)MIBSPI_DATA_FORMAT_ZERO << MIBSPI_DATA_FORMAT_ZERO_BIT_SHIFT_8 ) /* data format */ - | ((uint16)( ~((uint16)0xFFU ^ (uint16)CS_0)) & (uint16)0x00FFU ); /* chip select */ + | ((uint16)( ~((uint16)0xFFU ^ (uint16)CS_1)) & (uint16)0x00FFU ); /* chip select */ i++; } mibspiRAM3->tx[i].control = (uint16)( (uint16)MIBSPI_CONTINUOUS_MODE << MIBSPI_BUFFER_MODE_BIT_SHIFT_13 ) /* buffer mode */ | (uint16)( (uint16)MIBSPI_CHIP_SELECT_DEACTIVE << MIBSPI_CHIP_SELECT_BIT_SHIFT_12 ) /* chip select hold */ | (uint16)( (uint16)MIBSPI_NO_WDELAY << MIBSPI_NO_WDELAY_BIT_SHIFT_10 ) /* enable WDELAY */ | (uint16)( (uint16)MIBSPI_DATA_FORMAT_ZERO << MIBSPI_DATA_FORMAT_ZERO_BIT_SHIFT_8 ) /* data format */ - | ((uint16)( ~((uint16)0xFFU ^ (uint16)CS_0)) & (uint16)0x00FFU ); /* chip select */ + | ((uint16)( ~((uint16)0xFFU ^ (uint16)CS_1)) & (uint16)0x00FFU ); /* chip select */ transferStatus = TRUE; } @@ -917,7 +955,7 @@ } else if ( RTCSelfTestState == RTC_SELF_TEST_STATE_COMPLETE ) { - result = RTC_EXEC_STATE_FAULT; + result = RTC_EXEC_STATE_IDLE; } return result; @@ -974,8 +1012,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 ) { Index: RTC.h =================================================================== diff -u -rcc035ecce50596a70d2489e0f591e769d5f6977c -r19a5e3f400a939c2d186fac59c89960088f2a4d1 --- RTC.h (.../RTC.h) (revision cc035ecce50596a70d2489e0f591e769d5f6977c) +++ RTC.h (.../RTC.h) (revision 19a5e3f400a939c2d186fac59c89960088f2a4d1) @@ -55,6 +55,6 @@ RTC_RAM_STATE_T getRTCRAMState( void ); -void getDataFromRAM( U16* externalBuffer, U32 length ); +void getDataFromRAM( U08* externalBuffer, U32 length ); #endif