From: Alan Cox Date: Sat, 25 Aug 2018 18:19:42 +0000 (+0100) Subject: ds1302: Move the rtc_read/write helpers to the common code X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=6973b9c3882a0f558293eebf8f69b71c29728e8c;p=FUZIX.git ds1302: Move the rtc_read/write helpers to the common code --- diff --git a/Kernel/dev/ds1302.c b/Kernel/dev/ds1302.c index 35c18135..e0adf3b4 100644 --- a/Kernel/dev/ds1302.c +++ b/Kernel/dev/ds1302.c @@ -9,6 +9,7 @@ #include #include #include +#include #include void ds1302_send_byte(uint8_t byte) @@ -83,3 +84,51 @@ uint8_t platform_rtc_secs(void) ds1302_read_clock(&buffer, 1); /* read out only the seconds value */ return uint8_from_bcd(buffer & 0x7F); /* mask off top bit (clock-halt) */ } + +static uint8_t rtc_buf[8]; + +/* Full RTC support (for read - no write yet) */ +int platform_rtc_read(void) +{ + uint16_t len = sizeof(struct cmos_rtc); + uint16_t y; + struct cmos_rtc cmos; + uint8_t *p = cmos.data.bytes; + + if (udata.u_count < len) + len = udata.u_count; + + ds1302_read_clock(rtc_buf, 7); + + y = rtc_buf[6]; + if (y > 0x70) + y = 0x1900 | y; + else + y = 0x2000 | y; + *p++ = y >> 8; + *p++ = y; + rtc_buf[4]--; /* 0 based */ + if ((rtc_buf[4] & 0x0F) > 9) /* Overflow case */ + rtc_buf[4] -= 0x06; + *p++ = rtc_buf[4]; /* Month */ + *p++ = rtc_buf[3]; /* Day of month */ + if ((rtc_buf[2] & 0x90) == 0x90) { /* 12hr mode, PM */ + /* Add 12 BCD */ + rtc_buf[2] += 0x12; + if ((rtc_buf[2] & 0x0F) > 9) /* Overflow case */ + rtc_buf[2] += 0x06; + } + *p++ = rtc_buf[2]; /* Hour */ + *p++ = rtc_buf[1]; /* Minute */ + *p = rtc_buf[0]; /* Second */ + cmos.type = CMOS_RTC_BCD; + if (uput(&cmos, udata.u_base, len) == -1) + return -1; + return len; +} + +int platform_rtc_write(void) +{ + udata.u_error = -EOPNOTSUPP; + return -1; +} diff --git a/Kernel/dev/ds1302.h b/Kernel/dev/ds1302.h index 7227777b..4315d9d6 100644 --- a/Kernel/dev/ds1302.h +++ b/Kernel/dev/ds1302.h @@ -5,6 +5,8 @@ void ds1302_init(void); uint8_t platform_rtc_secs(void); void ds1302_read_clock(uint8_t *buffer, uint8_t length); +int platform_rtc_read(void); +int platform_rtc_write(void); #ifdef _DS1302_PRIVATE /* consult the DS1302 datasheet for data format; diff --git a/Kernel/platform-sbcv2/main.c b/Kernel/platform-sbcv2/main.c index 2cae50cc..f01f9cc4 100644 --- a/Kernel/platform-sbcv2/main.c +++ b/Kernel/platform-sbcv2/main.c @@ -52,52 +52,10 @@ void platform_discard(void) } } -static uint8_t rtc_buf[8]; -int platform_rtc_read(void) -{ - uint16_t len = sizeof(struct cmos_rtc); - uint16_t y; - struct cmos_rtc cmos; - uint8_t *p = cmos.data.bytes; - - if (udata.u_count < len) - len = udata.u_count; - - ds1302_read_clock(rtc_buf, 7); - - y = rtc_buf[6]; - if (y > 0x70) - y = 0x1900 | y; - else - y = 0x2000 | y; - *p++ = y >> 8; - *p++ = y; - rtc_buf[4]--; /* 0 based */ - if ((rtc_buf[4] & 0x0F) > 9) /* Overflow case */ - rtc_buf[4] -= 0x06; - *p++ = rtc_buf[4]; /* Month */ - *p++ = rtc_buf[3]; /* Day of month */ - if ((rtc_buf[2] & 0x90) == 0x90) { /* 12hr mode, PM */ - /* Add 12 BCD */ - rtc_buf[2] += 0x12; - if ((rtc_buf[2] & 0x0F) > 9) /* Overflow case */ - rtc_buf[2] += 0x06; - } - *p++ = rtc_buf[2]; /* Hour */ - *p++ = rtc_buf[1]; /* Minute */ - *p = rtc_buf[0]; /* Second */ - cmos.type = CMOS_RTC_BCD; - if (uput(&cmos, udata.u_base, len) == -1) - return -1; - return len; -} - -int platform_rtc_write(void) -{ - udata.u_error = -EOPNOTSUPP; - return -1; -} +/* + * Logic for tickless system. If you have an RTC you can ignore this. + */ static uint8_t newticks = 0xFF; static uint8_t oldticks;