SpenceKonde / megaTinyCore

Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!
Other
560 stars 146 forks source link

RTC_init() will only work in setup() , loop forever if run in main() #420

Closed cpainchaud closed 3 years ago

cpainchaud commented 3 years ago
void RTC_init()
{
  /* Initialize RTC: */
  while (RTC.STATUS > 0  )
  {
    ;                                 /* Wait for all register to be synchronized */
  }
  RTC.CLKSEL = RTC_CLKSEL_INT1K_gc;   

  RTC.PITINTCTRL = RTC_PI_bm;           /* PIT Interrupt: enabled */

  RTC.PITCTRLA = RTC_PERIOD_CYC4096_gc 
                 | RTC_PITEN_bm;
}

while (RTC.STATUS > 0) is never ending loop when RTC_init() is run from main() instead of setup();

cpainchaud commented 3 years ago

I guess both options should be given but for sure it's now clear that RTC based one will be way more flexible.

cpainchaud commented 3 years ago

one last question: while (RTC.STATUS > 0 ); this will wait for registers sync. Does it happen at CPU clock timing or RTC/PIT clock timing? I am trying to evaluate the potential lag introduced by this statement which cannot be avoided (going to sleep before waiting for this sync to happen will make CPU sleep forever)

SpenceKonde commented 3 years ago

It's like all the rest of the 2-flipflop-synchronizers; it's clocked ffrom CLK_RTC; Takes 2-3 (depending on where in the clock the input changes state) RTC clocks.

SpenceKonde commented 3 years ago

Closing - no defect in core. Feel free to open issue if there are further problems - though consider using discussion if the matter at hand appears to be the weird behavior of the chip itself, as opposed to the core.