NOTICE: The Processors Wiki will End-of-Life on January 15, 2021. It is recommended to download any files or other content you may need that are hosted on processors.wiki.ti.com. The site is now set to read only.

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()[edit]

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[edit]

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[edit]

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[edit]

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()[edit]

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

E2e.jpg {{
  1. switchcategory:MultiCore=
  • For technical support on MultiCore devices, please post your questions in the C6000 MultiCore Forum
  • For questions related to the BIOS MultiCore SDK (MCSDK), please use the BIOS Forum

Please post only comments related to the article Customizing the clock and time Functions here.

Keystone=
  • For technical support on MultiCore devices, please post your questions in the C6000 MultiCore Forum
  • For questions related to the BIOS MultiCore SDK (MCSDK), please use the BIOS Forum

Please post only comments related to the article Customizing the clock and time Functions here.

C2000=For technical support on the C2000 please post your questions on The C2000 Forum. Please post only comments about the article Customizing the clock and time Functions here. DaVinci=For technical support on DaVincoplease post your questions on The DaVinci Forum. Please post only comments about the article Customizing the clock and time Functions here. MSP430=For technical support on MSP430 please post your questions on The MSP430 Forum. Please post only comments about the article Customizing the clock and time Functions here. OMAP35x=For technical support on OMAP please post your questions on The OMAP Forum. Please post only comments about the article Customizing the clock and time Functions here. OMAPL1=For technical support on OMAP please post your questions on The OMAP Forum. Please post only comments about the article Customizing the clock and time Functions here. MAVRK=For technical support on MAVRK please post your questions on The MAVRK Toolbox Forum. Please post only comments about the article Customizing the clock and time Functions here. For technical support please post your questions at http://e2e.ti.com. Please post only comments about the article Customizing the clock and time Functions here.

}}

Hyperlink blue.png Links

Amplifiers & Linear
Audio
Broadband RF/IF & Digital Radio
Clocks & Timers
Data Converters

DLP & MEMS
High-Reliability
Interface
Logic
Power Management

Processors

Switches & Multiplexers
Temperature Sensors & Control ICs
Wireless Connectivity