u32 RTC_AtomicGetCounter(void) //assumes RTC_FLAG_SECF not cleared by any interrupts { RTC->CRL &= ~RTC_FLAG_SEC; //clear flag that gets set when counter is updated u32 counter = RTC_GetCounter(); //read counter if (RTC->CRL & RTC_FLAG_SEC) //if counter updated during read it may be unreliable { counter = RTC_GetCounter(); //re-read counter (assumes counter not updated again THIS SOON) } return counter; }