Forums

Real time clock functions on the DSP.

Started by William C Bonner December 1, 2006
It's been quite a while since I've asked this question, and I'm now much
more fluent in programming on the DSP.
I'm trying to keep track of time on a TMS320C6713 DSP. I'd like to use
the standard C library calls if possible, but I don't know how to hook
their backend up properly to keep time. If I'm connected and running
the emulator over the JTAG I get values that are from the host
computer. Here's a code snippet and the two examples of output.

time_t timer;
time(&timer);
fprintf(tx," clock() = %u\r\n",clock());
fprintf(tx," GMT Time = %s",asctime(gmtime(&timer)));
fprintf(tx,"Local Time = %s",asctime(localtime(&timer)));
Not connected:
clock() = 4177131769
GMT Time = Wed Nov 24 02:35:04 2032
Local Time = Tue Nov 23 20:35:04 2032

Connected and running code loaded via jtag:
clock() = 0
GMT Time = Thu Nov 30 22:49:37 2006
Local Time = Thu Nov 30 16:49:37 2006
To further complicate things, Timer0 on my system is being used to
generate a pulse to an external device, and I'm not in a position to be
able to rewire the connection to Timer1. I can use Timer1 if that's
what I need to do.
Because it took me quite a while to get around to coming up with a
functional clock, I thought I'd post my solution, and see if people can
tell me any real problems I'll get with this method. Basically I've got
a clock counter that is incremented every millisecond, I've copied the
time() and clock() function declarations from the TI source, then
overridden what they do inside, and added two functions to be able to
start and stop the clock by setting up the interrupts, and using the
timer resource of my choosing.

The strange thing that I'm running into is when I call settime() I can't
seem to get the time set accurately. My device clock seems to get set at
least 2 minutes ahead of tell it to be set, or in another case as much
as 40 minutes ahead. I think I'd understand if the clock was set
behind, because then it would simply be tht the interrupt counter is
running ahead of the output system, but I'm looking for understanding on
why it's running ahead. I have a routine that uses this chunk of code:

struct tm UTC;
UTC.tm_year = 0;
UTC.tm_mon = 0;
UTC.tm_mday = 1;
UTC.tm_hour = 0;
UTC.tm_min = 0;
UTC.tm_sec = 0;
UTC.tm_wday = 0;
UTC.tm_yday = 0;
UTC.tm_isdst = 0;
int millisec = 0;
if (2 < sscanf(InputBuffer,"%04d-%02d-%02dT%02d:%02d:%02d.03dZ",
&(UTC.tm_year),
&(UTC.tm_mon),
&(UTC.tm_mday),
&(UTC.tm_hour),
&(UTC.tm_min),
&(UTC.tm_sec),
&millisec))
{
UTC.tm_year -= 1900;
UTC.tm_mon -= 1;
#ifdef _TMS320C6X // Support for compiling on the DSP
settime(mktime(&UTC));
#endif
}


volatile unsigned long int sample_clock_counter = 0;
extern "C" {
interrupt void clock_isr(void)
{
if (sample_clock_counter == ULONG_MAX)
sample_clock_counter = 0;
else
sample_clock_counter++;
}
_DATA_ACCESS TZ _tz {
0, /* daylight */
0, /* timezone */
"UCT", /* tzname */
"DST", /* dstname */
};
volatile time_t time_offset = 0;
_CODE_ACCESS time_t settime(time_t timer)
{
time_offset = timer;
return(time_offset);
}
_CODE_ACCESS time_t time(time_t *timer)
{
time_t result = (time_t) sample_clock_counter / 1000 + time_offset;
if(timer)
*timer = result;
return(result);
}
_CODE_ACCESS clock_t clock(void)
{
return((clock_t) sample_clock_counter);
}
} // end of extern "C"
TIMER_Handle hTimer1 = (TIMER_Handle) INV;
void RTCStart(void)
{
/******************************************************************
Timer Configuration for Measurement Period
******************************************************************/
if (hTimer1 == INV)
{
hTimer1 = TIMER_open(TIMER_DEV1, TIMER_OPEN_RESET); // open
timer and set to default
}
if (hTimer1 != INV)
{
TIMER_configArgs(hTimer1,
0x00000281, /* Control Register (CTL) */ // CLKSRC=1
CP=0 HLD=1 GO=0 FUNC=1
0x0000C350, /* Period Register (PRD) */ // 50000 *
CPU clock/4 = 1.00E-03
0x00000000 /* Counter Register (CNT) */
);
}
IRQ_enable(IRQ_EVT_TINT1);
install_interrupt(CPU_INT15, clock_isr); // clock period
interrupt (Timer1 Interrupt)
}
void RTCStop(void)
{
TIMER_close(hTimer1); // timer: wait
hTimer1 = (TIMER_Handle) INV;
install_interrupt(CPU_INT15, NULL); // measurement period
interrupt (Timer1 Interrupt)
}
William C Bonner wrote:
> It's been quite a while since I've asked this question, and I'm now much
> more fluent in programming on the DSP.
> I'm trying to keep track of time on a TMS320C6713 DSP. I'd like to use
> the standard C library calls if possible, but I don't know how to hook
> their backend up properly to keep time. If I'm connected and running
> the emulator over the JTAG I get values that are from the host
> computer. Here's a code snippet and the two examples of output.
>
> time_t timer;
> time(&timer);
> fprintf(tx," clock() = %u\r\n",clock());
> fprintf(tx," GMT Time = %s",asctime(gmtime(&timer)));
> fprintf(tx,"Local Time = %s",asctime(localtime(&timer)));
> Not connected:
> clock() = 4177131769
> GMT Time = Wed Nov 24 02:35:04 2032
> Local Time = Tue Nov 23 20:35:04 2032
>
> Connected and running code loaded via jtag:
> clock() = 0
> GMT Time = Thu Nov 30 22:49:37 2006
> Local Time = Thu Nov 30 16:49:37 2006
> To further complicate things, Timer0 on my system is being used to
> generate a pulse to an external device, and I'm not in a position to be
> able to rewire the connection to Timer1. I can use Timer1 if that's
> what I need to do.
>
>