Handling Timer Overflow Interrupts

I'm having a hard time wrapping my head around what I'm trying to accomplish....

I'm using Timer1 in Input Capture Mode, to time an external event. I clear the timer, set it up in Input Capture Mode, set the pre-scaler to ClkIO/1024, and enable Overflow interrupts, as the event I'm timing can take up to 10 seconds, which means the timer may overflow twice. I maintain a software count, in a uint32_t that is increment by 65536 every time an overflow occurs. When the capture occurs, I can then read the timer count, add it to the software count, and have the total event time. What has me a bit stumped is how to handle the corner cases where the overflow and capture occur at nearly the same time. How do I know whether the capture occurred before or after the overflow? Consider this scenario:

Capture occurs at a count of 0xffff. Overflow interrupt occurs AFTER the capture, so the overflow handler SHOULD NOT increment the software count to account for the overflow.

Or, capture occurs at a count of 0x0000. Overflow interrupt occurs AFTER the capture, so the overflow handler SHOULD increment the software count to account for the overflow.

I think the correct logic would then be:

  1. Do NOT update the software count if TIFR1 ICF1 is set, AND Timer1Count is less than perhaps 100
  2. DO update the software count if TIFR1 ICF1 is clear OR Timer1Count is greater than perhaps 100

Is that correct and robust?

Regards,
Ray L.

Nick Gammon does it this way Gammon Forum : Electronics : Microprocessors : Timers and counters

He is checking the TOV1 flag instead of the ICF1 flag. The TOV1 flag will be cleared if the the OVF vector has been serviced and the overflow count variable incremented.

Nick seems to be doing essentially what I'm suggesting, in reverse:

  if ((TIFR1 & bit (TOV1)) && timer1CounterValue < 0x7FFF)
    overflowCopy++;

He does this inside the Input Capture interrupt handler, which I'm not using, because it conflicts with another library.
Here's what I came up with:

uint32_t Time1Count = 0;

ISR(TIMER1_OVF_vect)
{		
    union cnt
    {
        uint16_t uint16;
	uint8_t  uint8[2];
    } Capture1Cnt;
    Capture1Cnt.uint8[1] = ICR1H;
    Capture1Cnt.uint8[0] = ICR1L;
    if ((!(TIFR1 & bit(ICF1))) || (Capture1Cnt.uint16 < 1024))
    {
        // Count this overflow, unless capture has already happened, 
	// Or, Capture1Cnt is small, which means capture occurred AFTER 
	// overflow
	Timer1Count += 0x10000UL;
    }
    if (TIFR1 & bit(ICF1))
    {
	// Capture has happened, so disable Timer1 Overflow Interrupt
	TIMSK1 &= ~bit(TOIE1);
    }
}

Regards,
Ray L.