From 950e1c50903395939a6c7eef32197fa2897b459f Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Sat, 15 Oct 2022 02:37:06 +0200 Subject: [PATCH] Add BCD to decimal conversion of values read from RTC, and decimal to BCD when storing values. --- extdrv/rtc_pcf85363a.c | 42 ++++++++++++++++++++++++++++++++++ include/extdrv/rtc_pcf85363a.h | 14 ++++++++++++ 2 files changed, 56 insertions(+) diff --git a/extdrv/rtc_pcf85363a.c b/extdrv/rtc_pcf85363a.c index f95ffdf..15076ac 100644 --- a/extdrv/rtc_pcf85363a.c +++ b/extdrv/rtc_pcf85363a.c @@ -213,6 +213,7 @@ static int rtc_pcf85363_get_regs(struct rtc_pcf85363a_config* conf, * When performing a time read operation, all time registers have to be read at once as * a copy of the internal values is made by the device to a temporary buffer at the * beginning of the read for data coherency. + * This reads the raw values, which are stored in BCD format. */ int rtc_pcf85363_time_read(struct rtc_pcf85363a_config* conf, struct rtc_time* time) { @@ -227,6 +228,26 @@ int rtc_pcf85363_time_read(struct rtc_pcf85363a_config* conf, struct rtc_time* t return 0; } +/* Convertion of given time from BCD to decimal */ +void rtc_pcf85363_time_bcd_to_dec(struct rtc_time* time) +{ + time->sec = ((time->sec & 0x0F) + ((time->sec & 0xF0) >> 4) * 10); + time->min = ((time->min & 0x0F) + ((time->min & 0xF0) >> 4) * 10); + time->hour = ((time->hour & 0x0F) + ((time->hour & 0xF0) >> 4) * 10); + time->day = ((time->day & 0x0F) + ((time->day & 0xF0) >> 4) * 10); + time->month = ((time->month & 0x0F) + ((time->month & 0xF0) >> 4) * 10); + time->year = ((time->year & 0x0F) + ((time->year & 0xF0) >> 4) * 10); +} + +/* Time Read, converted to decimal */ +int rtc_pcf85363_time_read_dec(struct rtc_pcf85363a_config* conf, struct rtc_time* time) +{ + int ret = 0; + ret = rtc_pcf85363_time_read(conf, time); + rtc_pcf85363_time_bcd_to_dec(time); + return ret; +} + /* Time Write * A time write operation has to be done in one go for all time registers for time data * coherency. @@ -263,6 +284,27 @@ int rtc_pcf85363_time_write(struct rtc_pcf85363a_config* conf, struct rtc_time* return 0; } +/* Convertion of given time from decimal to BCD */ +void rtc_pcf85363_time_dec_to_bcd(struct rtc_time* time) +{ + time->sec = ((time->sec % 10) + ((time->sec / 10) << 4)); + time->min = ((time->min % 10) + ((time->min / 10) << 4)); + time->hour = ((time->hour % 10) + ((time->hour / 10) << 4)); + time->day = ((time->day % 10) + ((time->day / 10) << 4)); + time->month = ((time->month % 10) + ((time->month / 10) << 4)); + time->year = ((time->year % 10) + ((time->year / 10) << 4)); +} + +/* Time Write, from decimal */ +int rtc_pcf85363_time_write_dec(struct rtc_pcf85363a_config* conf, struct rtc_time* time) +{ + int ret = 0; + rtc_pcf85363_time_dec_to_bcd(time); + ret = rtc_pcf85363_time_write(conf, time); + return ret; +} + + /* Get one of the timestamps */ diff --git a/include/extdrv/rtc_pcf85363a.h b/include/extdrv/rtc_pcf85363a.h index 0d492fc..c833125 100644 --- a/include/extdrv/rtc_pcf85363a.h +++ b/include/extdrv/rtc_pcf85363a.h @@ -150,6 +150,20 @@ int rtc_pcf85363_time_read(struct rtc_pcf85363a_config* conf, struct rtc_time* t int rtc_pcf85363_time_write(struct rtc_pcf85363a_config* conf, struct rtc_time* time); +/* Convertion of given time from BCD to decimal */ +void rtc_pcf85363_time_bcd_to_dec(struct rtc_time* time); + +/* Convertion of given time from decimal to BCD */ +void rtc_pcf85363_time_dec_to_bcd(struct rtc_time* time); + + +/* Time Read, converted to decimal */ +int rtc_pcf85363_time_read_dec(struct rtc_pcf85363a_config* conf, struct rtc_time* time); + +/* Time Write, from decimal */ +int rtc_pcf85363_time_write_dec(struct rtc_pcf85363a_config* conf, struct rtc_time* time); + + /* Get one of the timestamps */ int rtc_pcf85363_get_timestamp(struct rtc_pcf85363a_config* conf, -- 2.43.0