Setting up to use clockgettime()

Include the following header file:

#include <time.h>

You also need to add the librt library to the linker

Using a Geany makefile, add this:

-lrt

Using eclipse:

Right click your project > Properties > C/C++ Build > Settings > Cygwin C++ Linker > Libraries.  Add a new library entry: rt

Example Which Provides Accurate uS Timing

//*****************************************************
//*****************************************************
//********** DELAY FOR # uS WITHOUT SLEEPING **********
//*****************************************************
//*****************************************************
//Using delayMicroseconds lets the linux scheduler decide to jump to another process.  Using this function avoids letting the
//scheduler know we are pausing and provides much faster operation if you are needing to use lots of delays.
void DelayMicrosecondsNoSleep (int delay_us)
{
	long int start_time;
	long int time_difference;
	struct timespec gettime_now;

	clock_gettime(CLOCK_REALTIME, &gettime_now);
	start_time = gettime_now.tv_nsec;		//Get nS value
	while (1)
	{
		clock_gettime(CLOCK_REALTIME, &gettime_now);
		time_difference = gettime_now.tv_nsec - start_time;
		if (time_difference < 0)
			time_difference += 1000000000;				//(Rolls over every 1 second)
		if (time_difference > (delay_us * 1000))		//Delay for # nS
			break;
	}
}

Example which creates a background heartbeat timer for your projects main loop

long int last_heartbeat;
long int heartbeat_difference;
struct timespec gettime_now;
int io_state = 0;
BYTE hb_10ms_timer = 0;
BYTE hb_100ms_timer = 0;


	//SETUP HEARTBEAT TIMER
	clock_gettime(CLOCK_REALTIME, &gettime_now);
	last_heartbeat = gettime_now.tv_nsec;



	//---------------------------
	//---------------------------
	//----- HEARTBEAT TIMER -----
	//---------------------------
	//---------------------------
	clock_gettime(CLOCK_REALTIME, &gettime_now);
	heartbeat_difference = gettime_now.tv_nsec - last_heartbeat;		//Get nS value
	if (heartbeat_difference < 0)
		heartbeat_difference += 1000000000;				//(Rolls over every 1 second)
	if (heartbeat_difference > 1000000)					//<<< Heartbeat every 1mS
	{
		//-------------------------
		//----- 1mS HEARTBEAT -----
		//-------------------------
		last_heartbeat += 1000000;						//<<< Heartbeat every 1mS
		if (last_heartbeat > 1000000000)				//(Rolls over every 1 second)
			last_heartbeat -= 1000000000;
	
	
	
		//Toggle a pin so we can verify the heartbeat is working using an oscilloscope
		io_state ^= 1;									//Toggle the pins state
		bcm2835_gpio_write(RPI_GPIO_P1_23, io_state);
	
	
	
	
		//--------------------------
		//----- 10mS HEARTBEAT -----
		//--------------------------
		hb_10ms_timer++;
		if (hb_10ms_timer == 10)
		{
			hb_10ms_timer = 0;
	
	
	
		} //if (hb_10ms_timer == 10)
	
	
		//---------------------------
		//----- 100mS HEARTBEAT -----
		//---------------------------
		hb_100ms_timer++;
		if (hb_100ms_timer == 100)
		{
			hb_100ms_timer = 0;
	
	
	
		} //if (hb_100ms_timer == 100)
	
	} //if (heartbeat_difference > 1000000)

System timer based 1mS timer

	static long int OurSystemTimerValueLast;
	long int TimeDifference;
	struct timespec gettime_now;

	//----- START OUR SYSTEM TIMER -----
	clock_gettime(CLOCK_REALTIME, &gettime_now);
	OurSystemTimerValueLast = gettime_now.tv_nsec;		//Get nS value
	
	
	
	//----- UPDATE OUR MS TIMER -----
	clock_gettime(CLOCK_REALTIME, &gettime_now);
	TimeDifference = gettime_now.tv_nsec - OurSystemTimerValueLast;
	OurSystemTimerValueLast = gettime_now.tv_nsec;
	if (TimeDifference < 0)
		TimeDifference += 1000000000;				//(Rolls over every 1 second)
	
	while (TimeDifference > 1000000)		//TimeDifference is in nS
	{
		TimeDifference -= 1000000;
		//----- 1mS TIMER -----
		
	}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

  1. aditya

    10 years ago

    hi everybody its not working in the raspberry pi 2 module please suggest me……

  2. yosheeck

    10 years ago

    Thanks for this article ! I started from here to get-time on my Pi project. However, it was not fast/accurate enough for me, so I went deeper in analysis of what/how we can use hardware clocks on Pi. If anyone is interested: http://yosh.ke.mu/article/raspberry_pi_getting_time

    1. Adam

      10 years ago

      Very nice – thanks!

    2. neha suresh hule

      4 years ago

      I tried this code. But it gives me error “can’t open /dev/mem – need root ?n”). Can you tell me why I’m getting this error? I gave permission with sudo chmod. But still get the error.

  3. xyz0k

    10 years ago

    Thank you so much!! I didn’t know the -lrt requirement in raspberry pi!

  4. Pingback: bcm2835 by Mike McCauley « Raspberry Pi Projects

Comments

Your email address will not be published. Required fields are marked *