Index: firmware/App/Controllers/RTC.c =================================================================== diff -u -r090b7303b8f6487879147191a3e5d6d783ed4af8 -r17cbafeeee71eef9a250142efbdf036c9970c2f4 --- firmware/App/Controllers/RTC.c (.../RTC.c) (revision 090b7303b8f6487879147191a3e5d6d783ed4af8) +++ firmware/App/Controllers/RTC.c (.../RTC.c) (revision 17cbafeeee71eef9a250142efbdf036c9970c2f4) @@ -21,6 +21,7 @@ #include "mibspi.h" // ********** 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 @@ -49,6 +50,7 @@ #define RTC_YEARS_INDEX 10 // *************** MibSPI values ************* // + #define MIBSPI_MAX_BUFFER_LENGTH 127U #define MIBSPI_CONTINUOUS_MODE 4U #define MIBSPI_CHIP_SELECT_ACTIVE 1U @@ -64,10 +66,12 @@ #define MIBSPI_DATA_FORMAT_ZERO_BIT_SHIFT 8U #define MIBSPI_BUFFER_TRANS_BIT_SHIFT 8U #define RTC_RAM_PREP_BUFFER_LENGTH 3U +#define RTC_TIMESTAMP_BUFFER_LENGTH 8U +#define RTC_GENERAL_BUFFER_LENGTH 11U #define RTC_READ_FROM_REG0 0x00A0 // RTC read from address 0 -#define RTC_WRITE_TO_REG3 0x0023 -#define RTC_PREP_RAM_READ_WRITE 0x003A // RTC cmd prior to RAM ops +#define RTC_WRITE_TO_REG3 0x0023 // Seconds register +#define RTC_PREP_RAM_READ_WRITE 0x003A // RTC cmd prior to RAM ops #define RTC_WRITE_TO_RAM 0x003C // RTC RAM write #define RTC_READ_FROM_RAM 0x00BD // RTC RAM read @@ -77,7 +81,6 @@ #define TIMER_COUNTER_TO_REQUEST_READ 18 #define MAX_ALLOWED_FAILED_RTC_TRANSFERS 2 -#define GENERAL_NUM_OF_ITEMS_TO_READ 11 typedef enum RTC_Self_Test_States @@ -129,6 +132,7 @@ 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; @@ -144,7 +148,7 @@ static U32 numberOfFailedRTCTransfers = 0; static BOOL hasWriteToRTCRequested = FALSE; -static BOOL hasReadFromRTCRequested = FALSE; +static BOOL hasReadFromRTCRequested = FALSE; // Remove static BOOL hasWriteToRAMRequested = FALSE; static BOOL hasReadFromRAMRequested = FALSE; @@ -153,7 +157,8 @@ static U16 RAMBuffer[MIBSPI_MAX_BUFFER_LENGTH]; -// ********** Private functions prototype ********* +// ********** Private function prototypes ********* + static BOOL serviceRTC( U16* buffer ); static BOOL isRTCFunctional(); static U08 convertBCD2Decimal( U08 bcd ); @@ -175,6 +180,7 @@ static RTC_EXEC_STATE_T handleExecWriteState(); // ********** Public functions ********** + void initRTC() { // Do nothing for now @@ -198,6 +204,15 @@ 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; } SELF_TEST_STATUS_T execRTCSelfTest( void ) @@ -349,30 +364,32 @@ U16* getDataFromRAM( U16 address, U32 length ) { U16* pointer; + // The read operation must have been finished - if ( ! hasReadFromRAMRequested ) + if ( !hasReadFromRAMRequested ) { if ( length == RAMBufferLength ) { U08 i; - for ( i = 0; i < length; i++) + + for ( i = 0; i < length; i++ ) { - RAMBuffer[i] = rxBuffer[i + 1]; + RAMBuffer[ i ] = rxBuffer[ i + 1 ]; } - pointer = &RAMBuffer[0]; + pointer = &RAMBuffer[ 0 ]; } else { pointer = 0; } - } return pointer; } // ********** Private functions ********* + static BOOL serviceRTC( U16* buffer ) { BOOL result = FALSE; @@ -475,6 +492,7 @@ // Set the alarm for register 3 hasTestPassed = FALSE; } + return hasTestPassed; } @@ -601,7 +619,7 @@ | ((uint16)( ~((uint16)0xFFU ^ (uint16)CS_0)) & (uint16)0x00FFU ); /* chip select */ - i++; + //i++; transferStatus = TRUE; } @@ -617,21 +635,29 @@ { RTC_EXEC_STATE_T result = RTC_EXEC_STATE_WRITE; - // FOR DEBUGGING ONLY, REMOVE THIS CODE - setRTCTimestamp(0, 10, 17, 8, 12, 2019); + if ( setMibSPIBufferLength( RTC_TIMESTAMP_BUFFER_LENGTH ) ) + { + BOOL isStatusOk = serviceRTC( &txBuffer[0] ); - U16 timestamp[8] = {RTC_WRITE_TO_REG3, write_ts.seconds, write_ts.minutes, write_ts.hours, - write_ts.days, write_ts.weekdays, write_ts.months, - write_ts.years}; + if ( RTCServiceState == RTC_SERVICE_COMPLETE && isStatusOk ) + { + // Reset the counter + timeCounter = 1; + hasWriteToRTCRequested = FALSE; - serviceRTC( ×tamp[0] ); + // Reset the RTC Service commands + RTCServiceState = RTC_SEND_COMMAND; - if ( RTCServiceState == RTC_SERVICE_COMPLETE ) + result = RTC_EXEC_STATE_IDLE; + } + else if ( RTCServiceState == RTC_SERVICE_COMPLETE && ! isStatusOk ) + { + result = RTC_EXEC_STATE_FAULT; + } + } + else { - // Reset the counter - timeCounter = 1; - hasWriteToRTCRequested = FALSE; - RTCExecState = RTC_EXEC_STATE_IDLE; + result = RTC_EXEC_STATE_FAULT; } return result; @@ -660,12 +686,12 @@ // Reset the RTC Service commands RTCServiceState = RTC_SEND_COMMAND; - if ( setMibSPIBufferLength(GENERAL_NUM_OF_ITEMS_TO_READ) ) + if ( setMibSPIBufferLength( RTC_GENERAL_BUFFER_LENGTH ) ) { txBuffer[0] = RTC_READ_FROM_REG0; U08 i; - for (i=1; i< GENERAL_NUM_OF_ITEMS_TO_READ; i++) + for ( i = 1; i < RTC_GENERAL_BUFFER_LENGTH; i++ ) { txBuffer[i] = 0x0000; } @@ -823,15 +849,15 @@ { RTC_SELF_TEST_STATE_T result = RTC_SELF_TEST_STATE_START; - if ( setMibSPIBufferLength( GENERAL_NUM_OF_ITEMS_TO_READ ) ) + if ( setMibSPIBufferLength( RTC_GENERAL_BUFFER_LENGTH ) ) { // Reset the states RTCServiceState = RTC_SEND_COMMAND; txBuffer[0] = RTC_READ_FROM_REG0; U08 i; - for (i=1; i< GENERAL_NUM_OF_ITEMS_TO_READ; i++) + for (i=1; i< RTC_GENERAL_BUFFER_LENGTH; i++) { txBuffer[i] = 0x0000; } Index: firmware/App/Tasks/TaskGeneral.c =================================================================== diff -u -r090b7303b8f6487879147191a3e5d6d783ed4af8 -r17cbafeeee71eef9a250142efbdf036c9970c2f4 --- firmware/App/Tasks/TaskGeneral.c (.../TaskGeneral.c) (revision 090b7303b8f6487879147191a3e5d6d783ed4af8) +++ firmware/App/Tasks/TaskGeneral.c (.../TaskGeneral.c) (revision 17cbafeeee71eef9a250142efbdf036c9970c2f4) @@ -50,15 +50,21 @@ //execSystemCommRx(); // FOR DEBUGGING PURPOSES ONLY REMOVE + // Used 120 and 200 so it will not read or + // write during POST static U16 test = 0; - if (test == 120) + /*if (test == 120) { static U16 data[5] = {7, 8, 9, 1, 22}; writeToRAM(8, &data[0], 5); } if (test == 200) { readFromRAM(8, 5); + }*/ + if (test == 140) + { + setRTCTimestamp(0, 46, 13, 17, 12, 2019); } test++; // FOR DEBUGGING PURPOSES ONLY REMOVE