How to deal with a wrapping counter in embedded C

26,218

Solution 1

If you take two timestamp readings and your first reading is greater than the second, then your counter has wrapped. That is the basic way to detect a wrapping counter.

This, however, won't detect if a counter has wrapped multiple times, or the case where a counter has wrapped and happens to be greater than the first reading. Since you said this was an embedded system and your description makes your "counter" sound like a clock, see if you can set an interrupt to fire whenever the clock reaches zero (so that you will get an interrupt every time the clock resets). When this interrupt fires, increment a separate counter. This should effectively add extra precision to your clock and allow your counter to wrap without causing problems.

Solution 2

It will not matter so long as the difference between the start and end count is less than 232, and unsigned 32bit arithmetic is performed, even if the count value spans the wrap-point. (If you use signed arithmetic, the span must be less than 232/2)

For example:

Start count: 0xfffffff
End Count:   0x00000002 (incremented through 0,1,2 - i.e. three counts)

End - Start == 0x00000002 - 0xfffffff == 0x00000003

So the right answer is achieved so long as the counter is the bit width of a built-in integer type, and that type is used. Where perhaps a counter register is not the width of a built-in integer type, you can achieve the same effect by masking the higher order "overflow" bits.

If you need the larger count for other reasons or if the difference between successive timestamps is too large, then you can simply use another integer that is incremented when the lower-order counter wraps. This integer will form the high order bits of a larger integer, so the LSB of the second integer is the 33rd bit of this larger integer.

Solution 3

Cast the result of unsigned subtraction to signed and compare to zero. Should handle overflow when you check it often enough (and your timeout is less than half the range of your timer).

uint32_t timer( void);             // Returns the current time value
uint32_t timeout;

timeout = timer() + offset;

// wait until timer() reaches or exceeds timeout value
while ((int32_t)(timeout - timer()) > 0);

Solution 4

If you use unsigned variables to store your counter and timer expiry time, then you can simply use this test:

if (current_time - expiry_time < 0x80000000UL)
    /* timer has expired */

This assumes that you test for expiry at least once every 0x80000000 ticks, and that your longest timer is set to expire less than 0x80000000 ticks into the future.

Solution 5

The question is a bit vague. One possibility is to set a flag when you first notice that the time has elapsed. A surefire way would be add a second counter which is incremented when the first counter overflows. This will in effect create a 64 bit counter which won't overflow.

Share:
26,218
jramirez
Author by

jramirez

Was born and still alive.

Updated on July 09, 2022

Comments

  • jramirez
    jramirez almost 2 years

    I need to deal with a counter that gives me ticks for my application. The counter is 32bits so what I need to know is how to deal with it when it wraps. for example:

    I have a function that returns a (timestamp + shifttime) and I have another function that will return 1 or 0 depending whether or not the time has elapsed, but there is a possibility that that my counter will wrap how, do I deal with this?


    Thanks a lot for all the responses guys. I will give more detail in this edit.

    I am using the STM32 Cortex-M3. I wanna use the RTC counter to use it as the tick for my application to schedule tasks that need to happen at certain intervals. The RTC can generate an overflow interrupt so it's not a problem to detect the interrupt. the main problem that I have (or at least I think is a problem) is when certain tasks gets a (timestamp+shift) i.e.

    int main( void )
    {
        FlashLedTimeStamp = ReturnCounter( 20 );  // currentcounter value + a shift of 20
        StatusLedTimeStamp = ReturnCounter( 3 );  // currentcounter value + a shift of 3
    
    // then later on ....
    while(1)
    {
        /* other tasks could go here */
    
        if( HasTimeElapsed( FlashLedTimeStamp ) )
        {
           /* do something and get another timestamp value */
           FlashLedTimeStamp = ReturnCounter( 20 );  // currentcounter value + a shift of 20
        }
        
        if( HasTimeElapsed( StatusLedTimeStamp ) )
        {
           /* do something and get another timestamp value */
           FlashLedTimeStamp = StatusLedTimeStamp( 3 );  // currentcounter value + a shift of 3
        }
    }   
    }
    

    Lets assume that my RTC counter is only 8 bits long to make the math easy.

    If my current counter is at 250 when I get my timestamps that means that FlashLedTimeStamp = 14 and StatusLedTimeStamp = 253 how would I check to see that FlashLedTimeStamp has expired??

    keep in mind that I don't necessarily check all the time to see what the current counter is and whether or not certain timestamps have expired. I hope this makes it clear what the problem I have is.