Please note as of Wednesday, August 15th, 2018 this wiki has been set to read only. If you are a TI Employee and require Edit ability please contact x0211426 from the company directory.

Customizing the clock and time Functions

From Texas Instruments Wiki
Jump to: navigation, search

The compiler's RTS library provides the C standard time and clock functions, but the default implementation of these functions require that the program be run under CCS, or a similar tool which supports the CIO System Call Protocol. If CIO is not available, and you need to use one of these functions, you must provide your own definition of the function.

Function clock()

You can replace the function clock with a custom mechanism. Be aware that the value of CLOCKS_PER_SEC in time.h is a hard-coded value and may not accurately reflect the number of "clocks" per second, especially if you have replaced the clock function.

C6000

For C64x+ and higher members of the C6000 family, there is a pair of registers, TSCL and TSCH, which together provide a 64-bit clock value. You can create your own clock function to take advantage of these registers. Simply add this function to your program and it will override the clock function from the library. NOTE: you must have one write to TSCL at the start of your program to start the clock running.

#include <time.h>
extern cregister volatile unsigned int TSCL;
extern cregister volatile unsigned int TSCH;
clock_t clock()
{
  /* must read TSCL first! */
  unsigned int low  = TSCL;
  unsigned int high = TSCH;
  /* clock_t is only 32 bits, so if TSCH is
     non-zero return -1 meaning "unrepresentable" */
  if (high) return (clock_t)-1;
  return low;
}

XDC

If you have XDC (provided with SYS/BIOS), you could use:

#include <time.h>
#include <xdc/runtime/Timestamp.h>
clock_t clock()
{
  return Timestamp_get32();
}

What further setup is needed?

SYS/BIOS

If you have SYS/BIOS, you could use:

#include <time.h>
#include <???>
clock_t clock()
{
  return CLK_gethtime();
}

What further setup is needed?

Function time()

If you have access to a reliable real world clock (such as a radio time signal [such as radio stations WWV and WWVH], NTP, or NITZ), you can create your own time function to take advantage of it. Add this function to your program and it will override the clock function from the library.

#include <time.h>
time_t time(time_t *t)
{
   time_t seconds_since_epoch = reliable_real_world_clock();
   if (t) *t = seconds_since_epoch;
   return seconds_since_epoch;
}

NOTE: be aware that the TI RTS library uses a different epoch than POSIX systems use. If your real world clock source uses a different epoch, you'll need to adjust it for the TI epoch, or else the functions gmtime, localtime, and mktime will return incorrect results.

#include <time.h>
time_t time(time_t *t)
{
   time_t seconds_since_epoch =
       (unsigned long long)reliable_real_world_clock() + EPOCH_DELTA;
   if (t) *t = seconds_since_epoch;
   return seconds_since_epoch;
}

If your real world clock source is POSIX, EPOCH_DELTA is 0x83aa2a20