Index: RTC.c =================================================================== diff -u -r02f990b33c415985dcd9c765c49604226cb371e3 -rcc035ecce50596a70d2489e0f591e769d5f6977c --- RTC.c (.../RTC.c) (revision 02f990b33c415985dcd9c765c49604226cb371e3) +++ RTC.c (.../RTC.c) (revision cc035ecce50596a70d2489e0f591e769d5f6977c) @@ -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,6 +100,7 @@ #define RTC_ACCURACY_TIMEOUT 1000U // ms #define RTC_ACCURACY_TIMEOUT_TOLERANCE 1050U // ms +#define RTC_PUBLISH_INTERVAL 1U // seconds #define TIMER_COUNTER_TO_REQUEST_READ 18U #define MAX_ALLOWED_FAILED_RTC_TRANSFERS 3U @@ -159,12 +170,14 @@ 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 static U32 RAMBufferLength = 0; static U32 lastEpochTime = 0; // Last value that has been converted to epoch static U32 previousTransferLength = 0; +static U32 RTCPublishIntervalCounter = 1; static U32 timeCounter = 1; static U32 numberOfFailedRTCTransfers = 1; @@ -173,6 +186,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 +246,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; } /************************************************************************* @@ -917,7 +957,7 @@ } else if ( RTCSelfTestState == RTC_SELF_TEST_STATE_COMPLETE ) { - result = RTC_EXEC_STATE_FAULT; + result = RTC_EXEC_STATE_IDLE; } return result; @@ -950,7 +990,7 @@ { result = RTC_EXEC_STATE_PREP_RAM; } - else if ( timeCounter == TIMER_COUNTER_TO_REQUEST_READ ) + else if ( (timeCounter == TIMER_COUNTER_TO_REQUEST_READ) ) { prepBufferForReadCommand( RTC_GENERAL_BUFFER_LENGTH ); result = RTC_EXEC_STATE_READ; @@ -974,8 +1014,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 ) { @@ -1168,7 +1224,6 @@ { U32 RTCCurrentSecond = rxBuffer[ RTC_SECONDS_INDEX ]; RTCPreviousSecond = RTCCurrentSecond; - result = RTC_SELF_TEST_STATE_WAIT_FOR_FIRST_SECOND; } else Index: RTC.h =================================================================== diff -u -r9e60eaa3f9b152acef1fa1c99bea1fc255813f59 -rcc035ecce50596a70d2489e0f591e769d5f6977c --- RTC.h (.../RTC.h) (revision 9e60eaa3f9b152acef1fa1c99bea1fc255813f59) +++ RTC.h (.../RTC.h) (revision cc035ecce50596a70d2489e0f591e769d5f6977c) @@ -41,7 +41,7 @@ void execRTC( void ); -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 ); U32 getRTCTimestamp( void );