From 9dc37754e0f08ca0dd5fca52df9335dfdf305677 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Thu, 5 Dec 2024 10:58:13 +0000 Subject: [PATCH] setdate: support decimal encoded RTC devices From: dennowiggle@github The purpose of this issue is to document a change that may be helpful for others. I modified function rtcwrite() in the setdate application to support the real time clock decimal format in addition to binary coded decimal. I tested on an eZ80 system I am working on and could write both BCD and DEC formats successfully. The eZ80 internal RTC supports both modes. Here is the updated rtcwrite() function for setdate.c based on the 0.4 code base. (merged with 0.5 changes - AC) Closes: #1122 --- Applications/util/setdate.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/Applications/util/setdate.c b/Applications/util/setdate.c index 7c71d9d64e..ffec98ea19 100644 --- a/Applications/util/setdate.c +++ b/Applications/util/setdate.c @@ -182,7 +182,7 @@ int rtcwrite(void) close(fd); return 0; } - if (rtc.type != CMOS_RTC_BCD) { + if (rtc.type != CMOS_RTC_BCD && rtc.type != CMOS_RTC_DEC) { close (fd); return 0; } @@ -191,14 +191,27 @@ int rtcwrite(void) tm = localtime(&t); p = rtc.data.bytes; - *p++ = bc((tm->tm_year + 1900) / 100); - *p++ = bc((tm->tm_year + 1900) % 100); - *p++ = bc(tm->tm_mon); - *p++ = bc(tm->tm_mday); - *p++ = bc(tm->tm_hour); - *p++ = bc(tm->tm_min); - *p++ = bc(tm->tm_sec); - *p = bc(tm->tm_wday); + if (rtc.type == CMOS_RTC_BCD) + { + *p++ = bc((tm->tm_year + 1900) / 100); + *p++ = bc((tm->tm_year + 1900) % 100); + *p++ = bc(tm->tm_mon); + *p++ = bc(tm->tm_mday); + *p++ = bc(tm->tm_hour); + *p++ = bc(tm->tm_min); + *p++ = bc(tm->tm_sec); + *p = bc(tm->tm_wday); + } else { /* CMOS_RTC_DEC */ + uint16_t year = tm->tm_year + 1900; + *p++ = year & 0xFF; + *p++ = year >> 8; + *p++ = tm->tm_mon; + *p++ = tm->tm_mday; + *p++ = tm->tm_hour; + *p++ = tm->tm_min; + *p++ = tm->tm_sec; + *p = tm->tm_wday; + } lseek(fd, 0, SEEK_SET); puts("writing\n");