Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-08

High resolution timers on linux ?

16 messages · 8 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Kenneth Due

Hello list

I need some way to determine when 1/10000 sec has passed

I suppose it can be done with the timerchip in the PC - but how ?

anyone ......

Kenneth Due

http://www.skummel.dk
Tlf.
5850 6402 priv.
2220 7691 arb.

Risikabel : Hybridnet med kinesiske tv-udsendeler

Matti Hamalainen
to Kenneth Due
Kenneth Due wrote 8 lines
Hello list I need some way to determine when 1/10000 sec has passed I suppose it can be done with the timerchip in the PC - but how ? anyone ......

man usleep

usleep(X) suspends the execution of calling process for X microseconds (10^-6 sec). The accuracy may not be (== is not) 100% due to the multitasking nature of UNIX-type operating systems.

There are also setitimer() and getitimer()-functions that can be used to get a callback-type timed action.

=- ccr/TNSP^pWp
] | http://www.tnsp.org/
] Show me your "realtime raytracer in Java"
] and I'll show you my CBM 8032, 2 MHz!

Marco van de Voort
to Matti Hamalainen: man usleep usleep(X) suspends the execution of calling process for X microseconds (10^-6…
Show quoted text 19 lines
On Mon, 27 Aug 2001, Kenneth Due wrote: > Hello list > > I need some way to determine when 1/10000 sec has passed > > I suppose it can be done with the timerchip in the PC - but how ? > > anyone ...... > man usleep usleep(X) suspends the execution of calling process for X microseconds (10^-6 sec). The accuracy may not be (== is not) 100% due to the multitasking nature of UNIX-type operating systems. There are also setitimer() and getitimer()-functions that can be used to get a callback-type timed action.

nanosleep in the Unix/Linux unit works for both Linux and FreeBSD.

To properly work, it needs a machine with the rdtsc instruction (IIRC later Intel 486's and all but the old pentiums)

Unknown author

486 ?

This piece of software is meant to be running on a SingleBoardComputer running a Intel 386EX Microprocessor @ 25 Mhz with a 2.2."something" kernel

I suppose it still can use the usleep function,
if it is implementet into fpc ?

Or if the nanosleep function works on old x386 type processors ?

(Marco van de Voort) wrote ..

Show quoted text 28 lines
> On Mon, 27 Aug 2001, Kenneth Due wrote: > > > Hello list > > > > I need some way to determine when 1/10000 sec has passed > > > > I suppose it can be done with the timerchip in the PC - but how ? > > > > anyone ...... > > > > man usleep > > usleep(X) suspends the execution of calling process for X microseconds > (10^-6 sec). The accuracy may not be (== is not) 100% due to the > multitasking nature of UNIX-type operating systems. > > There are also setitimer() and getitimer()-functions that can be used > to get a callback-type timed action. nanosleep in the Unix/Linux unit works for both Linux and FreeBSD. To properly work, it needs a machine with the rdtsc instruction (IIRC later Intel 486's and all but the old pentiums) _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Marco van de Voort
to Unknown author
This piece of software is meant to be running on a SingleBoardComputer running a Intel 386EX Microprocessor @ 25 Mhz with a 2.2."something" kernel

Then you might not be able to get such resolutions. I don't know exactly how far the ordinary timer can go.

I suppose it still can use the usleep function, if it is implementet into fpc ?

FPC isn't the problem. If it is a problem, it is either hardware or OS. If it is callable from gcc (c), it can be done in FPC.

It turns out that usleep is based on nanosleep. Afaik it is

Or if the nanosleep function works on old x386 type processors ?

I don't see warnings. I think simply try to use these functions in a few small testprograms, and then determine if the resolution they provide is adequate. If not, I think you should talk to your hardware vendor, maybe the embedded system has another (hardware) way of doing precise timings)

Matti Hamalainen
to Unknown author: 486 ? This piece of software is meant to be running on a SingleBoardComputer running a…
Show quoted text 8 lines
486 ? This piece of software is meant to be running on a SingleBoardComputer running a Intel 386EX Microprocessor @ 25 Mhz with a 2.2."something" kernel I suppose it still can use the usleep function, if it is implementet into fpc ?

usleep() is a libc function and interface should be available in the Linux-unit. It works independant of the underlying platform.

You could also use the ualarm(), if you need a "callback" style working. It is in practice, however, equal to set/getitimer().

Please do
man alarm
man setitimer
man usleep

And select whatever you think is good for you. I believe that most of these libc-functions can be interfaced through Linux-unit.

=- ccr/TNSP^pWp
] | http://www.tnsp.org/
] Show me your "realtime raytracer in Java"
] and I'll show you my CBM 8032, 2 MHz!

Matti Hamalainen
to Gabor DEAK JAHN: I have a unit I've used quite a lot some years ago, it was written for Borland Pascal, so…
Gabor DEAK JAHN wrote 15 lines
At 8/27/01 09:13 PM, you wrote: Kenneth, > I need some way to determine when 1/10000 sec has passed > I suppose it can be done with the timerchip in the PC - but how ? I have a unit I've used quite a lot some years ago, it was written for Borland Pascal, so you will need to modify it slightly. As far as I can remember, its precision was below a microsecond and didn't even reprogram the timer (it did modify one setting but not the actual rate so it didn't disturb other software running). The trick is that although the usual timer interrupt is very rare, the timer chip itself counts rather fast. All you have to do is to read this counter value and use it to form a very high resolution timer.

I think you missed the point: You can only use that kind of trick under DOS, he wanted a solution that works under Linux. Under Linux you would need a kernel module for that and that's probably beyond the scope of this mailing-list and is not even possible with FPC (AFAIK).

=- ccr/TNSP^pWp
] | http://www.tnsp.org/
] Show me your "realtime raytracer in Java"
] and I'll show you my CBM 8032, 2 MHz!

Lee, John

Thomas Schatzl's cpu unit works for linux & has all of the (fpc) code for accessing the cpu timers as well as other useful stuff... surprised noone has mentioned this...J

Gabor DEAK JAHN
to Kenneth Due: Hello list I need some way to determine when 1/10000 sec has passed I suppose it can be…

At 8/27/01 09:13 PM, you wrote:

Kenneth,

I need some way to determine when 1/10000 sec has passed I suppose it can be done with the timerchip in the PC - but how ?

I have a unit I've used quite a lot some years ago, it was written for Borland Pascal, so you will need to modify it slightly. As far as I can remember, its precision was below a microsecond and didn't even reprogram the timer (it did modify one setting but not the actual rate so it didn't disturb other software running). The trick is that although the usual timer interrupt is very rare, the timer chip itself counts rather fast. All you have to do is to read this counter value and use it to form a very high resolution timer.

=================================================
unit Time;
interface
function GetTime : longint;
procedure ConvertTime (Stamp : longint; var HH, MM : word; var SS : real);

implementation
const

   TIMER0 = $40; TIMERC = $43; PICO0 = $20; OCW3IR = $0A; TMODE2 = $34;
   NEW_TICKS = trunc (256 * 12 * 3600 * 18.2); { Number of ticks until noon  }
   FRAC_SEC = 256 * 182;                       { Number of ticks in sec * 10 }
   NOON_SECS = 3600 * 12;                      { Number of secs in day at

noon }

function GetTime;
var
   dummy : word;
   TempTime, Timer : longint;
   BiosTime : longint absolute $0040:$006B;    { $0040:$006C * 256           }
begin
   asm cli; end;
   TempTime := BiosTime and $FFFFFF00;
   Port [TIMERC] := 0; dummy := Port [TIMER0];
   Timer := 255 - ($FF and Port [TIMER0]);
   Port [PICO0] := OCW3IR;
   if (Port [PICO0] and 1) > 0 then Timer := Timer or $100;
   inc (TempTime, Timer);
   asm sti; end;
   GetTime := TempTime;
end; { GetTime }
procedure ConvertTime;
var
   Temp, SubFlag : longint;
begin
   SubFlag := 0;
   if Stamp > NEW_TICKS then
     begin dec (Stamp, NEW_TICKS); SubFlag := NOON_SECS; end;
   Temp := SubFlag + (Stamp * 10) div FRAC_SEC;
   SS := Temp mod 60 + word ((10000 * ((10 * Stamp) mod FRAC_SEC)) div
FRAC_SEC) / 10000;
   Temp := Temp div 60; MM := Temp mod 60; HH := Temp div 60;
end; { ConvertTime }
begin
   asm cli; end;
   Port [TIMERC] := TMODE2;
   Port [TIMER0] := 0; Port [TIMER0] := 0;
   asm sti; end;
end.
=================================================

Regards,

   Gabor DEAK JAHN

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
WWW: www.tramontana.co.hu
E-mail:

Gabor DEAK JAHN
to Matti Hamalainen: I think you missed the point: You can only use that kind of trick under DOS, he wanted a…

At 8/28/01 02:00 PM, you wrote:

Matti,

I think you missed the point: You can only use that kind of trick under DOS, he wanted a solution that works under Linux. Under Linux you would need a kernel module for that and that's probably beyond the scope of this mailing-list and is not even possible with FPC (AFAIK).

The unit is not linked to DOS, it is pure hardware but yes, you might need services that Linux does not allow for user programs. I'd experiment with it, anyway, it might turn out that those port writes are not that important after all (and I suppose, you can read ports on Linux without problems). I cannot really tell because it was more than five years ago that I had written any program on Linux but I still think it is worth a try.

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
E-mail:

Marco van de Voort
to Matti Hamalainen: usleep() is a libc function and interface should be available in the Linux-unit. It works…
Show quoted text 22 lines
> 486 ? > > This piece of software is meant to be running on a > SingleBoardComputer running a Intel 386EX Microprocessor @ 25 Mhz > with a 2.2."something" kernel > > I suppose it still can use the usleep function, > if it is implementet into fpc ? usleep() is a libc function and interface should be available in the Linux-unit. It works independant of the underlying platform. You could also use the ualarm(), if you need a "callback" style working. It is in practice, however, equal to set/getitimer(). Please do man alarm man setitimer man usleep And select whatever you think is good for you. I believe that most of these libc-functions can be interfaced through Linux-unit.

The Linux unit is standalone, and doesn't interface to libc.

It contains similar functions though (because they are based on the same primitives)

Kenneth Due
to Marco van de Voort

I think usleep will work for me, but how do I get fpc to understand the command

how do i make a link from fpc to libc ?

you wrote 32 lines
> > 486 ? > > > > This piece of software is meant to be running on a > > SingleBoardComputer running a Intel 386EX Microprocessor @ 25 Mhz > > with a 2.2."something" kernel > > > > I suppose it still can use the usleep function, > > if it is implementet into fpc ? > > usleep() is a libc function and interface should be available > in the Linux-unit. It works independant of the underlying platform. > > You could also use the ualarm(), if you need a "callback" style > working. It is in practice, however, equal to set/getitimer(). > > Please do > man alarm > man setitimer > man usleep > > And select whatever you think is good for you. I believe that > most of these libc-functions can be interfaced through Linux-unit. The Linux unit is standalone, and doesn't interface to libc. It contains similar functions though (because they are based on the same primitives) _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Kenneth Due

http://www.skummel.dk
Tlf.
5850 6402 priv.
2220 7691 arb.

Risikabel : Hybridnet med kinesiske tv-udsendeler

Lee, John

did you look at Thomas Schatzl's CPU- advantage it's already in fpc...

Earlier message 57 lines
-----Original Message----- From: Kenneth Due Sent: Tuesday, August 28, 2001 07:07 To: Subject: Re: [fpc-pascal]High resolution timers on linux ? I think usleep will work for me, but how do I get fpc to understand the command how do i make a link from fpc to libc ? At 18:53 28-08-01 +0200, you wrote: >> > 486 ? >> > >> > This piece of software is meant to be running on a >> > SingleBoardComputer running a Intel 386EX Microprocessor @ 25 Mhz >> > with a 2.2."something" kernel >> > >> > I suppose it still can use the usleep function, >> > if it is implementet into fpc ? >> >> usleep() is a libc function and interface should be available >> in the Linux-unit. It works independant of the underlying platform. >> >> You could also use the ualarm(), if you need a "callback" style >> working. It is in practice, however, equal to set/getitimer(). >> >> Please do >> man alarm >> man setitimer >> man usleep >> >> And select whatever you think is good for you. I believe that >> most of these libc-functions can be interfaced through Linux-unit. > >The Linux unit is standalone, and doesn't interface to libc. > >It contains similar functions though (because they are based on the same >primitives) > >_______________________________________________ >fpc-pascal maillist - >http://lists.freepascal.org/mailman/listinfo/fpc-pascal > Kenneth Due http://www.skummel.dk Tlf. 5850 6402 priv. 2220 7691 arb. Risikabel : Hybridnet med kinesiske tv-udsendeler
Kuba Ober
to Matti Hamalainen: I think you missed the point: You can only use that kind of trick under DOS, he wanted a…
Show quoted text 13 lines
> I have a unit I've used quite a lot some years ago, it was written for > Borland Pascal, so you will need to modify it slightly. As far as I can > remember, its precision was below a microsecond and didn't even reprogram > the timer (it did modify one setting but not the actual rate so it didn't > disturb other software running). The trick is that although the usual > timer interrupt is very rare, the timer chip itself counts rather fast. > All you have to do is to read this counter value and use it to form a > very high resolution timer. I think you missed the point: You can only use that kind of trick under DOS, he wanted a solution that works under Linux. Under Linux you would need a kernel module for that and that's probably beyond the scope of this mailing-list and is not even possible with FPC (AFAIK).

Huh??
You can program i/o ports as you wish, as long as your program starts as root, gets proper ioperms, and then drops root privileges. Not very clean, and not portable outside ibm-pc compatibles, but works ;-).

Kuba

Lee, John

The best timers, which work on all i386s supporting linux, go32, win32, os2 for vpascal, fpc & maybe others are IMHO, those from Thomas Schatzl's cpu unit (see his web site)- http://members.yline.com/~tom_at_work/index.html J

Earlier message 30 lines
-----Original Message----- From: Kuba Ober Sent: 02 October 2001 08:30 To: Subject: Re: [fpc-pascal]High resolution timers on linux ? > > I have a unit I've used quite a lot some years ago, it was written for > > Borland Pascal, so you will need to modify it slightly. As far as I can > > remember, its precision was below a microsecond and didn't even reprogram > > the timer (it did modify one setting but not the actual rate so it didn't > > disturb other software running). The trick is that although the usual > > timer interrupt is very rare, the timer chip itself counts rather fast. > > All you have to do is to read this counter value and use it to form a > > very high resolution timer. > > I think you missed the point: You can only use that kind of trick under > DOS, he wanted a solution that works under Linux. Under Linux you would > need a kernel module for that and that's probably beyond the scope of > this mailing-list and is not even possible with FPC (AFAIK). Huh?? You can program i/o ports as you wish, as long as your program starts as root, gets proper ioperms, and then drops root privileges. Not very clean, and not portable outside ibm-pc compatibles, but works ;-). Kuba