LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-05-2010, 01:13 AM   #1
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Rep: Reputation: 39
delay calculation for LSB in timer


On the following link

Following code is given
Code:
loopbit = loops_per_jiffy;
/* Gradually work on the lower-order bits */
while (lps_precision-- && (loopbit >>= 1)) {
loops_per_jiffy |= loopbit;
ticks = jiffies;
while (ticks == jiffies); /* Wait until the start
of the next jiffy */
ticks = jiffies;
/* Delay */
__delay(loops_per_jiffy);

if (jiffies != ticks)
/* longer than 1 tick */
loops_per_jiffy &= ~loopbit;
}
I am not clear with how
Code:
loops_per_jiffy |= loopbit;
OR operation above and
Code:
loops_per_jiffy &= ~loopbit;
AND with Inverse help to achieve the desired result.
 
Old 09-05-2010, 01:41 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The first one sets the bits in loopbit, the second operation clears them. The whole operation tries to set the delay, if the current configuration doesn't work, it doesn't use it anymore. Try to work it out using some example numbers.
 
Old 09-05-2010, 02:47 PM   #3
tkmsr
Member
 
Registered: Oct 2006
Distribution: Ubuntu,Open Suse,Debian,Mac OS X
Posts: 798

Original Poster
Rep: Reputation: 39
Yea I was able to do it with example.I will mention my observation if some once come here by chance should help them.

lets assume

Code:
loops_per_jiffy = 1011 0010
Code:
 loopbit = loops_per_jiffy;
So loopbit = 1011 0010

Code:
 /* Gradually work on the lower-order bits */
 while (lps_precision-- && (loopbit >>= 1)) {
no idea of what lps_precision is may be some sort of check.

loopbit = loopbit/2 by above operation so new value of loopbit is
Code:
loopbit = 0101 1001
Code:
 loops_per_jiffy |= loopbit;
Now due to above
Quote:
loops_per_jiffy = loops_per_jiffy | loopbit
so the result of an OR operation
Quote:
loops_per_jiffy = 1011 0010 OR 0101 1001 = 1111 1011
which is higher than the initial value of loops_per_jiffy

Code:
 ticks = jiffies;
simple

Code:
 while (ticks == jiffies); /* Wait until the start
is also simple if due to some calculation you are in mid of jiffy interval then
it will make sure that as
Code:
tick==jiffies
will come out of loop only
at the start of jiffy

Code:
 of the next jiffy */
 ticks = jiffies;
simple

Code:
 /* Delay */
 __delay(loops_per_jiffy);
Will delay for the value loops_per_jiffy

Code:
 if (jiffies != ticks)
 /* longer than 1 tick */
 loops_per_jiffy &= ~loopbit;
here comes the magic if it is not satisfying the if condition then
will go back divide the loopbits by 2 as a result of >> (right shift)
and the OR operation will make sure that the value which is divided by
2 of loopbits gets added to loops_per_jiffy
will enter the delay loop and in this case if this time the "if"
condition is true then
the result of
Quote:
loops_per_jiffy &= ~loopbit
is
Quote:
loops_per_jiffy = loops_per_jiffy AND (~loopbit)
where taking complement of loopbit as above makes it negative (not
sure on this part)
and the result is the value is subtracted (as it was more than required value)

and then loopbit is again divided so added and this cycle will continue until
exact value of loops_per_jiffy is calculated.
Code:
 }
but if there are more examples of such a calculation then let me know.
I just came across this one.
 
  


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
how to implement delay (microseconds) with real timer ticking at 100ms. mayankparasher Programming 1 12-24-2009 12:04 PM
Multimedia timer (SMP friendly timer) bigqueso Linux - Kernel 0 03-15-2007 03:49 PM
Building LSB compatible application with LSB SDK - lsbappchk fails gkiagia Programming 0 01-12-2007 05:00 AM
How do you call delay/udelay/mdelay timer in userspace? pat_and_cami SUSE / openSUSE 0 02-23-2006 02:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:42 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