I am trying to understand the overflow conditions for Arduino C++ (specifically the Due/SAM3X8E), in order to write timer logic based on the micros() function.
For those who don't know, the Arduino micros() function returns the number of microseconds which have elapsed since program start, as a 32 bit unsigned long. This value will wrap each 2^32 microseconds (~71 minutes).
The issue is that I am seeing a different result in GCC to what I get on an online arduino simulator.
Here is my test code - first, the GCC version:
#include <stdio.h>
int main()
{
unsigned long currentTime = 0x00000100;
unsigned long earlierTime = 0xFFFFFF00;
unsigned long result = currentTime-earlierTime;
printf("%lx", result);
return 0;
}
The above is setup to get the value to overflow by causing a negative number in the result variable, and returns hex ffffffff00000200, as I would expect.
But on Arduino:
// setup
unsigned long currentTime = 0x00000100;
unsigned long earlierTime = 0xFFFFFF00;
// main loop
unsigned long result = currentTime-earlierTime;
Running this on the online arduino sim wokwi returns (hex) 200, which is not expected but is actually more useful to me as my timer checks will still work when the micros() output wraps to zero each 71 minutes.
So my question is: why the difference in behaviour? And can I rely on the behaviour that the online Arduino simulator exhibits to be the same for an Arduino Due (the simulator is based on Uno)? I don't have real Due hardware to test with at the moment.
I hope this question makes sense to someone.