3 Activating and Testing the Auto-Tune Feature

In order to use the auto-tune feature, a high precision 32.768 kHz external crystal oscillator is required.

The bits that need to be set in order to enable this feature are under Configuration Change Protection. These are I/O registers, so the I/O signature, 0xD8, needs to be written in the Configuration Change Protection (CPU.CCP) register.

In order to write to a register under I/O Configuration Change Protection, the _PROTECTED_WRITE macro must be used, which will write the signature to the CPU.CCP register and then the value to the desired register:

_PROTECTED_WRITE (register, value);

Replace ‘register’ with the register that needs to be written and ‘value’ with the value that needs to be written to it.

Note: The compiler optimization level must be set to at least O1 in the Atmel Studio project settings. Oterwise, the macro will not be able to write the value.

This crystal oscillator needs to be enabled, by writing the ENABLE bit from the 32.768 kHz crystal oscillator Control A (CLKCTRL.XOSC32KCTRLA) register to ‘1’. The following line of code enables the oscillator:

_PROTECTED_WRITE (CLKCTRL.XOSC32KCTRLA, CLKCTRL_ENABLE_bm);

After this is done, the auto-tune feature needs to be enabled by setting the AUTOTUNE bit (bit 0) in the internal high-frequency oscillator Control A (CLKCTRL.OSCHFCTRLA) register. This register has two other features. The RUNSTDBY bit (bit 7) activates the running in Standby mode and the FRQSEL bitfield (bit 2 to bit 5) sets the frequency of the oscillator. The next line of code enables the auto-tune feature and sets the frequency to 1 MHz. The frequency can be changed by giving different values that will be detailed later in this document:

_PROTECTED_WRITE (CLKCTRL.OSCHFCTRLA, (CLKCTRL_FREQSEL_1M_gc | CLKCTRL_AUTOTUNE_bm));

The internal high-frequency oscillator must be selected as the main clock. This is done by writing the value that selects the internal high-frequency oscillator to the CLKSEL bit field in the Main Clock Control A (CLKCTRL.MCLKCTRLA) register. Setting the CLKOUT bit in this register outputs the clock signal on the PA7 pin. The next two lines of code show how to select the internal high-frequency oscillator and how to enable the clock output signal:

_PROTECTED_WRITE (CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_OSCHF_gc);
_PROTECTED_WRITE (CLKCTRL.MCLKCTRLA, (CLKCTRL_CLKSEL_OSCHF_gc | CLKCTRL_CLKOUT_bm));
Note: The default startup oscillator is configured using FUSE.OSCCFG fuse. If the internal high-frequency oscillator is already set as default, then this step may be omitted. For more details on Fuse settings, see ‘FUSE - Configuration and User Fuses’ chapter of the AVR128DA48 Data Sheet.

If the only requirement is the turning on of the auto-tune feature, the following code provides this functionality:

_PROTECTED_WRITE (CLKCTRL.XOSC32KCTRLA, CLKCTRL_ENABLE_bm);
_PROTECTED_WRITE (CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_OSCHF_gc);
_PROTECTED_WRITE (CLKCTRL.OSCHFCTRLA, (CLKCTRL_FREQSEL_1M_gc | CLKCTRL_AUTOTUNE_bm));
Note: The required frequency is also set through the OSCHFCTRLA register, so the value needs to be changed to the desired one. The register setting from above sets it to 1 MHz.

In order to highlight the auto-tune feature, several tests were performed at different frequencies and the results are provided in each use case chapter.

The image captures were done using a Tektronix MDO3024 Mixed Domain Oscilloscope with the following settings:
  • Gating set to Screen
  • High-Low Method set to Histogram
  • Coupling in DC
  • Bandwidth Full

The testing method involves setting up the register for the correct frequency and using a button that activates the auto-tune feature, waits for one second, and then stops it. The tuning (CLKCTRL.OSCHFTUNE) register can then be read in Debugging mode to check the value put there by the auto-tune feature.

The mean and standard deviation values were calculated using 100 samples. The mean is the value that best indicates the clock frequency, because at high frequencies, random noise from the environment makes the instant reading inaccurate.