// Example - 32MHz with DFLL using external 32.768kHz // DFLL is run-time mechanism calibrating internal RC oscillator (2MHz or 32MHz) against another clock signal // typicaly against external 32.768 kHz Xtal Oscillator or internal 32.768kHz RC oscillator. Possible against external arbitrary clock (XTAL1). // if using external clock, load proper value to COMP register // bug in Atmel, Errata says that both oscillators (32MHz and 2MHz) must be started // and both of them must be set up for run time calibration (DFLL) to proper operation of DFLL !!! // DFLL clock has large jitter ... void clock_init(void){ OSC.XOSCCTRL = OSC_XOSCSEL_32KHz_gc; // external xtal is 32k OSC.CTRL |= OSC_XOSCEN_bm | OSC_RC32MEN_bm | OSC_RC2MEN_bm; // start external xtal oscillator and both internal RC oscillators while (!(OSC.STATUS & OSC_XOSCRDY_bm) || !(OSC.STATUS & OSC_RC32MRDY_bm) || !(OSC.STATUS & OSC_RC2MRDY_bm)){}; // wait until all oscillators stabilize OSC.DFLLCTRL = 0b11; // bug, DFLL must be set up for both internal oscillators (select external 32kHz as reference for them) DFLLRC32M.CTRL = DFLL_ENABLE_bm; // start DFLL (runtime calibration of 32MHz oscillator) DFLLRC2M.CTRL = DFLL_ENABLE_bm; // start DFLL (runtime calibration of 2MHz oscillator) CCP=CCP_IOREG_gc; // unlock writing to CLK_CTRL CLK.CTRL = CLK_SCLKSEL_RC32M_gc; // select internal 32 MHZ (calibrated) as system clock // clockout at PC7 - debug purposes PORTC.DIR = PIN7_bm; PORTC.PIN7CTRL = PORT_OPC_TOTEM_gc ; // clockout / eventout / RTCout settings PORTCFG.CLKEVOUT = PORTCFG_EVOUT_OFF_gc | PORTCFG_CLKOUT_PC7_gc ; //CCP=CCP_IOREG_gc; // unlock writing to CLK.PSCTRL (optional) //CLK.PSCTRL = CLK_PSADIV_1_gc; // Set up prescalers (optional) }