Skip to content

micros() is non-monotonic #65

@Khalinda

Description

@Khalinda

I was benchmarking some floating point operations on a CH32V307 chip and I was getting crazy results. The following sketch illustrates the problem:

uint32_t au32Time[1000];

void setup()
{
	int i;

	Serial.begin(115200);
	delay(1000);
	for(i=0;i<1000;i++)
		au32Time[i]=micros();
	for(i=0;i<1000;i++)
		Serial.println(au32Time[i]);
}

void loop(){}

The output starts out with:

1000998
1000996
1000994
1000992
1000990

And about half way down, this happens:

1000005
1000003
1000001
1001998
1001996
1001994

The counts should be strictly increasing. I haven't looked at the CH32 core, but in the Arduino core, micros() is essentially calculated from millis(). There seems to be an arithmetic error in the CH32 version. I tested this sketch on versions 1.0.3 and 1.0.4 and I obtained the same results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions