Micros() rolling over after 59.652323 seconds, not 71 minutes [SOLVED]

Thanks rvnash. For now, since I know it is a Photon, I hardcoded the rollover time and it seems to work. MAX_MICROS is simply 2^32/120




void isrduration1()
{
  // if the pin is low, its the start of an interrupt
  if(digitalRead(D3) == LOW)
  { 
    StartPeriod1 = micros();
  }
  else
  {
    // if the pin is high, its the rising edge of the pulse so now we can calculate the pulse duration by subtracting the 
    // start time  from the current time returned by micros()
    if(StartPeriod1 && (pulse1 == false))
    {
      EndPeriod1 = micros();
      if(StartPeriod1 < EndPeriod1)
        duration1 = EndPeriod1 - StartPeriod1;
      else
        duration1 = MAX_MICROS - StartPeriod1 + EndPeriod1;
      StartPeriod1 = 0;
      pulse1 = true;
    }
  }
}

The latest release contains this fix. You can download it from github releases - https://github.com/spark/firmware/releases/tag/v0.4.4-rc.3, or wait a few days when we roll it out to the Cloud.

Can anyone point me to the docs for this, please - I’m a huge fan of free-running unsigned counters, and am massively glad to hear this is done, now I just want to learn how to use it right.

Hi @AndyW

The new doc location is here:

https://docs.particle.io/reference/firmware/photon/#micros-

But it does not look like the text has been updated to match to correct roll-over yet.

It is an Arduino wiring compatible function, so that doc is here:

https://www.arduino.cc/en/Reference/Micros

The docs will be released with 0.4.4 being available in the cloud. (All the pending changes are PRs.)

I recall that the usual micros() is to read the millis() timer count, know its freq. and calculate micros within the current millis() interval (i.e., interpolating).
Then multiply last millis() count to be micros and add the interpolation value.

Thanks stevech, yes, that’s how I re-implemented it. Previously it was taking a 32-bit clock counter and dividing it, resulting in a rollover period of 2^32 / Clock_Hz.

well… the 32 bit millis() overflows the modulo every 2^32 milliseconds.
that’s 49 days, right?