LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 03-26-2012, 02:15 AM   #1
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Rep: Reputation: Disabled
Change High Resolution Time Source


Hi all-

I am working on a hardware BSP. The underlying hardware has an oven-controlled quarz oscillator (OCXO) onboard, which is used to count a high precision NTP time at a dedicated HW address, which I can read out memory-mapped from software.

The processor is a Freescale MPC8568. It has a high precision timer, which - I think - is used by the Linux kernel to provide HR timing.

The question is: can I tell Linux to use OUR hardware timer as the system time and thus for all HR time related issues?

At the moment, I have to maintain two parallel clocks. I'd rather like our timer to be used as system time (with all system calls and functions working implicitly), because it's more stable than the CPU's timer.

Edit: I am using Linux 2.6.29.6 RT24

Cheers,
Matt

Last edited by taylor_ma; 03-26-2012 at 06:30 AM. Reason: added used kernel version
 
Old 04-16-2012, 07:26 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
My understanding is that you need to implement your timer as a clocksource driver, and then use the clocksource kernel parameter to set this as the default source.

Something like the scx200_hrt driver might be a useful model.

The system interface /sys/devices/system/clocksource/clocksource0/current_clocksource and /sys/devices/system/clocksource/clocksource0/available_clocksource will give you information about which source is being used, and what other clock sources are available.

The conference report on the introduction of hrtimers may also be useful.

Last edited by neonsignal; 04-16-2012 at 07:28 PM.
 
Old 04-17-2012, 02:14 AM   #3
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
Thanks for the hints!

I'm trying this out right now, and I'm not so sure what to put into the struct clocksource.

I have two hardware registers:

1. a 64-bit value from the OCXO running at 33 1/3 MHz, resulting in an effictive LSB tick of exact 30 nanosec.

2. a 64-bit NTP register, where the time above is converted by hardware.

Which one should I take, and what are the parameters for .mask, .shift and .mult?

I assume using .flags = CLOCK_SOURCE_IS_CONTINUOUS is correct?!

Thanks again,
Matt
 
Old 04-17-2012, 03:05 AM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
I haven't implemented one of these myself, so if you get stuck, you might want to ask on one of the kernel mailing lists. Looking through clocksource.h, I gather that mult and shift form the scale ratio (to convert hardware ticks into nanoseconds), so I guess you could just work with a 30 times multiply and a zero shift. The mask is intended for registers of less than 64 bits; in your case you will have all the bits set (or just use CLOCKSOURCE_MASK(64)). I don't know which flags you should use, though the CONTINUOUS and the VALID_FOR_HRES look reasonable.
 
Old 05-21-2012, 08:34 AM   #5
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
Hi, I am back again with some results:

I managed to create a clocksource driver that reads the monotonically upcounting counter of my hardware.

It is possible, to apply some sort of long time correction to the hardware counter. In fact, it does not count up the register every x-th tick or inserts another tick after x ticks, depending on if I want it to go slower of faster.

Now: I have put a 4 into the correction register which actually means that every fourth tick, the counter is not incremented. Indeed, this is a rather hard correction, but good for testing.

On the command line I start this simple loop:

Code:
while `true`; do date; time sleep 20; done
which is pretty straight forward just printing the current date then sleep for 20 secs.

This is my result:

Code:
Mon Nov 25 21:19:22 CET 1929

real    0m20.025s
user    0m0.000s
sys     0m0.004s
Mon Nov 25 21:19:38 CET 1929

real    0m20.025s
user    0m0.000s
sys     0m0.004s
Mon Nov 25 21:19:54 CET 1929

real    0m20.022s
user    0m0.000s
sys     0m0.000s
Mon Nov 25 21:20:10 CET 1929

real    0m20.023s
user    0m0.000s
sys     0m0.004s
Mon Nov 25 21:20:26 CET 1929

real    0m20.025s
user    0m0.000s
sys     0m0.000s
Mon Nov 25 21:20:42 CET 1929

real    0m20.025s
user    0m0.000s
sys     0m0.000s
Obviously, the hardware measures a real amount of time of 20 secs for the sleep. But the wall time actually only increases by approx. 16 secs every time.

Why that? I mean, when I measure externally, the elapsed real time is in fact 16 seconds, what makes sense, as I told the clock to go a lot slower.

But how the hack does Linux still keep track of the outside wall time?

BTW: This also happens, if I do this whole thing only in software by changing the struct clocksource .mult value to the half of what it actually is. A sleep 1 actually takes two seconds for real, but the date-command still maintains the outside wall time...
 
Old 05-21-2012, 08:46 AM   #6
taylor_ma
LQ Newbie
 
Registered: Feb 2012
Distribution: Debian or Ubuntu
Posts: 17

Original Poster
Rep: Reputation: Disabled
In another test program, I compared clock_gettime over gettimeofday which also gave me a headache:

Code:
clock_gettime: 3029539150 s 269233685 ns
gettimeofday:  3029539150 s 921013 us

sleeping 30 seconds...

clock_gettime: 3029539174 s 270666841 ns
gettimeofday:  3029539180 s 922981 us

Elapsed timespec: 24.001 s
Elapsed timeval : 30.002 s
WHY AND HOW IS THIS HAPPENING???
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
High Resolution Timers sachin_damle Linux - General 1 07-23-2008 03:15 PM
LXer: Open source-based high-resolution cameras for Web developers LXer Syndicated Linux News 0 04-12-2007 06:01 AM
Text Resolution, Graphic resolution. Change from prompt dlublink SUSE / openSUSE 2 01-28-2005 11:50 AM
Can't get high resolution in Slackware Algernon Linux - Newbie 7 08-06-2004 02:24 PM
High resolution on booting spank Linux - Newbie 4 08-26-2003 03:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 11:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration