Index: firmware/App/Controllers/RTC.c =================================================================== diff -u -r17cbafeeee71eef9a250142efbdf036c9970c2f4 -rfdeda091d43e144eb63f62189c25b916b2eb5c57 --- firmware/App/Controllers/RTC.c (.../RTC.c) (revision 17cbafeeee71eef9a250142efbdf036c9970c2f4) +++ firmware/App/Controllers/RTC.c (.../RTC.c) (revision fdeda091d43e144eb63f62189c25b916b2eb5c57) @@ -22,11 +22,11 @@ // ********** Definitions ********** -#define RTC_REG_1_12_HOUR_MODE 0X0004 // 12-hour or 24-hour mode -#define RTC_REG_1_PORO 0X0008 // Power On Reset Override -#define RTC_REG_1_CLK_STOPPED 0X0020 // RTC source clock -#define RTC_REG_1_UNUSED 0X0040 // Unused -#define RTC_REG_1_EXT_CLK_MODE 0X0080 // RTC external or normal mode +#define RTC_REG_1_12_HOUR_MODE 0X0004 // 12-hour or 24-hour mode +#define RTC_REG_1_PORO 0X0008 // Power On Reset Override +#define RTC_REG_1_CLK_STOPPED 0X0020 // RTC source clock +#define RTC_REG_1_UNUSED 0X0040 // Unused +#define RTC_REG_1_EXT_CLK_MODE 0X0080 // RTC external or normal mode #define RTC_REG_2_MASK 0X00FF #define RTC_REG_2_MSF 0X0080 // Minute or second interrupt @@ -58,6 +58,7 @@ #define MIBSPI_NO_WDELAY 0U #define MIBSPI_LOCK_TG 0U #define MIBSPI_DATA_FORMAT_ZERO 0U +#define MIBSPI_GROUP_ZEREO 0U #define MIBSPI_BUFFER_MODE_BIT_SHIFT 13U #define MIBSPI_CHIP_SELECT_BIT_SHIFT 12U @@ -75,14 +76,19 @@ #define RTC_WRITE_TO_RAM 0x003C // RTC RAM write #define RTC_READ_FROM_RAM 0x00BD // RTC RAM read +#define BUFFER_INDEX_0 0U +#define BUFFER_INDEX_1 1U +#define BUFFER_INDEX_2 2U +#define BUFFER_INDEX_3 3U +#define BUFFER_INDEX_4 4U + #define RTC_ACCURACY_TIMEOUT 1000 // ms #define RTC_ACCURACY_TIMEOUT_TOLERANCE 1050 // ms -#define TIMER_COUNTER_TO_REQUEST_READ 18 +#define TIMER_COUNTER_TO_REQUEST_READ 18 -#define MAX_ALLOWED_FAILED_RTC_TRANSFERS 2 +#define MAX_ALLOWED_FAILED_RTC_TRANSFERS 2 - typedef enum RTC_Self_Test_States { RTC_SELF_TEST_STATE_START = 0, @@ -111,7 +117,8 @@ RTC_EXEC_STATE_FAULT } RTC_EXEC_STATE_T; -static struct RTCReadTimestamp +#pragma pack(push,4) +typedef struct { U16 seconds; U16 minutes; @@ -120,58 +127,52 @@ U16 weekdays; U16 months; U16 years; -} read_ts; +} RTC_TIMESTAMP_T; +#pragma pack(pop) -static struct RTCWriteTimestamp -{ - U16 seconds; - U16 minutes; - U16 hours; - U16 days; - U16 weekdays; - U16 months; - U16 years; -} write_ts; // ********** private data ********** static RTC_SELF_TEST_STATE_T RTCSelfTestState = RTC_SELF_TEST_STATE_START; static RTC_GET_DATA_STATE_T RTCServiceState = RTC_SEND_COMMAND; static RTC_EXEC_STATE_T RTCExecState = RTC_EXEC_STATE_WAIT_FOR_POST; static SELF_TEST_STATUS_T RTCSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; +static RTC_TIMESTAMP_T RTCTimestampStruct; static U32 RTCSelfTestTimer = 0; static U32 RTCPreviousSecond = 0; +static U32 RAMBufferLength = 0; +static U32 lastEpochTime = 0; -static U32 RAMBufferLength = 0; - static U32 timeCounter = 1; static U32 numberOfFailedRTCTransfers = 0; static BOOL hasWriteToRTCRequested = FALSE; -static BOOL hasReadFromRTCRequested = FALSE; // Remove static BOOL hasWriteToRAMRequested = FALSE; static BOOL hasReadFromRAMRequested = FALSE; +static BOOL hasServiceRTCRequested = FALSE; -static U16 rxBuffer[MIBSPI_MAX_BUFFER_LENGTH + 1]; -static U16 txBuffer[MIBSPI_MAX_BUFFER_LENGTH + 1]; +static U16 rxBuffer[ MIBSPI_MAX_BUFFER_LENGTH + 1 ]; +static U16 txBuffer[ MIBSPI_MAX_BUFFER_LENGTH + 1 ]; -static U16 RAMBuffer[MIBSPI_MAX_BUFFER_LENGTH]; +static U16 RAMBuffer[ MIBSPI_MAX_BUFFER_LENGTH ]; // ********** Private function prototypes ********* -static BOOL serviceRTC( U16* buffer ); +static BOOL serviceRTC( U16* bufferTransmit, U16* bufferReceive, U16 bufferLength ); static BOOL isRTCFunctional(); static U08 convertBCD2Decimal( U08 bcd ); static U08 convertDecimal2BCD( U08 decimal ); static U32 convertTime2Epoch(); static void updateReadTimestampStruct(); static BOOL setMibSPIBufferLength( U16 length ); +static void prepBufferForReadCommand( U16 length ); // Puts the read command static RTC_SELF_TEST_STATE_T handleSelfTestStart(); static RTC_SELF_TEST_STATE_T handleSelfTestCheckCtrlRegs(); static RTC_SELF_TEST_STATE_T handleSelfTestCompareSeconds(); static RTC_SELF_TEST_STATE_T handleSelfTestCheckAccuracy(); +static RTC_EXEC_STATE_T handleExecWaitForPostState(); static RTC_EXEC_STATE_T handleExecIdleState(); static RTC_EXEC_STATE_T handleExecReadState(); static RTC_EXEC_STATE_T handleExecPrepRAMState(); @@ -190,29 +191,22 @@ { hasWriteToRTCRequested = TRUE; + // TODO: Remove these variables U16 decimalSeconds = convertDecimal2BCD( secs ); U16 decimalMins = convertDecimal2BCD( mins ); U16 decimalHours = convertDecimal2BCD( hours ); U16 decimalDays = convertDecimal2BCD( days ); U16 decimalMonths = convertDecimal2BCD( months ); U16 decimalYears = convertDecimal2BCD( years - 2000 ); - write_ts.seconds = decimalSeconds; - write_ts.minutes = decimalMins; - write_ts.hours = decimalHours; - write_ts.days = decimalDays; - write_ts.weekdays = 0; // Weekdays will not be used - write_ts.months = decimalMonths; - write_ts.years = decimalYears; - txBuffer[0] = RTC_WRITE_TO_REG3; - txBuffer[1] = write_ts.seconds; - txBuffer[2] = write_ts.minutes; - txBuffer[3] = write_ts.hours; - txBuffer[4] = write_ts.days; - txBuffer[5] = write_ts.weekdays; - txBuffer[6] = write_ts.months; - txBuffer[7] = write_ts.years; + txBuffer[1] = convertDecimal2BCD( secs ); + txBuffer[2] = convertDecimal2BCD( mins ); + txBuffer[3] = convertDecimal2BCD( hours ); + txBuffer[4] = convertDecimal2BCD( days ); + txBuffer[5] = 0; + txBuffer[6] = convertDecimal2BCD( months ); + txBuffer[7] = convertDecimal2BCD( years - 2000 ); } SELF_TEST_STATUS_T execRTCSelfTest( void ) @@ -257,18 +251,7 @@ { case RTC_EXEC_STATE_WAIT_FOR_POST: - if ( RTCSelfTestState == RTC_SELF_TEST_STATE_COMPLETE && - RTCSelfTestResult == SELF_TEST_STATUS_PASSED ) - { - RTCExecState = RTC_EXEC_STATE_IDLE; - // Reset the RTC Service commands - RTCServiceState = RTC_SEND_COMMAND; - } - else if ( RTCSelfTestState == RTC_SELF_TEST_STATE_COMPLETE && - RTCSelfTestResult == SELF_TEST_STATUS_FAILED ) - { - RTCExecState = RTC_EXEC_STATE_FAULT; - } + RTCExecState = handleExecWaitForPostState(); break; case RTC_EXEC_STATE_IDLE: @@ -314,13 +297,7 @@ U32 getRTCTimestamp() { - hasReadFromRTCRequested = TRUE; - - U32 timestamp = convertTime2Epoch(); - - hasReadFromRTCRequested = FALSE; - - return timestamp; + return lastEpochTime; } void writeToRAM( U16 address, U16* data, U32 length ) @@ -329,17 +306,17 @@ RAMBufferLength = length; - txBuffer[0] = RTC_PREP_RAM_READ_WRITE; - txBuffer[1] = ( address >> 8 ); - txBuffer[2] = ( address & 0x00FF ); - txBuffer[3] = RTC_WRITE_TO_RAM; + txBuffer[ BUFFER_INDEX_0 ] = RTC_PREP_RAM_READ_WRITE; + txBuffer[ BUFFER_INDEX_1 ] = ( address >> 8 ); + txBuffer[ BUFFER_INDEX_2 ] = ( address & 0x00FF ); + txBuffer[ BUFFER_INDEX_3 ] = RTC_WRITE_TO_RAM; U08 i; for ( i = 0; i < RAMBufferLength; i++ ) { // The first 3 elements in txBuffer // are filled - txBuffer[i + 4] = data[i]; + txBuffer[ i + BUFFER_INDEX_4 ] = data[ i ]; } } @@ -349,15 +326,15 @@ RAMBufferLength = length; - txBuffer[0] = RTC_PREP_RAM_READ_WRITE; - txBuffer[1] = ( address >> 8 ); - txBuffer[2] = ( address & 0x00FF ); - txBuffer[3] = RTC_READ_FROM_RAM; + txBuffer[ BUFFER_INDEX_0 ] = RTC_PREP_RAM_READ_WRITE; + txBuffer[ BUFFER_INDEX_1 ] = ( address >> 8 ); + txBuffer[ BUFFER_INDEX_2 ] = ( address & 0x00FF ); + txBuffer[ BUFFER_INDEX_3 ] = RTC_READ_FROM_RAM; U08 i; for ( i = 0; i < length; i++ ) { - txBuffer[ i + 4 ] = 0; + txBuffer[ i + BUFFER_INDEX_4 ] = 0; } } @@ -390,26 +367,39 @@ // ********** Private functions ********* -static BOOL serviceRTC( U16* buffer ) +static BOOL serviceRTC( U16* bufferTransmit, U16* bufferReceive, U16 bufferLength ) { BOOL result = FALSE; + if ( !hasServiceRTCRequested ) + { + RTCServiceState = RTC_SEND_COMMAND; + hasServiceRTCRequested = TRUE; + } + switch ( RTCServiceState ) { case RTC_SEND_COMMAND: - mibspiSetData(mibspiREG3, 0, buffer); + if ( setMibSPIBufferLength( bufferLength ) ) + { + mibspiSetData( mibspiREG3, MIBSPI_GROUP_ZEREO, bufferTransmit ); - mibspiTransfer(mibspiREG3, 0); + mibspiTransfer( mibspiREG3, MIBSPI_GROUP_ZEREO ); - RTCServiceState = RTC_WAIT_FOR_TRANSFER_AND_READ; + RTCServiceState = RTC_WAIT_FOR_TRANSFER_AND_READ; + } + else + { + RTCServiceState = RTC_SERVICE_COMPLETE; + } break; case RTC_WAIT_FOR_TRANSFER_AND_READ: - if ( mibspiIsTransferComplete(mibspiREG3, 0) ) + if ( mibspiIsTransferComplete( mibspiREG3, MIBSPI_GROUP_ZEREO ) ) { - mibspiGetData(mibspiREG3, 0, rxBuffer); + mibspiGetData( mibspiREG3, MIBSPI_GROUP_ZEREO, bufferReceive ); RTCServiceState = RTC_SERVICE_COMPLETE; @@ -424,14 +414,14 @@ // Transfer to RTC failed. This transfer // should be done in 50ms numberOfFailedRTCTransfers++; - - RTCServiceState = RTC_SEND_COMMAND; } + + hasServiceRTCRequested = FALSE; break; case RTC_SERVICE_COMPLETE: - // Done with reading and transfer do nothing + // Done with reading and transfer break; default: @@ -528,16 +518,15 @@ static U32 convertTime2Epoch() { - struct tm t; time_t epochTime; - t.tm_sec = read_ts.seconds; - t.tm_min = read_ts.minutes; - t.tm_hour = read_ts.hours; - t.tm_mday = read_ts.days; - t.tm_mon = read_ts.months - 1; - t.tm_year = read_ts.years + 2000 - 1970; + t.tm_sec = RTCTimestampStruct.seconds; + t.tm_min = RTCTimestampStruct.minutes; + t.tm_hour = RTCTimestampStruct.hours; + t.tm_mday = RTCTimestampStruct.days; + t.tm_mon = RTCTimestampStruct.months - 1; + t.tm_year = RTCTimestampStruct.years + 2000 - 1970; epochTime = mktime(&t); @@ -546,20 +535,20 @@ static void updateReadTimestampStruct() { - U16 decimalSeconds = convertBCD2Decimal( rxBuffer[RTC_SECONDS_INDEX] ); - U16 decimalMins = convertBCD2Decimal( rxBuffer[RTC_MINUTES_INDEX] ); - U16 decimalHours = convertBCD2Decimal( rxBuffer[RTC_HOURS_INDEX] ); - U16 decimalDays = convertBCD2Decimal( rxBuffer[RTC_DAYS_INDEX] ); - U16 decimalMonths = convertBCD2Decimal( rxBuffer[RTC_MONTHS_INDEX] ); - U16 decimalYears = convertBCD2Decimal( rxBuffer[RTC_YEARS_INDEX] ); + U16 decimalSeconds = convertBCD2Decimal( rxBuffer[ RTC_SECONDS_INDEX ] ); + U16 decimalMins = convertBCD2Decimal( rxBuffer[ RTC_MINUTES_INDEX ] ); + U16 decimalHours = convertBCD2Decimal( rxBuffer[ RTC_HOURS_INDEX ] ); + U16 decimalDays = convertBCD2Decimal( rxBuffer[ RTC_DAYS_INDEX ] ); + U16 decimalMonths = convertBCD2Decimal( rxBuffer[ RTC_MONTHS_INDEX ] ); + U16 decimalYears = convertBCD2Decimal( rxBuffer[ RTC_YEARS_INDEX ] ); - read_ts.seconds = decimalSeconds; - read_ts.minutes = decimalMins; - read_ts.hours = decimalHours; - read_ts.days = decimalDays; - read_ts.weekdays = 0; // Weekdays will not be used - read_ts.months = decimalMonths; - read_ts.years = decimalYears; + RTCTimestampStruct.seconds = decimalSeconds; + RTCTimestampStruct.minutes = decimalMins; + RTCTimestampStruct.hours = decimalHours; + RTCTimestampStruct.days = decimalDays; + RTCTimestampStruct.weekdays = 0; // Weekdays will not be used + RTCTimestampStruct.months = decimalMonths; + RTCTimestampStruct.years = decimalYears; } static BOOL setMibSPIBufferLength( U16 length ) @@ -602,25 +591,22 @@ while ( i < ( length - 1U ) ) { - mibspiRAM3->tx[i].control = (uint16)( (uint16)MIBSPI_CONTINUOUS_MODE << MIBSPI_BUFFER_MODE_BIT_SHIFT ) /* buffer mode */ + mibspiRAM3->tx[i].control = (uint16)( (uint16)MIBSPI_CONTINUOUS_MODE << MIBSPI_BUFFER_MODE_BIT_SHIFT ) /* buffer mode */ | (uint16)( (uint16)MIBSPI_CHIP_SELECT_ACTIVE << MIBSPI_CHIP_SELECT_BIT_SHIFT ) /* chip select hold */ - | (uint16)( (uint16)MIBSPI_NO_WDELAY << MIBSPI_NO_WDELAY_BIT_SHIT ) /* enable WDELAY */ - | (uint16)( (uint16)MIBSPI_LOCK_TG << MIBSPI_LOCK_TRANS_BIT_SHIFT ) /* lock transmission */ - | (uint16)( (uint16)MIBSPI_DATA_FORMAT_ZERO << MIBSPI_DATA_FORMAT_ZERO_BIT_SHIFT ) /* data format */ + | (uint16)( (uint16)MIBSPI_NO_WDELAY << MIBSPI_NO_WDELAY_BIT_SHIT ) /* enable WDELAY */ + | (uint16)( (uint16)MIBSPI_LOCK_TG << MIBSPI_LOCK_TRANS_BIT_SHIFT ) /* lock transmission */ + | (uint16)( (uint16)MIBSPI_DATA_FORMAT_ZERO << MIBSPI_DATA_FORMAT_ZERO_BIT_SHIFT ) /* data format */ /*SAFETYMCUSW 334 S MR:10.5 "LDRA Tool issue" */ | ((uint16)( ~((uint16)0xFFU ^ (uint16)CS_0)) & (uint16)0x00FFU ); /* chip select */ i++; } - mibspiRAM3->tx[i].control = (uint16)( (uint16)MIBSPI_CONTINUOUS_MODE << MIBSPI_BUFFER_MODE_BIT_SHIFT ) /* buffer mode */ - | (uint16)( (uint16)MIBSPI_CHIP_SELECT_DEACTIVE << MIBSPI_CHIP_SELECT_BIT_SHIFT ) /* chip select hold */ - | (uint16)( (uint16)MIBSPI_NO_WDELAY << MIBSPI_NO_WDELAY_BIT_SHIT ) /* enable WDELAY */ - | (uint16)( (uint16)MIBSPI_DATA_FORMAT_ZERO << MIBSPI_DATA_FORMAT_ZERO_BIT_SHIFT ) /* data format */ + mibspiRAM3->tx[i].control = (uint16)( (uint16)MIBSPI_CONTINUOUS_MODE << MIBSPI_BUFFER_MODE_BIT_SHIFT ) /* buffer mode */ + | (uint16)( (uint16)MIBSPI_CHIP_SELECT_DEACTIVE << MIBSPI_CHIP_SELECT_BIT_SHIFT ) /* chip select hold */ + | (uint16)( (uint16)MIBSPI_NO_WDELAY << MIBSPI_NO_WDELAY_BIT_SHIT ) /* enable WDELAY */ + | (uint16)( (uint16)MIBSPI_DATA_FORMAT_ZERO << MIBSPI_DATA_FORMAT_ZERO_BIT_SHIFT ) /* data format */ /*SAFETYMCUSW 334 S MR:10.5 "LDRA Tool issue" */ | ((uint16)( ~((uint16)0xFFU ^ (uint16)CS_0)) & (uint16)0x00FFU ); /* chip select */ - - //i++; - transferStatus = TRUE; } else @@ -631,31 +617,28 @@ return transferStatus; } -static RTC_EXEC_STATE_T handleExecWriteState() +static void prepBufferForReadCommand( U16 length ) { - RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WRITE; + txBuffer[ BUFFER_INDEX_0 ] = RTC_READ_FROM_REG0; - if ( setMibSPIBufferLength( RTC_TIMESTAMP_BUFFER_LENGTH ) ) + U08 i; + for ( i = 1; i < length; i++ ) { - BOOL isStatusOk = serviceRTC( &txBuffer[0] ); + txBuffer[ i ] = 0x0000; + } +} - if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) - { - // Reset the counter - timeCounter = 1; - hasWriteToRTCRequested = FALSE; +static RTC_EXEC_STATE_T handleExecWaitForPostState() +{ + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WAIT_FOR_POST; - // Reset the RTC Service commands - RTCServiceState = RTC_SEND_COMMAND; - - result = RTC_EXEC_STATE_IDLE; - } - else if ( RTCServiceState == RTC_SERVICE_COMPLETE && ! isStatusOk ) - { - result = RTC_EXEC_STATE_FAULT; - } + if ( RTCSelfTestState == RTC_SELF_TEST_STATE_COMPLETE && + RTCSelfTestResult == SELF_TEST_STATUS_PASSED ) + { + result = RTC_EXEC_STATE_IDLE; } - else + else if ( RTCSelfTestState == RTC_SELF_TEST_STATE_COMPLETE && + RTCSelfTestResult == SELF_TEST_STATUS_FAILED ) { result = RTC_EXEC_STATE_FAULT; } @@ -679,28 +662,11 @@ { result = RTC_EXEC_STATE_PREP_RAM; } - // If write to RTC has been requested, we don't have to read - // write must be finished first - else if ( timeCounter == TIMER_COUNTER_TO_REQUEST_READ || hasReadFromRTCRequested ) + else if ( timeCounter == TIMER_COUNTER_TO_REQUEST_READ ) { - // Reset the RTC Service commands - RTCServiceState = RTC_SEND_COMMAND; + prepBufferForReadCommand( RTC_GENERAL_BUFFER_LENGTH ); - if ( setMibSPIBufferLength( RTC_GENERAL_BUFFER_LENGTH ) ) - { - txBuffer[0] = RTC_READ_FROM_REG0; - - U08 i; - for ( i = 1; i < RTC_GENERAL_BUFFER_LENGTH; i++ ) - { - txBuffer[i] = 0x0000; - } - result = RTC_EXEC_STATE_READ; - } - else - { - result = RTC_EXEC_STATE_FAULT; - } + result = RTC_EXEC_STATE_READ; } else { @@ -710,28 +676,24 @@ return result; } -static RTC_EXEC_STATE_T handleExecPrepRAMState() +static RTC_EXEC_STATE_T handleExecWriteState() { - RTC_EXEC_STATE_T result = RTC_EXEC_STATE_PREP_RAM; + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WRITE; - // The transfer operations must be ok before moving on - if ( setMibSPIBufferLength( RTC_RAM_PREP_BUFFER_LENGTH ) ) + if ( setMibSPIBufferLength( RTC_TIMESTAMP_BUFFER_LENGTH ) ) { - BOOL isStatusOk = serviceRTC( &txBuffer[0] ); + BOOL isStatusOk = serviceRTC( &txBuffer[ BUFFER_INDEX_0 ], &rxBuffer[ BUFFER_INDEX_0 ], RTC_TIMESTAMP_BUFFER_LENGTH ); if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) { - if ( hasWriteToRAMRequested ) - { - result = RTC_EXEC_STATE_WRITE_TO_RAM; - } - else if ( hasReadFromRAMRequested ) - { - result = RTC_EXEC_STATE_READ_FROM_RAM; - } + // Reset the counter + timeCounter = 1; + hasWriteToRTCRequested = FALSE; // Reset the RTC Service commands RTCServiceState = RTC_SEND_COMMAND; + + result = RTC_EXEC_STATE_IDLE; } else if ( RTCServiceState == RTC_SERVICE_COMPLETE && ! isStatusOk ) { @@ -746,97 +708,90 @@ return result; } -static RTC_EXEC_STATE_T handleExecWriteToRAMState() +static RTC_EXEC_STATE_T handleExecPrepRAMState() { - RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WRITE_TO_RAM; + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_PREP_RAM; - if ( setMibSPIBufferLength( RAMBufferLength + 1 ) ) - { - BOOL isStatusOk = serviceRTC( &txBuffer[3] ); + BOOL isStatusOk = serviceRTC( &txBuffer[ BUFFER_INDEX_0 ], &RAMBuffer[ BUFFER_INDEX_0 ], RTC_RAM_PREP_BUFFER_LENGTH ); - if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) + if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) + { + if ( hasWriteToRAMRequested ) { - result = RTC_EXEC_STATE_IDLE; - - // Reset the RTC Service commands - RTCServiceState = RTC_SEND_COMMAND; - - hasWriteToRAMRequested = FALSE; + result = RTC_EXEC_STATE_WRITE_TO_RAM; } - else if ( RTCServiceState == RTC_SERVICE_COMPLETE && ! isStatusOk ) + else if ( hasReadFromRAMRequested ) { - result = RTC_EXEC_STATE_FAULT; + result = RTC_EXEC_STATE_READ_FROM_RAM; } } - else + else if ( RTCServiceState == RTC_SERVICE_COMPLETE && !isStatusOk ) { result = RTC_EXEC_STATE_FAULT; } return result; } -static RTC_EXEC_STATE_T handleExecReadFromRAMState() +static RTC_EXEC_STATE_T handleExecWriteToRAMState() { - RTC_EXEC_STATE_T result = RTC_EXEC_STATE_READ_FROM_RAM; + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WRITE_TO_RAM; - if ( setMibSPIBufferLength( RAMBufferLength + 1 ) ) + BOOL isStatusOk = serviceRTC( &txBuffer[ BUFFER_INDEX_3 ], &RAMBuffer[ BUFFER_INDEX_0 ], RAMBufferLength + 1 ); + + if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) { - BOOL isStatusOk = serviceRTC( &txBuffer[3] ); + result = RTC_EXEC_STATE_IDLE; - if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) - { - result = RTC_EXEC_STATE_IDLE; + hasWriteToRAMRequested = FALSE; + } + else if ( RTCServiceState == RTC_SERVICE_COMPLETE && !isStatusOk ) + { + result = RTC_EXEC_STATE_FAULT; + } - hasReadFromRAMRequested = FALSE; + return result; +} - // Reset the RTC Service commands - RTCServiceState = RTC_SEND_COMMAND; - } - else if ( RTCServiceState == RTC_SERVICE_COMPLETE && ! isStatusOk ) - { - result = RTC_EXEC_STATE_FAULT; - } +static RTC_EXEC_STATE_T handleExecReadFromRAMState() +{ + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_READ_FROM_RAM; + + BOOL isStatusOk = serviceRTC( &txBuffer[ BUFFER_INDEX_3 ], &RAMBuffer[ BUFFER_INDEX_0 ], RAMBufferLength + 1 ); + + if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) + { + result = RTC_EXEC_STATE_IDLE; + + hasReadFromRAMRequested = FALSE; } - else + else if ( RTCServiceState == RTC_SERVICE_COMPLETE && !isStatusOk ) { result = RTC_EXEC_STATE_FAULT; } return result; } - static RTC_EXEC_STATE_T handleExecReadState() { RTC_EXEC_STATE_T result = RTC_EXEC_STATE_READ; - BOOL isStatusOk = serviceRTC( &txBuffer[0] ); + BOOL isStatusOk = serviceRTC( &txBuffer[ BUFFER_INDEX_0 ], &rxBuffer[ BUFFER_INDEX_0 ], RTC_GENERAL_BUFFER_LENGTH ); if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) { if ( isRTCFunctional() ) { updateReadTimestampStruct(); - convertTime2Epoch(); + lastEpochTime = convertTime2Epoch(); - // If an off time read request happened, we shouldn't - // reset the counter for auto read - if ( timeCounter == TIMER_COUNTER_TO_REQUEST_READ ) - { - timeCounter = 1; - } - else if ( hasReadFromRTCRequested ) - { - hasReadFromRTCRequested = FALSE; - } - result = RTC_EXEC_STATE_IDLE; + timeCounter = 1; - // Reset the RTC Service commands - RTCServiceState = RTC_SEND_COMMAND; + result = RTC_EXEC_STATE_IDLE; } } - else if ( RTCServiceState == RTC_SERVICE_COMPLETE && ! isStatusOk ) + else if ( RTCServiceState == RTC_SERVICE_COMPLETE && !isStatusOk ) { result = RTC_EXEC_STATE_FAULT; } @@ -852,16 +807,10 @@ if ( setMibSPIBufferLength( RTC_GENERAL_BUFFER_LENGTH ) ) { // Reset the states - RTCServiceState = RTC_SEND_COMMAND; + //RTCServiceState = RTC_SEND_COMMAND; - txBuffer[0] = RTC_READ_FROM_REG0; + prepBufferForReadCommand( RTC_GENERAL_BUFFER_LENGTH ); - U08 i; - for (i=1; i< RTC_GENERAL_BUFFER_LENGTH; i++) - { - txBuffer[i] = 0x0000; - } - result = RTC_SELF_TEST_STATE_CHECK_CTRL_REGS; } else @@ -877,7 +826,7 @@ { RTC_SELF_TEST_STATE_T result = RTC_SELF_TEST_STATE_CHECK_CTRL_REGS; - serviceRTC( &txBuffer[0] ); + serviceRTC( &txBuffer[ BUFFER_INDEX_0 ], &rxBuffer[ BUFFER_INDEX_0 ], RTC_GENERAL_BUFFER_LENGTH ); if ( RTCServiceState == RTC_SERVICE_COMPLETE ) { @@ -902,7 +851,7 @@ { RTC_SELF_TEST_STATE_T result = RTC_SELF_TEST_STATE_COMPARE_SECONDS; - serviceRTC( &txBuffer[0] ); + serviceRTC( &txBuffer[ BUFFER_INDEX_0 ], &rxBuffer[ BUFFER_INDEX_0 ], RTC_GENERAL_BUFFER_LENGTH ); if ( RTCServiceState == RTC_SERVICE_COMPLETE ) {