dave_albert wrote:
>The RTC day-of-week (DOW) register does not appear to automatically
>reflect the correct day of the week based on a correctly set month,
>day-of-month, year in the RTC. Is this calculation supposed to be
>done automatically in the RTC or is it just a modulo 7 counter (i.e.
>do I need to set it to the correct day of week when I set the RTC date?)
>
>
>
>
Correct, you are responsible for setting DOW and DOY. This is a good
reason to use newlib, it has those calculations built into the mktime()
functions.
For example, I have a TTY menu system to field configure the unit. This
is a snippet of taking an ASCII time string ("HH:MM:SS YYYY:mm:DD") into
a unix epoch (via recordTimeStuff()), then setting the clock via newlib
setttimeofday():
========== begin menu func =================
time_t timeNow;
char strResult[64];
void setClockTime (const action_params_t * param)
{
struct timeval newTime;
newTime.tv_sec = timeNow;
newTime.tv_usec = 0;
settimeofday(&newTime, 0);
}
static bool extractTimeInfo (void)
{// return True if something went wrong.
struct tm nowTime;
char * ptr;
// extract hours.
if ((ptr = strtok(strResult, ":")) == NULL) return True;
nowTime.tm_hour = atoi(ptr);
if ((nowTime.tm_hour < 0) || (nowTime.tm_hour > 23)) return True;
// extract minutes.
if ((ptr = strtok(NULL, ":")) == NULL) return True;
nowTime.tm_min = atoi(ptr);
if ((nowTime.tm_min < 0) || (nowTime.tm_min > 59)) return True;
// extract seconds.
if ((ptr = strtok(NULL, " ")) == NULL) return True;
nowTime.tm_sec = atoi(ptr);
if ((nowTime.tm_sec < 0) || (nowTime.tm_sec > 59)) return True;
// extract year.
if ((ptr = strtok(NULL, "-")) == NULL) return True;
nowTime.tm_year = (atoi(ptr)-1900);
if ((nowTime.tm_year < 0) || (nowTime.tm_year > 137)) return True;
// extract month.
if ((ptr = strtok(NULL, "-")) == NULL) return True;
nowTime.tm_mon = (atoi(ptr))-1;
if ((nowTime.tm_mon < 0) || (nowTime.tm_mon > 200)) return True;
// extract day.
if ((ptr = strtok(NULL, " ")) == NULL) return True;
nowTime.tm_mday = atoi(ptr);
if ((nowTime.tm_mday < 0) || (nowTime.tm_mday > 200)) return True;
// create unix epoch.
timeNow = mktime(&nowTime);
return False;
}
static void recordTimeStuff (void)
{// just parse into time_t for now, will SET later.
if (extractTimeInfo()) {
emitErrorPrompt(eStrBadTimeDateValue);
}
}
============== snip ====================
This is the newlib stub I wrote to settimeofday():
============ begin setttimeofday ===========
int settimeofday (const struct timeval * newDate, const struct timezone
* tz)
{
struct tm newTime;
time_t epochTime = newDate->tv_sec;
// convert from unix seconds into discrete time elements.
#ifdef HAS_CLOCK
localtime_r(&epochTime, &newTime);
newTime.tm_year += 1900;
newTime.tm_mon += 1;
newTime.tm_yday += 1;
writeRTC (&newTime);
#endif
return 0;
}
============ snip =====================
TomW
--
Tom Walsh - WN3L - Embedded Systems Consultant
http://openhardware.net, http://cyberiansoftware.com
"Windows? No thanks, I have work to do..."
----------------------------------------------------