3
3
*/
4
4
5
5
#include " RTC.h"
6
+ #include < time.h>
6
7
7
8
am_hal_rtc_time_t hal_time;
9
+ am_hal_rtc_time_t alm_time;
8
10
9
- // String arrays to index Days and Months with the values returned by the RTC.
11
+ #define EPOCH_TIME 946684800 // UNIX Epoch time = 2000-01-01 00:00:00
12
+
13
+ // String arrays to index Days and Months with the values returned by the RTC.
10
14
char const *pcWeekday[] =
11
15
{
12
16
" Sunday" ,
@@ -37,31 +41,32 @@ char const *pcMonth[] =
37
41
// Constructor
38
42
APM3_RTC::APM3_RTC ()
39
43
{
40
- // Enable the XT for the RTC.
44
+ // Enable the XT for the RTC.
41
45
am_hal_clkgen_control (AM_HAL_CLKGEN_CONTROL_XTAL_START, 0 );
42
46
43
- // Select XT for RTC clock source
47
+ // Select XT for RTC clock source
44
48
am_hal_rtc_osc_select (AM_HAL_RTC_OSC_XT);
45
49
46
- // Enable the RTC.
50
+ // Enable the RTC.
47
51
am_hal_rtc_osc_enable ();
48
52
}
49
53
54
+
55
+ // Note: Order of parameters to change in v2.0.0.
50
56
void APM3_RTC::setTime (uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month, uint16_t year)
51
57
{
58
+ hal_time.ui32Weekday = am_util_time_computeDayofWeek (2000 + year, month, dayOfMonth); // computeDayofWeek is expecting 1 to 12 months
59
+ hal_time.ui32Century = 0 ;
60
+ hal_time.ui32Year = year;
61
+ hal_time.ui32Month = month; // HAL is expecting 1 to 12 months
62
+ hal_time.ui32DayOfMonth = dayOfMonth;
52
63
hal_time.ui32Hour = hour;
53
64
hal_time.ui32Minute = min;
54
65
hal_time.ui32Second = sec;
55
66
hal_time.ui32Hundredths = hund;
56
67
57
- hal_time.ui32DayOfMonth = dayOfMonth;
58
- hal_time.ui32Month = month; // HAL is expecting 1 to 12 months
59
- hal_time.ui32Year = year;
60
- hal_time.ui32Century = 0 ;
61
-
62
- hal_time.ui32Weekday = am_util_time_computeDayofWeek (2000 + year, month, dayOfMonth); // computeDayofWeek is expecting 1 to 12 months
63
-
64
68
am_hal_rtc_time_set (&hal_time); // Initialize the RTC with this date/time
69
+ getTime ();
65
70
}
66
71
67
72
// Takes the time from the last build and uses it as the current time
@@ -70,15 +75,39 @@ void APM3_RTC::setToCompilerTime()
70
75
{
71
76
// Get the current date/time from the compiler
72
77
// Alternatively, you can set these values manually
78
+ hal_time.ui32Weekday = am_util_time_computeDayofWeek (2000 + toVal (&__DATE__[9 ]), mthToIndex (&__DATE__[0 ]) + 1 , toVal (&__DATE__[4 ]));
79
+ hal_time.ui32Century = 0 ;
80
+ hal_time.ui32Year = toVal (&__DATE__[9 ]);
81
+ hal_time.ui32Month = mthToIndex (&__DATE__[0 ]) + 1 ; // Compiler ouputs months in 0-11.
82
+ hal_time.ui32DayOfMonth = toVal (&__DATE__[4 ]);
73
83
hal_time.ui32Hour = toVal (&__TIME__[0 ]);
74
84
hal_time.ui32Minute = toVal (&__TIME__[3 ]);
75
85
hal_time.ui32Second = toVal (&__TIME__[6 ]);
76
86
hal_time.ui32Hundredths = 00 ;
77
- hal_time.ui32Weekday = am_util_time_computeDayofWeek (2000 + toVal (&__DATE__[9 ]), mthToIndex (&__DATE__[0 ]) + 1 , toVal (&__DATE__[4 ]));
78
- hal_time.ui32DayOfMonth = toVal (&__DATE__[4 ]);
79
- hal_time.ui32Month = mthToIndex (&__DATE__[0 ]) + 1 ; // Compiler ouputs months in 0-11.
80
- hal_time.ui32Year = toVal (&__DATE__[9 ]);
87
+
88
+ am_hal_rtc_time_set (&hal_time); // Initialize the RTC with this date/time
89
+ getTime ();
90
+ }
91
+
92
+ void APM3_RTC::setEpoch (uint32_t ts)
93
+ {
94
+ if (ts < EPOCH_TIME) {
95
+ ts = EPOCH_TIME;
96
+ }
97
+
98
+ struct tm tm ;
99
+
100
+ time_t t = ts;
101
+ struct tm * tmp = gmtime (&t);
102
+ hal_time.ui32Weekday = 0 ;
81
103
hal_time.ui32Century = 0 ;
104
+ hal_time.ui32Year = tmp->tm_year - 100 ;
105
+ hal_time.ui32Month = tmp->tm_mon + 1 ;
106
+ hal_time.ui32DayOfMonth = tmp->tm_mday ;
107
+ hal_time.ui32Hour = tmp->tm_hour ;
108
+ hal_time.ui32Minute = tmp->tm_min ;
109
+ hal_time.ui32Second = tmp->tm_sec ;
110
+ hal_time.ui32Hundredths = 0 ;
82
111
83
112
am_hal_rtc_time_set (&hal_time); // Initialize the RTC with this date/time
84
113
}
@@ -87,17 +116,101 @@ void APM3_RTC::getTime()
87
116
{
88
117
am_hal_rtc_time_get (&hal_time);
89
118
119
+ weekday = hal_time.ui32Weekday ;
120
+ textWeekday = pcWeekday[hal_time.ui32Weekday ]; // Given a number (day of week) return the string that represents the name
121
+ year = hal_time.ui32Year ;
122
+ month = hal_time.ui32Month ; // HAL outputs months in 1 to 12 form
123
+ dayOfMonth = hal_time.ui32DayOfMonth ;
90
124
hour = hal_time.ui32Hour ;
91
125
minute = hal_time.ui32Minute ;
92
126
seconds = hal_time.ui32Second ;
93
127
hundredths = hal_time.ui32Hundredths ;
128
+ }
94
129
95
- month = hal_time. ui32Month ; // HAL outputs months in 1 to 12 form
96
- dayOfMonth = hal_time. ui32DayOfMonth ;
97
- year = hal_time. ui32Year ;
130
+ uint32_t APM3_RTC::getEpoch ()
131
+ {
132
+ am_hal_rtc_time_get (& hal_time) ;
98
133
99
- weekday = hal_time.ui32Weekday ;
100
- textWeekday = pcWeekday[hal_time.ui32Weekday ]; // Given a number (day of week) return the string that represents the name
134
+ struct tm tm ;
135
+
136
+ tm .tm_isdst = -1 ;
137
+ tm .tm_yday = 0 ;
138
+ tm .tm_wday = 0 ;
139
+ tm .tm_year = hal_time.ui32Year + 100 ; // Number of years since 1900.
140
+ tm .tm_mon = hal_time.ui32Month - 1 ; // mktime is expecting 0 to 11 months
141
+ tm .tm_mday = hal_time.ui32DayOfMonth ;
142
+ tm .tm_hour = hal_time.ui32Hour ;
143
+ tm .tm_min = hal_time.ui32Minute ;
144
+ tm .tm_sec = hal_time.ui32Second ;
145
+
146
+ return mktime (&tm );
147
+ }
148
+
149
+ void APM3_RTC::getAlarm ()
150
+ {
151
+ am_hal_rtc_alarm_get (&alm_time); // Get the RTC's alarm time
152
+
153
+ alarmWeekday = alm_time.ui32Weekday ;
154
+ alarmMonth = alm_time.ui32Month ; // HAL outputs months in 1 to 12 form
155
+ alarmDayOfMonth = alm_time.ui32DayOfMonth ;
156
+ alarmHour = alm_time.ui32Hour ;
157
+ alarmMinute = alm_time.ui32Minute ;
158
+ alarmSeconds = alm_time.ui32Second ;
159
+ alarmHundredths = alm_time.ui32Hundredths ;
160
+ }
161
+
162
+ // Note: Order of parameters to change in v2.0.0.
163
+ void APM3_RTC::setAlarm (uint8_t hour, uint8_t min, uint8_t sec, uint8_t hund, uint8_t dayOfMonth, uint8_t month)
164
+ {
165
+ alm_time.ui32Weekday = 0 ; // WIP
166
+ alm_time.ui32Month = month;
167
+ alm_time.ui32DayOfMonth = dayOfMonth;
168
+ alm_time.ui32Hour = hour;
169
+ alm_time.ui32Minute = min;
170
+ alm_time.ui32Second = sec;
171
+ alm_time.ui32Hundredths = hund;
172
+
173
+ am_hal_rtc_alarm_set (&alm_time, AM_HAL_RTC_ALM_RPT_DIS); // Initialize the RTC alarm with this date/time
174
+ }
175
+
176
+ // Sets the RTC alarm repeat interval.
177
+ /*
178
+ RTC alarm repeat intervals
179
+ 0: AM_HAL_RTC_ALM_RPT_DIS Alarm interrupt disabled
180
+ 1: AM_HAL_RTC_ALM_RPT_YR Interrupt every year
181
+ 2: AM_HAL_RTC_ALM_RPT_MTH Interrupt every month
182
+ 3: AM_HAL_RTC_ALM_RPT_WK Interrupt every week
183
+ 4: AM_HAL_RTC_ALM_RPT_DAY Interrupt every day
184
+ 5: AM_HAL_RTC_ALM_RPT_HR Interrupt every hour
185
+ 6: AM_HAL_RTC_ALM_RPT_MIN Interrupt every minute
186
+ 7: AM_HAL_RTC_ALM_RPT_SEC Interrupt every second
187
+ 8: AM_HAL_RTC_ALM_RPT_10TH Interrupt every 10th second (unused)
188
+ 9: AM_HAL_RTC_ALM_RPT_100TH Interrupt every 100th second (unused)
189
+ */
190
+ void APM3_RTC::setAlarmMode (uint8_t mode)
191
+ {
192
+ am_hal_rtc_alarm_interval_set (mode);
193
+ }
194
+
195
+ void APM3_RTC::attachInterrupt ()
196
+ {
197
+ // Enable the RTC interrupt.
198
+ am_hal_rtc_int_enable (AM_HAL_RTC_INT_ALM);
199
+
200
+ // Clear the RTC interrupt.
201
+ am_hal_rtc_int_clear (AM_HAL_RTC_INT_ALM);
202
+
203
+ // Enable RTC interrupts to the NVIC.
204
+ NVIC_EnableIRQ (RTC_IRQn);
205
+ }
206
+
207
+ void APM3_RTC::detachInterrupt ()
208
+ {
209
+ // Disable RTC interrupts to the NVIC.
210
+ NVIC_DisableIRQ (RTC_IRQn);
211
+
212
+ // Disable the RTC interrupt.
213
+ am_hal_rtc_int_disable (AM_HAL_RTC_INT_ALM);
101
214
}
102
215
103
216
// mthToIndex() converts a string indicating a month to an index value.
0 commit comments