Skip to content

Commit 1268e34

Browse files
committed
optimized reading the clock by reading rgister ounce + fix potential issue when tm_wday corrupted
1 parent 7540f37 commit 1268e34

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

STM32F4/libraries/RTClock/src/RTClock.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ void RTClock::setTime (time_t time_stamp) {
196196

197197
void RTClock::setTime (struct tm* tm_ptr) {
198198
rtc_enter_config_mode();
199+
if (tm_ptr->tm_year > 99)
200+
tm_ptr->tm_year = tm_ptr->tm_year % 100;
201+
tm_ptr->tm_wday = tm_ptr->tm_wday & 0x7;
199202
RTC_BASE->TR = ((tm_ptr->tm_hour / 10) << 20) | ((tm_ptr->tm_hour % 10) << 16) |
200203
((tm_ptr->tm_min / 10) << 12) | ((tm_ptr->tm_min % 10) << 8) |
201204
((tm_ptr->tm_sec / 10) << 4) | (tm_ptr->tm_sec % 10);
@@ -206,12 +209,14 @@ void RTClock::setTime (struct tm* tm_ptr) {
206209
}
207210

208211
time_t RTClock::getTime() {
209-
int years = 10 * ((RTC_BASE->DR & 0x00F00000) >> 20) + ((RTC_BASE->DR & 0x000F0000) >> 16);
210-
int months = 10 * ((RTC_BASE->DR & 0x00001000) >> 12) + ((RTC_BASE->DR & 0x00000F00) >> 8);
211-
int days = 10 * ((RTC_BASE->DR & 0x00000030) >> 4) + (RTC_BASE->DR & 0x000000F);
212-
int hours = 10 * ((RTC_BASE->TR & 0x00300000) >> 20) + ((RTC_BASE->TR & 0x000F0000) >> 16);
213-
int mins = 10 * ((RTC_BASE->TR & 0x00007000) >> 12) + ((RTC_BASE->TR & 0x0000F00) >> 8);
214-
int secs = 10 * ((RTC_BASE->TR & 0x00000070) >> 4) + (RTC_BASE->TR & 0x0000000F);
212+
uint32 dr_reg = RTC_BASE->DR;
213+
uint32 tr_reg = RTC_BASE->TR;
214+
int years = 10 * ((dr_reg & 0x00F00000) >> 20) + ((dr_reg & 0x000F0000) >> 16);
215+
int months = 10 * ((dr_reg & 0x00001000) >> 12) + ((dr_reg & 0x00000F00) >> 8);
216+
int days = 10 * ((dr_reg & 0x00000030) >> 4) + (dr_reg & 0x000000F);
217+
int hours = 10 * ((tr_reg & 0x00300000) >> 20) + ((tr_reg & 0x000F0000) >> 16);
218+
int mins = 10 * ((tr_reg & 0x00007000) >> 12) + ((tr_reg & 0x0000F00) >> 8);
219+
int secs = 10 * ((tr_reg & 0x00000070) >> 4) + (tr_reg & 0x0000000F);
215220
// seconds from 1970 till 1 jan 00:00:00 of the given year
216221
time_t t = (years + 30) * SECS_PER_DAY * 365;
217222
for (int i = 0; i < years; i++) {
@@ -232,12 +237,14 @@ time_t RTClock::getTime() {
232237
}
233238

234239
struct tm* RTClock::getTime(struct tm* tm_ptr) {
235-
tm_ptr->tm_year = 10 * ((RTC_BASE->DR & 0x00F00000) >> 20) + ((RTC_BASE->DR & 0x000F0000) >> 16);
236-
tm_ptr->tm_mon = 10 * ((RTC_BASE->DR & 0x00001000) >> 12) + ((RTC_BASE->DR & 0x00000F00) >> 8);
237-
tm_ptr->tm_mday = 10 * ((RTC_BASE->DR & 0x00000030) >> 4) + (RTC_BASE->DR & 0x000000F);
238-
tm_ptr->tm_hour = 10 * ((RTC_BASE->TR & 0x00300000) >> 20) + ((RTC_BASE->TR & 0x000F0000) >> 16);
239-
tm_ptr->tm_min = 10 * ((RTC_BASE->TR & 0x00007000) >> 12) + ((RTC_BASE->TR & 0x0000F00) >> 8);
240-
tm_ptr->tm_sec = 10 * ((RTC_BASE->TR & 0x00000070) >> 4) + (RTC_BASE->TR & 0x0000000F);
240+
uint32 dr_reg = RTC_BASE->DR;
241+
uint32 tr_reg = RTC_BASE->TR;
242+
tm_ptr->tm_year = 10 * ((dr_reg & 0x00F00000) >> 20) + ((dr_reg & 0x000F0000) >> 16);
243+
tm_ptr->tm_mon = 10 * ((dr_reg & 0x00001000) >> 12) + ((dr_reg & 0x00000F00) >> 8);
244+
tm_ptr->tm_mday = 10 * ((dr_reg & 0x00000030) >> 4) + (dr_reg & 0x000000F);
245+
tm_ptr->tm_hour = 10 * ((tr_reg & 0x00300000) >> 20) + ((tr_reg & 0x000F0000) >> 16);
246+
tm_ptr->tm_min = 10 * ((tr_reg & 0x00007000) >> 12) + ((tr_reg & 0x0000F00) >> 8);
247+
tm_ptr->tm_sec = 10 * ((tr_reg & 0x00000070) >> 4) + (tr_reg & 0x0000000F);
241248
return tm_ptr;
242249
}
243250

0 commit comments

Comments
 (0)