aboutsummaryrefslogtreecommitdiff
path: root/lib/lufa/Projects/TempDataLogger/Lib/RTC.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lufa/Projects/TempDataLogger/Lib/RTC.c')
-rw-r--r--lib/lufa/Projects/TempDataLogger/Lib/RTC.c159
1 files changed, 159 insertions, 0 deletions
diff --git a/lib/lufa/Projects/TempDataLogger/Lib/RTC.c b/lib/lufa/Projects/TempDataLogger/Lib/RTC.c
new file mode 100644
index 000000000..a5291eaf3
--- /dev/null
+++ b/lib/lufa/Projects/TempDataLogger/Lib/RTC.c
@@ -0,0 +1,159 @@
1/*
2 Copyright (C) Dean Camera, 2017.
3
4 dean [at] fourwalledcubicle [dot] com
5 www.lufa-lib.org
6*/
7
8#include "RTC.h"
9
10#if defined(DUMMY_RTC)
11
12/** Current dummy RTC time and date */
13static volatile TimeDate_t DummyRTC_Count;
14
15void RTC_Init(void)
16{
17 DummyRTC_Count.Hour = 0;
18 DummyRTC_Count.Minute = 0;
19 DummyRTC_Count.Second = 0;
20 DummyRTC_Count.Day = 1;
21 DummyRTC_Count.Month = 1;
22 DummyRTC_Count.Year = 00;
23}
24
25void RTC_Tick500ms(void)
26{
27 static bool HalfSecondElapsed = false;
28
29 HalfSecondElapsed = !HalfSecondElapsed;
30 if (HalfSecondElapsed == false)
31 return;
32
33 if (++DummyRTC_Count.Second < 60)
34 return;
35
36 DummyRTC_Count.Second = 0;
37
38 if (++DummyRTC_Count.Minute < 60)
39 return;
40
41 DummyRTC_Count.Minute = 0;
42
43 if (++DummyRTC_Count.Hour < 24)
44 return;
45
46 DummyRTC_Count.Hour = 0;
47
48 static const char MonthLength[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
49 uint8_t DaysInMonth = MonthLength[DummyRTC_Count.Month - 1];
50
51 /* Check if we need to account for a leap year */
52 if ((DummyRTC_Count.Month == 2) &&
53 ((!(DummyRTC_Count.Year % 400)) || ((DummyRTC_Count.Year % 100) && !(DummyRTC_Count.Year % 4))))
54 {
55 DaysInMonth++;
56 }
57
58 if (++DummyRTC_Count.Day <= DaysInMonth)
59 return;
60
61 DummyRTC_Count.Day = 1;
62
63 if (++DummyRTC_Count.Month <= 12)
64 return;
65
66 DummyRTC_Count.Month = 1;
67 DummyRTC_Count.Year++;
68}
69
70bool RTC_SetTimeDate(const TimeDate_t* NewTimeDate)
71{
72 GlobalInterruptDisable();
73 DummyRTC_Count = *NewTimeDate;
74 GlobalInterruptEnable();
75
76 return true;
77}
78
79bool RTC_GetTimeDate(TimeDate_t* const TimeDate)
80{
81 GlobalInterruptDisable();
82 *TimeDate = DummyRTC_Count;
83 GlobalInterruptEnable();
84
85 return true;
86}
87
88#else
89
90void RTC_Init(void)
91{
92 /* Unused for a real external DS1307 RTC device */
93}
94
95void RTC_Tick500ms(void)
96{
97 /* Unused for a real external DS1307 RTC device */
98}
99
100bool RTC_SetTimeDate(const TimeDate_t* NewTimeDate)
101{
102 DS1307_DateTimeRegs_t NewRegValues;
103 const uint8_t WriteAddress = 0;
104
105 // Convert new time data to the DS1307's time register layout
106 NewRegValues.Byte1.Fields.TenSec = (NewTimeDate->Second / 10);
107 NewRegValues.Byte1.Fields.Sec = (NewTimeDate->Second % 10);
108 NewRegValues.Byte1.Fields.CH = false;
109 NewRegValues.Byte2.Fields.TenMin = (NewTimeDate->Minute / 10);
110 NewRegValues.Byte2.Fields.Min = (NewTimeDate->Minute % 10);
111 NewRegValues.Byte3.Fields.TenHour = (NewTimeDate->Hour / 10);
112 NewRegValues.Byte3.Fields.Hour = (NewTimeDate->Hour % 10);
113 NewRegValues.Byte3.Fields.TwelveHourMode = false;
114
115 // Convert new date data to the DS1307's date register layout
116 NewRegValues.Byte4.Fields.DayOfWeek = 0;
117 NewRegValues.Byte5.Fields.TenDay = (NewTimeDate->Day / 10);
118 NewRegValues.Byte5.Fields.Day = (NewTimeDate->Day % 10);
119 NewRegValues.Byte6.Fields.TenMonth = (NewTimeDate->Month / 10);
120 NewRegValues.Byte6.Fields.Month = (NewTimeDate->Month % 10);
121 NewRegValues.Byte7.Fields.TenYear = (NewTimeDate->Year / 10);
122 NewRegValues.Byte7.Fields.Year = (NewTimeDate->Year % 10);
123
124 // Write the new Time and Date into the DS1307
125 if (TWI_WritePacket(DS1307_ADDRESS, 10, &WriteAddress, sizeof(WriteAddress),
126 (uint8_t*)&NewRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError)
127 {
128 return false;
129 }
130
131 return true;
132}
133
134bool RTC_GetTimeDate(TimeDate_t* const TimeDate)
135{
136 DS1307_DateTimeRegs_t CurrentRegValues;
137 const uint8_t ReadAddress = 0;
138
139 // Read in the stored Time and Date from the DS1307
140 if (TWI_ReadPacket(DS1307_ADDRESS, 10, &ReadAddress, sizeof(ReadAddress),
141 (uint8_t*)&CurrentRegValues, sizeof(DS1307_DateTimeRegs_t)) != TWI_ERROR_NoError)
142 {
143 return false;
144 }
145
146 // Convert stored time value into decimal
147 TimeDate->Second = (CurrentRegValues.Byte1.Fields.TenSec * 10) + CurrentRegValues.Byte1.Fields.Sec;
148 TimeDate->Minute = (CurrentRegValues.Byte2.Fields.TenMin * 10) + CurrentRegValues.Byte2.Fields.Min;
149 TimeDate->Hour = (CurrentRegValues.Byte3.Fields.TenHour * 10) + CurrentRegValues.Byte3.Fields.Hour;
150
151 // Convert stored date value into decimal
152 TimeDate->Day = (CurrentRegValues.Byte5.Fields.TenDay * 10) + CurrentRegValues.Byte5.Fields.Day;
153 TimeDate->Month = (CurrentRegValues.Byte6.Fields.TenMonth * 10) + CurrentRegValues.Byte6.Fields.Month;
154 TimeDate->Year = (CurrentRegValues.Byte7.Fields.TenYear * 10) + CurrentRegValues.Byte7.Fields.Year;
155
156 return true;
157}
158
159#endif