Index: firmware/App/Controllers/RTC.c =================================================================== diff -u -r706a2e3f1718102cd34bd7f255901e5e7b8910cb -r6deface076f5ce0351b44055118efa1936771776 --- firmware/App/Controllers/RTC.c (.../RTC.c) (revision 706a2e3f1718102cd34bd7f255901e5e7b8910cb) +++ firmware/App/Controllers/RTC.c (.../RTC.c) (revision 6deface076f5ce0351b44055118efa1936771776) @@ -53,7 +53,7 @@ #define RTC_ACCURACY_TIMEOUT_TOLERANCE 1050 //ms #define NUM_OF_ITEMS_TO_READ 11 -#define TIMER_COUNTER_TO_REQUEST_READ 18 +#define TIMER_COUNTER_TO_REQUEST_READ 19 typedef enum RTC_Self_Test_States { @@ -74,13 +74,13 @@ typedef enum RTC_Exec_State { RTC_EXEC_STATE_WAIT_FOR_POST = 0, - RTC_EXEC_STATE_WAIT_FOR_COUNTER, + RTC_EXEC_STATE_IDLE, RTC_EXEC_STATE_READ, RTC_EXEC_STATE_WRITE, RTC_EXEC_STATE_FAULT } RTC_EXEC_STATE_T; -static struct RTCTimestamp +static struct RTCReadTimestamp { U16 seconds; U16 minutes; @@ -89,8 +89,19 @@ U16 weekdays; U16 months; U16 years; -} ts; +} read_ts; +static struct RTCWriteTimestamp +{ + U16 seconds; + U16 minutes; + U16 hours; + U16 days; + U16 weekdays; + U16 months; + U16 years; +} write_ts; + 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; @@ -102,13 +113,14 @@ static BOOL hasWriteTimestampRequested = FALSE; -// TODO: Increase the size of the buffer static U16 rxBuffer[NUM_OF_ITEMS_TO_READ]; static U16 txBuffer[] = {RTC_READ_FROM_REG0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000}; +// ********** Private functions prototype ********* + // ********** Private functions ********* -void serviceRTC( U16* buffer ) +static void serviceRTC( U16* buffer ) { switch ( RTCServiceState ) { @@ -141,7 +153,7 @@ } } -BOOL isRTCFunctional() +static BOOL isRTCFunctional() { BOOL hasTestPassed = TRUE; @@ -187,7 +199,7 @@ return hasTestPassed; } -U08 convertBCD2Decimal( U08 bcd ) +static U08 convertBCD2Decimal( U08 bcd ) { U08 bcdHigh; U08 bcdLow; @@ -201,7 +213,7 @@ return decimal; } -U08 convertDecimal2BCD( U08 decimal ) +static U08 convertDecimal2BCD( U08 decimal ) { U08 decimalHigh; U08 decimalLow; @@ -217,35 +229,40 @@ return bcd; } -U32 convertTime2Epoch() +static U32 convertTime2Epoch() { - // Either convert time to epoch or use the C time.h library - // to convert to epoch struct tm t; time_t epochTime; - t.tm_sec = ts.seconds; - t.tm_min = ts.minutes; - t.tm_hour= ts.hours; - t.tm_mday= ts.days; - t.tm_mon = ts.months; - t.tm_year= ts.years + 2000; - t.tm_isdst = -1; + 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; epochTime = mktime(&t); return (U32)epochTime; } -void updateTimestampStruct() +static void updateReadTimestampStruct() { - ts.seconds = rxBuffer[RTC_SECONDS_INDEX]; - ts.minutes = rxBuffer[RTC_MINUTES_INDEX]; - ts.hours = rxBuffer[RTC_HOURS_INDEX]; - ts.days = rxBuffer[RTC_DAYS_INDEX]; - ts.months = rxBuffer[RTC_MONTHS_INDEX]; - ts.years = 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; } // ********** Public functions ********** @@ -254,7 +271,6 @@ // Do nothing for now } -// TODO: Does this function need to return a bool? void setRTCTimestamp( U08 secs, U08 mins, U08 hours, U08 days, U08 months, U16 years ) { hasWriteTimestampRequested = TRUE; @@ -266,13 +282,13 @@ U16 decimalMonths = convertDecimal2BCD( months ); U16 decimalYears = convertDecimal2BCD( years - 2000 ); - ts.seconds = decimalSeconds; - ts.minutes = decimalMins; - ts.hours = decimalHours; - ts.days = decimalDays; - ts.weekdays = 0; // Weekdays will not be used - ts.months = decimalMonths; - ts.years = decimalYears; + 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; } SELF_TEST_STATUS_T execRTCSelfTest( void ) @@ -372,20 +388,20 @@ if ( RTCSelfTestState == RTC_SELF_TEST_STATE_COMPLETE ) { - RTCExecState = RTC_EXEC_STATE_WAIT_FOR_COUNTER; + RTCExecState = RTC_EXEC_STATE_IDLE; } break; - case RTC_EXEC_STATE_WAIT_FOR_COUNTER: + case RTC_EXEC_STATE_IDLE: if ( hasWriteTimestampRequested ) { // FOR DEBUGGING ONLY, REMOVE THIS CODE setRTCTimestamp(0, 10, 17, 8, 12, 2019); - U16 timestamp[8] = {RTC_WRITE_TO_REG3, ts.seconds, ts.minutes, ts.hours, - ts.days, ts.weekdays, ts.months, - ts.years}; + 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}; serviceRTC( ×tamp[0] ); @@ -416,7 +432,7 @@ // the read again timeCounter = 1; hasWriteTimestampRequested = FALSE; - RTCExecState = RTC_EXEC_STATE_WAIT_FOR_COUNTER; + RTCExecState = RTC_EXEC_STATE_IDLE; } break; @@ -433,12 +449,12 @@ //U08 testDate = (U08)rxBuffer[RTC_SECONDS_INDEX]; //convertBCD2Decimal( testDate ); - updateTimestampStruct(); + updateReadTimestampStruct(); convertTime2Epoch(); timeCounter = 1; - RTCExecState = RTC_EXEC_STATE_WAIT_FOR_COUNTER; + RTCExecState = RTC_EXEC_STATE_IDLE; } else {