1
0
Fork 0
forked from len0rd/rockbox

Reworking of Archos RTC code to create a (slightly) more abstract RTC api

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8216 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Dave Chapman 2005-12-11 00:47:40 +00:00
parent 6ddfac0806
commit 790b365595
3 changed files with 61 additions and 26 deletions

View file

@ -47,21 +47,18 @@ struct tm *get_time(void)
{
#ifndef SIMULATOR
#ifdef CONFIG_RTC
char rtcbuf[8];
char rtcbuf[7];
/* We don't need the first byte, but we want the indexes in the
buffer to match the data sheet */
rtc_read_multiple(1, &rtcbuf[1], 7);
rtc_read_datetime(rtcbuf);
tm.tm_sec = ((rtcbuf[0] & 0x70) >> 4) * 10 + (rtcbuf[0] & 0x0f);
tm.tm_min = ((rtcbuf[1] & 0x70) >> 4) * 10 + (rtcbuf[1] & 0x0f);
tm.tm_hour = ((rtcbuf[2] & 0x30) >> 4) * 10 + (rtcbuf[2] & 0x0f);
tm.tm_wday = rtcbuf[3] & 0x07;
tm.tm_mday = ((rtcbuf[4] & 0x30) >> 4) * 10 + (rtcbuf[4] & 0x0f);
tm.tm_mon = ((rtcbuf[5] & 0x10) >> 4) * 10 + (rtcbuf[5] & 0x0f) - 1;
tm.tm_year = ((rtcbuf[6] & 0xf0) >> 4) * 10 + (rtcbuf[6] & 0x0f) + 100;
tm.tm_sec = ((rtcbuf[1] & 0x70) >> 4) * 10 + (rtcbuf[1] & 0x0f);
tm.tm_min = ((rtcbuf[2] & 0x70) >> 4) * 10 + (rtcbuf[2] & 0x0f);
tm.tm_hour = ((rtcbuf[3] & 0x30) >> 4) * 10 + (rtcbuf[3] & 0x0f);
tm.tm_mday = ((rtcbuf[5] & 0x30) >> 4) * 10 + (rtcbuf[5] & 0x0f);
tm.tm_mon = ((rtcbuf[6] & 0x10) >> 4) * 10 + (rtcbuf[6] & 0x0f) - 1;
tm.tm_year = ((rtcbuf[7] & 0xf0) >> 4) * 10 + (rtcbuf[7] & 0x0f) + 100;
tm.tm_wday = rtcbuf[4] & 0x07;
if(tm.tm_wday == 7)
tm.tm_wday = 0;
tm.tm_yday = 0; /* Not implemented for now */
tm.tm_isdst = -1; /* Not implemented for now */
#else
@ -86,22 +83,19 @@ int set_time(const struct tm *tm)
{
#ifdef CONFIG_RTC
int rc;
int tmp;
char rtcbuf[7];
if (valid_time(tm))
{
rc = rtc_write(1, ((tm->tm_sec/10) << 4) | (tm->tm_sec%10));
rc |= rtc_write(2, ((tm->tm_min/10) << 4) | (tm->tm_min%10));
rc |= rtc_write(3, ((tm->tm_hour/10) << 4) | (tm->tm_hour%10));
tmp = tm->tm_wday;
if(tmp == 0)
tmp = 7;
rc |= rtc_write(4, tmp);
rc |= rtc_write(5, ((tm->tm_mday/10) << 4) | (tm->tm_mday%10));
rc |= rtc_write(6, (((tm->tm_mon+1)/10) << 4) | ((tm->tm_mon+1)%10));
rc |= rtc_write(7, (((tm->tm_year-100)/10) << 4) | ((tm->tm_year-100)%10));
rtcbuf[0]=((tm->tm_sec/10) << 4) | (tm->tm_sec%10);
rtcbuf[1]=((tm->tm_min/10) << 4) | (tm->tm_min%10);
rtcbuf[2]=((tm->tm_hour/10) << 4) | (tm->tm_hour%10);
rtcbuf[3]=tm->tm_wday;
rtcbuf[4]=((tm->tm_mday/10) << 4) | (tm->tm_mday%10);
rtcbuf[5]=(((tm->tm_mon+1)/10) << 4) | ((tm->tm_mon+1)%10);
rtcbuf[6]=(((tm->tm_year-100)/10) << 4) | ((tm->tm_year-100)%10);
rc |= rtc_write(8, 0x80); /* Out=1, calibration = 0 */
rc = rtc_write_datetime(rtcbuf);
if(rc)
return -1;

View file

@ -249,4 +249,34 @@ int rtc_read_multiple(unsigned char address, unsigned char *buf, int numbytes)
i2c_end();
return ret;
}
#endif
int rtc_read_datetime(unsigned char* buf) {
int rc;
rc = rtc_read_multiple(1, buf, 7);
/* Adjust weekday */
if(buf[3] == 7)
buf[3]=0;
return rc;
}
int rtc_write_datetime(unsigned char* buf) {
int i;
int rc = 0;
/* Adjust weekday */
if(buf[3] == 0)
buf[3] = 7;
for (i = 0; i < 7 ; i++)
{
rc |= rtc_write(i+1, buf[i]);
}
rc |= rtc_write(8, 0x80); /* Out=1, calibration = 0 */
return rc;
}
#endif /* CONFIG_RTC */

View file

@ -22,7 +22,16 @@
#include <stdbool.h>
#ifdef CONFIG_RTC
/* Common functions for all targets */
void rtc_init(void);
int rtc_read_datetime(unsigned char* buf);
int rtc_write_datetime(unsigned char* buf);
#if CONFIG_RTC == RTC_M41ST84W
/* The RTC in the Archos devices is used for much more than just the clock
data */
int rtc_read(unsigned char address);
int rtc_read_multiple(unsigned char address, unsigned char *buf, int numbytes);
int rtc_write(unsigned char address, unsigned char value);
@ -35,6 +44,8 @@ bool rtc_check_alarm_started(bool release_alarm);
bool rtc_check_alarm_flag(void);
#endif /* HAVE_ALARM_MOD */
#endif /* RTC_M41ST84W */
#endif /* CONFIG_RTC */
#endif