This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Problem with micros() timer going backwards briefly (not wraparound) in TM4C123GXL

Other Parts Discussed in Thread: ENERGIA

I'm using the micros() call in Energia to get the microsecond timer and using it to calculate speeds for a quadrature decoder on a Tiva C Launchpad TM4C123GXL.

But I am running into problem with the microsecond timer (micros() call in Energia) going briefly backwards.  I know about wraparound since micros() returns an unsinged long I expect that it would wrap around after a while, but this is going backwards when the counter is nowhere near the point when it should wrap.

I wrote a program to collect time values returned by micors() in the loop and print out the saved values when I detect an error. Here is an example of what I get (older time values are further down, newer value are on top).

5025005 
5024001 <<< went backward here, should have been 5025001
5024996
5024992
5024989
5024985

Notice that that the count goes from 5024996 to 5024001, and then 5025005 as if the carry to the 1000's digit gets delayed.

This happens multiple times, always on the same 1000's digit.  And I have tried this on two different parts with the same result.

Here is another example:

5401005
5400001 <<< went backward here
5400996
5400992
5400989
5400985

Can anyone let me know if I'm doing something wrong or if this is a bug.  I know I can work around it but it seems like a hack to do so.

When you see the word "Last" appear in the serial output, then the next few counts are the ones to look at.

Here is the code to reproduce:

unsigned long prevus[1000];
unsigned long lastus = 0;
int idx = 0;
int printnext = 0;

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(57600);  
}

void printprev() {
  Serial.println("Last");
  int pidx = idx - 1;
  for (int count = 0; count < 100; count++) {
    if (pidx < 0) {
      pidx = 999;
    }
    Serial.println(prevus[pidx]);
    pidx--;
  }
}

void loop()
{
  // put your main code here, to run repeatedly:
  unsigned long curus;
  
  curus = micros();
  prevus[idx] = curus;
  idx++;
  if (idx > 999) {
    idx = 0;
  }
  if (printnext == 1) {
        printprev();
        printnext = 0;
  }

  if (curus < lastus) {
    printnext = 1;
  }
  lastus = curus;
}

  • I would suggest this is an Energia question, not a TM4C question.

    Robert
  • Hello Ashok,

    As Robert has indicated, this seems to be more of an issue with Energia not the TM4C. I would recommend you check with the Energia community to see if they would have any solutions to offer.

  • Hello Chuck,

    OK, sorry, I thought I was in the right forum because when I looked at the TI site for Energia Support and Community (www.ti.com/.../Energia it seems to include a link to this forum.

    I see that page does provide a link for Energia-related support (forum.43oh.com/.../) but the link is listed (incorrectly?) under "Description" instead of "Support".

    I'll ask my question there.

    Sorry for the disruption.

  • Hi Ashok,

    No disruption was noted. I think the confusion with these types of tools is that the tools themselves require specific support which the energia community would need to support as in the content of the misbehaving function you have noted. Certainly, it would be nice to have the knowledge of all tools and third party implementations applying to TM4C so that we could provide one stop shopping for support; unfortunately, this isn't the reality.

    If the situation is diagnosed as the content of the function is correct but there is some misbehavior of the platform (i.e. TM4C), we can help trouble shoot the behavior of the TM4C and make sure that any special handling of the TM4C is noted. So, once you get feedback from the energia community, please come back and let us help if we can nail down some specific issue with the microcontroller.