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 -----
		
	}

Comments

  1. aditya

    11 years ago

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

  2. yosheeck

    11 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

      11 years ago

      Very nice – thanks!

    2. neha suresh hule

      5 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

    12 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