Remove non-functional maintenance link check.
[pub/USBasp.git] / Projects / TempDataLogger / Lib / DS1307.c
index 40aba32..0fbd17b 100644 (file)
@@ -1,5 +1,5 @@
 /*
-     Copyright (C) Dean Camera, 2011.
+     Copyright (C) Dean Camera, 2012.
 
   dean [at] fourwalledcubicle [dot] com
            www.lufa-lib.org
@@ -7,13 +7,11 @@
 
 #include "DS1307.h"
 
-void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate)
+bool DS1307_SetTimeDate(const TimeDate_t* NewTimeDate)
 {
-#if defined(DUMMY_RTC)
-       return;
-#endif
-
+#if !defined(DUMMY_RTC)
        DS1307_DateTimeRegs_t NewRegValues;
+       const uint8_t         WriteAddress = 0;
 
        // Convert new time data to the DS1307's time register layout
        NewRegValues.Byte1.Fields.TenSec    = (NewTimeDate->Second / 10);
@@ -33,64 +31,37 @@ void DS1307_SetTimeDate(const TimeDate_t* NewTimeDate)
        NewRegValues.Byte6.Fields.Month     = (NewTimeDate->Month % 10);
        NewRegValues.Byte7.Fields.TenYear   = (NewTimeDate->Year / 10);
        NewRegValues.Byte7.Fields.Year      = (NewTimeDate->Year % 10);
-       
-       if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
-       {
-               // Must start writing to the first address within the device            
-               TWI_SendByte(0);
-
-               // Write time data to the first set of device registers
-               TWI_SendByte(NewRegValues.Byte1.IntVal);
-               TWI_SendByte(NewRegValues.Byte2.IntVal);
-               TWI_SendByte(NewRegValues.Byte3.IntVal);
 
-               // Write date data to the second set of device registers
-               TWI_SendByte(NewRegValues.Byte4.IntVal);
-               TWI_SendByte(NewRegValues.Byte5.IntVal);
-               TWI_SendByte(NewRegValues.Byte6.IntVal);
-               TWI_SendByte(NewRegValues.Byte7.IntVal);
-               
-               TWI_StopTransmission();
+       // Write the new Time and Date into the DS1307
+       if (TWI_WritePacket(DS1307_ADDRESS, 10, &WriteAddress, sizeof(WriteAddress),
+                          (uint8_t*)&NewRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError)
+       {
+               return false;
        }
+#endif
+
+       return true;
 }
 
-void DS1307_GetTimeDate(TimeDate_t* const TimeDate)
+bool DS1307_GetTimeDate(TimeDate_t* const TimeDate)
 {
 #if defined(DUMMY_RTC)
        TimeDate->Hour   = 1;
        TimeDate->Minute = 1;
        TimeDate->Second = 1;
-       
+
        TimeDate->Day    = 1;
        TimeDate->Month  = 1;
        TimeDate->Year   = 1;
-       
-       return;
-#endif
-
-       if (TWI_StartTransmission(DS1307_ADDRESS_WRITE, 10))
-       {
-               // Must start reading from the first address within the device
-               TWI_SendByte(0);
-               TWI_StopTransmission();
-       }
-
+#else
        DS1307_DateTimeRegs_t CurrentRegValues;
-       
-       if (TWI_StartTransmission(DS1307_ADDRESS_READ, 10))
-       {
-               // First set of registers store the current time
-               TWI_ReceiveByte(&CurrentRegValues.Byte1.IntVal, false);
-               TWI_ReceiveByte(&CurrentRegValues.Byte2.IntVal, false);
-               TWI_ReceiveByte(&CurrentRegValues.Byte3.IntVal, false);
+       const uint8_t         ReadAddress = 0;
 
-               // Second set of registers store the current date
-               TWI_ReceiveByte(&CurrentRegValues.Byte4.IntVal, false);
-               TWI_ReceiveByte(&CurrentRegValues.Byte5.IntVal, false);
-               TWI_ReceiveByte(&CurrentRegValues.Byte6.IntVal, false);
-               TWI_ReceiveByte(&CurrentRegValues.Byte7.IntVal, true);
-               
-               TWI_StopTransmission();
+       // Read in the stored Time and Date from the DS1307
+       if (TWI_ReadPacket(DS1307_ADDRESS, 10, &ReadAddress, sizeof(ReadAddress),
+                          (uint8_t*)&CurrentRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError)
+       {
+               return false;
        }
 
        // Convert stored time value into decimal
@@ -102,5 +73,8 @@ void DS1307_GetTimeDate(TimeDate_t* const TimeDate)
        TimeDate->Day    = (CurrentRegValues.Byte5.Fields.TenDay   * 10) + CurrentRegValues.Byte5.Fields.Day;
        TimeDate->Month  = (CurrentRegValues.Byte6.Fields.TenMonth * 10) + CurrentRegValues.Byte6.Fields.Month;
        TimeDate->Year   = (CurrentRegValues.Byte7.Fields.TenYear  * 10) + CurrentRegValues.Byte7.Fields.Year;
+#endif
+
+       return true;
 }