From 9f7d75bbc0826718d536e134cbe868830dfd0aa1 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Thu, 11 Apr 2019 15:51:18 +0200 Subject: [PATCH] Add rtc_pcf85363_daytime_to_seconds and rtc_pcf85363_seconds_to_daytime --- extdrv/rtc_pcf85363a.c | 32 ++++++++++++++++++++++++++++++++ include/extdrv/rtc_pcf85363a.h | 23 ++++++++++------------- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/extdrv/rtc_pcf85363a.c b/extdrv/rtc_pcf85363a.c index 47d99a1..f95ffdf 100644 --- a/extdrv/rtc_pcf85363a.c +++ b/extdrv/rtc_pcf85363a.c @@ -297,6 +297,38 @@ int rtc_pcf85363_time_to_str(struct rtc_time* time, char* str, int size) return len; } +/* Return the number of seconds of the curent day */ +uint32_t rtc_pcf85363_daytime_to_seconds(struct rtc_time* time) +{ + uint32_t seconds = ((time->sec & 0x0F) + ((time->sec & 0xF0) >> 4) * 10); + seconds += ((time->min & 0x0F) + ((time->min & 0xF0) >> 4) * 10) * 60; + seconds += ((time->hour & 0x0F) + ((time->hour & 0xF0) >> 4) * 10) * 3600; + return seconds; +} + +/* Update the hours, minutes and seconds fields of the given time according to the number + * of seconds given in second argument. + * LPC1224 uses a Cortex M0, which has no modulo or division instruction, so this solution + * limits the calls to the ROM division routines to 5. + */ +void rtc_pcf85363_seconds_to_daytime(struct rtc_time* time, uint32_t seconds) +{ + uint32_t sec_tmp, min_tmp, hour_tmp; + hour_tmp = seconds / 3600; + seconds -= 3600 * hour_tmp; + min_tmp = seconds / 60; + sec_tmp = seconds - 60 * min_tmp; + + time->hour = (hour_tmp / 10) << 4; + time->hour += (hour_tmp - (time->hour >> 4) * 10); + + time->min = (min_tmp / 10) << 4; + time->min += (min_tmp - (time->min >> 4) * 10); + + time->sec = (sec_tmp / 10) << 4; + time->sec += (sec_tmp - (time->sec >> 4) * 10); +} + /* Compare two times * Return 0 if they are equal, 1 if t1 > t2, and -1 if t1 < t2 */ diff --git a/include/extdrv/rtc_pcf85363a.h b/include/extdrv/rtc_pcf85363a.h index bb57643..0d492fc 100644 --- a/include/extdrv/rtc_pcf85363a.h +++ b/include/extdrv/rtc_pcf85363a.h @@ -143,25 +143,15 @@ struct rtc_pcf85363a_config { }; -/* Time Read - * 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. - */ +/* Time Read from RTC */ int rtc_pcf85363_time_read(struct rtc_pcf85363a_config* conf, struct rtc_time* time); -/* Time Write - * A time write operation has to be done in one go for all time registers for time data - * coherency. - * Also, when performing a time write, the STOP bit must be set and the prescalers should - * be cleared. - */ +/* Time Write to RTC */ int rtc_pcf85363_time_write(struct rtc_pcf85363a_config* conf, struct rtc_time* time); -/* Get one of the timestamps - */ +/* Get one of the timestamps */ int rtc_pcf85363_get_timestamp(struct rtc_pcf85363a_config* conf, struct rtc_timestamp* tstmp, uint8_t timestamp_num); /* Erase timestamps */ @@ -172,6 +162,13 @@ int rtc_pcf85363_erase_timestamps(struct rtc_pcf85363a_config* conf); /* Print the time to a usable string, in a unix format which "date" could understand. */ int rtc_pcf85363_time_to_str(struct rtc_time* time, char* str, int size); +/* Return the number of seconds of the curent day */ +uint32_t rtc_pcf85363_daytime_to_seconds(struct rtc_time* time); + +/* Update the hours, minutes and seconds fields of the given time according to the number + * of seconds given in second argument. */ +void rtc_pcf85363_seconds_to_daytime(struct rtc_time* time, uint32_t seconds); + /* Compare two times * Return 0 if they are equal, 1 if t1 > t2, and -1 if t1 < t2 */ -- 2.43.0