Index: firmware/App/Controllers/RTC.c =================================================================== diff -u -r46ab59f1a24da466a153a8880f3e66460c80512b -r72311accbea39c95b0ee1efbd28dfbcf9fea74d1 --- firmware/App/Controllers/RTC.c (.../RTC.c) (revision 46ab59f1a24da466a153a8880f3e66460c80512b) +++ firmware/App/Controllers/RTC.c (.../RTC.c) (revision 72311accbea39c95b0ee1efbd28dfbcf9fea74d1) @@ -44,13 +44,14 @@ #define RTC_YEARS_INDEX 10 // *************** MibSPI values ************* // -#define MIBSPI_MAX_BUFFER_LENGTH 127U -#define MIBSPI_CONTINUOUS_MODE 4U -#define MIBSPI_CHIP_SELECT_ACTIVE 1U -#define MIBSPI_CHIP_SELECT_DEACTIVE 0U -#define MIBSPI_NO_WDELAY 0U -#define MIBSPI_LOCK_TG 0U -#define MIBSPI_DATA_FORMAT_ZERO 0U +#define MIBSPI_MAX_BUFFER_LENGTH 127U +#define MIBSPI_PREP_RAM_BUFFER_LENGTH 3U +#define MIBSPI_CONTINUOUS_MODE 4U +#define MIBSPI_CHIP_SELECT_ACTIVE 1U +#define MIBSPI_CHIP_SELECT_DEACTIVE 0U +#define MIBSPI_NO_WDELAY 0U +#define MIBSPI_LOCK_TG 0U +#define MIBSPI_DATA_FORMAT_ZERO 0U // This command puts RTC into read mode from // address 0 @@ -99,7 +100,8 @@ RTC_EXEC_STATE_WAIT_FOR_POST = 0, RTC_EXEC_STATE_IDLE, RTC_EXEC_STATE_PREP_RAM, - RTC_EXEC_STATE_RAM, + RTC_EXEC_STATE_WRITE_TO_RAM, + RTC_EXEC_STATE_READ_FROM_RAM, RTC_EXEC_STATE_READ, RTC_EXEC_STATE_WRITE, RTC_EXEC_STATE_FAULT @@ -138,14 +140,14 @@ static U32 timeCounter = 1; static U32 numberOfFailedRTCTransfers = 0; -static BOOL hasWriteToRTCRequested = FALSE; -static BOOL hasWriteToRAMRequested = FALSE; -static BOOL hasReadFromRAMRequested = TRUE; +static BOOL hasWriteToRTCRequested = FALSE; +static BOOL hasReadFromRTCRequested = TRUE; +static BOOL hasWriteToRAMRequested = FALSE; +static BOOL hasReadFromRAMRequested = TRUE; static U16 rxBuffer[MAXIMUM_NUM_OF_BUFFER]; static U16 txBuffer[MAXIMUM_NUM_OF_BUFFER]; - // ********** Private functions prototype ********* static BOOL serviceRTC( U16* buffer ); static BOOL isRTCFunctional(); @@ -162,7 +164,8 @@ static RTC_EXEC_STATE_T handleExecIdleState(); static RTC_EXEC_STATE_T handleExecReadState(); static RTC_EXEC_STATE_T handleExecPrepRAMState( U08* address ); -static RTC_EXEC_STATE_T handleExecRAMState( U08* data, U32 length ); +static RTC_EXEC_STATE_T handleExecWriteToRAMState( U08* data, U32 length ); +static RTC_EXEC_STATE_T handleExecReadFromRAMState( U32 length ); static RTC_EXEC_STATE_T handleExecWriteState(); static BOOL setMibSPIBufferLength(U16 length); @@ -239,11 +242,11 @@ data[0] = 4; data[1] = 5; data[2] = 6; - data[3] = 7; + data[3] = 71; data[4] = 8; data[5] = 9; - data[6] = 3; - data[7] = 1; + data[6] = 13; + data[7] = 115; length = sizeof(data); // FOR DEBUGGING PURPOSED ONLY @@ -273,11 +276,16 @@ RTCExecState = handleExecPrepRAMState( &address ); break; - case RTC_EXEC_STATE_RAM: + case RTC_EXEC_STATE_WRITE_TO_RAM: - RTCExecState = handleExecRAMState( &data[0], length ); + RTCExecState = handleExecWriteToRAMState( &data[0], length ); break; + case RTC_EXEC_STATE_READ_FROM_RAM: + + RTCExecState = handleExecReadFromRAMState( length ); + break; + case RTC_EXEC_STATE_WRITE: RTCExecState = handleExecWriteState(); @@ -298,6 +306,23 @@ break; } } + +U32 getRTCTimestamp() +{ + hasReadFromRTCRequested = TRUE; + + U32 timestamp = convertTime2Epoch() + + hasReadFromRTCRequested = FALSE; + + return timestamp; +} + +void writeToRAM( U08* address, U08* data, U32 length ) +{ + hasWriteToRAMRequested = TRUE; + +} // ********** Private functions ********* static BOOL serviceRTC( U16* buffer ) { @@ -495,17 +520,13 @@ { result = RTC_EXEC_STATE_WRITE; } - else if ( hasWriteToRAMRequested ) + else if ( hasWriteToRAMRequested || hasReadFromRAMRequested ) { result = RTC_EXEC_STATE_PREP_RAM; } - else if ( hasReadFromRAMRequested ) - { - 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 ) + else if ( timeCounter == TIMER_COUNTER_TO_REQUEST_READ || hasReadFromRTCRequested ) { // Reset the RTC Service commands RTCServiceState = RTC_SEND_COMMAND; @@ -537,13 +558,20 @@ txBuffer[1] = (U16)( *address / 10 ); txBuffer[2] = (U16)( *address % 10 ); - setMibSPIBufferLength(3); + setMibSPIBufferLength( MIBSPI_PREP_RAM_BUFFER_LENGTH ); BOOL isStatusOk = serviceRTC( &txBuffer[0] ); if ( RTCServiceState == RTC_READ_COMPLETE && isStatusOk ) { - result = RTC_EXEC_STATE_RAM; + if ( hasWriteToRAMRequested ) + { + result = RTC_EXEC_STATE_WRITE_TO_RAM; + } + else if ( hasReadFromRAMRequested ) + { + result = RTC_EXEC_STATE_READ_FROM_RAM; + } // Reset the RTC Service commands RTCServiceState = RTC_SEND_COMMAND; @@ -556,14 +584,15 @@ return result; } -static RTC_EXEC_STATE_T handleExecRAMState( U08* data, U32 length ) +static RTC_EXEC_STATE_T handleExecWriteToRAMState( U08* data, U32 length ) { - RTC_EXEC_STATE_T result = RTC_EXEC_STATE_RAM; + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WRITE_TO_RAM; - U08 i; - + // Set the buffer once if ( hasWriteToRAMRequested ) { + U08 i; + txBuffer[0] = RTC_WRITE_TO_RAM; for ( i = 0; i < length; i++ ) @@ -577,17 +606,38 @@ // Set the buffer once and done hasWriteToRAMRequested = FALSE; } - else if ( hasReadFromRAMRequested ) + + BOOL isStatusOk = serviceRTC( &txBuffer[0] ); + + if ( RTCServiceState == RTC_READ_COMPLETE && isStatusOk ) { + result = RTC_EXEC_STATE_IDLE; + } + else if ( RTCServiceState == RTC_READ_COMPLETE && ! isStatusOk ) + { + result = RTC_EXEC_STATE_FAULT; + } + + return result; +} + +static RTC_EXEC_STATE_T handleExecReadFromRAMState( U32 length ) +{ + RTC_EXEC_STATE_T result = RTC_EXEC_STATE_READ_FROM_RAM; + + // Set the buffer once + if ( hasReadFromRAMRequested ) + { + U08 i; + txBuffer[0] = RTC_READ_FROM_RAM; - for ( i = 0; i < MAXIMUM_NUM_OF_BUFFER - 1; i++ ) + for ( i = 0; i < length; i++ ) { txBuffer[i+1] = 0; } - // Max buffer length is 127, so 129 - 2 will be 127 - setMibSPIBufferLength( MAXIMUM_NUM_OF_BUFFER - 2 ); + setMibSPIBufferLength( length + 1 ); // Set the buffer to the maximum since // we don't know how many buffer are in a @@ -622,7 +672,16 @@ convertTime2Epoch(); - timeCounter = 1; + // 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; } } Index: firmware/App/Controllers/RTC.h =================================================================== diff -u -r526902d0a427e330e23ae5eaf433e89298643ac7 -r72311accbea39c95b0ee1efbd28dfbcf9fea74d1 --- firmware/App/Controllers/RTC.h (.../RTC.h) (revision 526902d0a427e330e23ae5eaf433e89298643ac7) +++ firmware/App/Controllers/RTC.h (.../RTC.h) (revision 72311accbea39c95b0ee1efbd28dfbcf9fea74d1) @@ -23,11 +23,14 @@ void execRTC( void ); void setRTCTimestamp( U08 secs, U08 mins, U08 hours, U08 days, U08 months, U16 years ); +U32 getRTCTimestamp(); SELF_TEST_STATUS_T execRTCSelfTest( void ); -void writeToRAM( U16 *buffer ); -void readFromRAM(); +void writeToRAM( U08* address, U08* data, U32 length ); +// TODO what to return? +void readFromRAM( U08* address, U32 length ); + #endif