6 RTC with XOSCHF

To use XOSCHF as clock source for the Real-Time Counter (RTC), first enable the XOSCHF similarly to the first two use cases. The crystal or input clock frequency can be up to ¼ of the main clock frequency.

Warning: The 16 MHz crystal on the ATtiny1627 Curiosity Nano will generate a too high frequency for this example. Replace the crystal or use an external clock with maximum ¼ of the main clock frequency to run within spec.
	/* Enable crystal oscillator with frequency range 8 MHz and 1K cycles start-up time */
	ccp_write_io((uint8_t *) &CLKCTRL.XOSCHFCTRLA, CLKCTRL_RUNSTDBY_bm
	             | CLKCTRL_CSUTHF_1K_gc
	             | CLKCTRL_FRQRANGE_8M_gc
	             | CLKCTRL_SELHF_CRYSTAL_gc
	             | CLKCTRL_ENABLE_bm);

	/* Confirm crystal oscillator start-up */
	while(!(CLKCTRL.MCLKSTATUS & CLKCTRL_EXTS_bm))
	{
		;
	}

	/* Clear RUNSTDBY for power save during sleep */
	ccp_write_io((uint8_t *) &CLKCTRL.XOSCHFCTRLA, 
	             CLKCTRL.XOSCHFCTRLA & ~CLKCTRL_RUNSTDBY_bm);

To select the XOSCHF as clock source for RTC, the EXTCLK setting must be written to the Clock Select (RTC.CLKSEL) register.

Figure 6-1. RTC.CLKSEL – Select XOSCHF as source

	/* Configure RTC to use XOSCHF as source */
	RTC.CLKSEL = RTC_CLKSEL_EXTCLK_gc;

Now the RTC can be configured and enabled with XOSCHF as clock source.

The code for this example is available in the RTC-with-XOSCHF folder in these github repositories: