Clarifications for Write-Synchronized Register

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Context: chip=SAMD21J, peripheral=RTC, clock=XOSC32

 

It's been a good year since I've played with my samd21 code, I'm revisiting it now, and finding that I have some questions about some of the register semantics. In particular the RTC->MODE0.COMP0 register. Said register is Write-Synchronized. I have two basic questions/clarifications regarding this.

 

1. What are the implications during the time it takes to synchronize? Because my RTC is hooked to XOSC32 clock, I'm measuring it takes ~200 µsec to wait for the STATUS.SYNCBUSY to clear after I've set the COMP0 register. I understand that until it is synced, it wouldn't be a valid compare value. So if I set the COMP0 to COUNT + 1 less than 200 µsec before the interrupt, I'd likely miss the match. Are there other implications? What if I try to read/set this register again before it has sync'ed. Will Bad Things (TM) happen? RTC hang or something? Or is it just that it will be invalid. What about other RTC registers during this period? The same? Or just this register?

 

2. Optimistic Check vs Pessimistic Check - I see two ways to protect against interacting with the register while it is syncing. The first is a pessimistic (and deterministic) approach:

RTC->MODE0.COMP[0].reg =newValue; // SET COMP0 with the newValue
while (RTC->MODE0.STATUS.bit.SYNCBUSY) { } // NOW WAIT UNTIL SYNC CLEARS SO WE CAN ASSERT THAT WE PROCEEDED FROM THIS POINT IN A KNOWN STATE

But I could be more optimistic about it and reverse the order like this:

while (RTC->MODE0.STATUS.bit.SYNCBUSY) { } // MAKE SURE THAT THERE ISN'T ANY SYNCING GOING ON, THIS USUALLY FALLS THROUGH FAST
RTC->MODE0.COMP[0].reg = newValue // MAKE THE CHANGE AND CHARGE ON 

Is the second idiom correct/acceptable as long as I'm consistent in using it (and recognizing I may or may not spend time waiting for it to sync)?

Last Edited: Wed. Sep 23, 2020 - 06:45 PM