user2578666
user2578666

Reputation: 1143

What does the following piece of code do

short rtimer_arch_now(void)
{
  short t1, t2;
  do {
    t1 = TA1R;
    t2 = TA1R;
  } while(t1 != t2);
  return t1;
}

TA1R is a The Timer_A Register. I still dont get why there is a loop. If they want to return the time whydont they simply return TA1R. What is the loop for?

Upvotes: 10

Views: 1042

Answers (2)

markgz
markgz

Reputation: 6266

The code is attempting to wait until TA1R changes and then return the old value of TA1R.

This code will only work if TA1R was declared as volatile, otherwise the compiler can optimize the loop away.

Upvotes: 2

perreal
perreal

Reputation: 98108

It tries to avoid the case when you ask the current time but it returns the value right before the time ticks. So it only returns the current time if the reading is stable.

Upvotes: 13

Related Questions