using XOSC clock source

Hello, in a previous post, I found that the 48MHz clock source was a bit too error prone for very exact timing, but that using XOSC32K was a great way to get really accurate timing, with the constraint of that relatively low frequency.

I noticed that the SAMD21G18A (used on the Zero), does have another external clock source, which can take a crystal of 0.4-32MHz. I figured this could be a great way to get accurate timing at a much higher frequency. So I developed my own board with the usual 32KHz crystal as well as a 25MHz crystal between pins D2 (PA14) and D5(PA15) (according to the datasheet those are the input/output for XOSC), in the hopes of testing this out.

I was wondering if anyone else has used XOSC and can point me in the right direction as far as using it.

1 Like

I actually resolved this one. One can setup XOSC with:

   //this sets up the external OSC.  
   SYSCTRL->XOSC.reg = 0
   | SYSCTRL_XOSC_XTALEN
   | SYSCTRL_XOSC_STARTUP(0xF) // 32768
   | SYSCTRL_XOSC_AMPGC
   | SYSCTRL_XOSC_ENABLE
   ;
 while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);

then it can be attached to a clock like:

   REG_GCLK_GENCTRL = GCLK_GENCTRL_IDC |           // Set the duty cycle to 50/50 HIGH/LOW
                      GCLK_GENCTRL_GENEN |         // Enable GCLK4
//                      GCLK_GENCTRL_SRC_XOSC |
                      GCLK_GENCTRL_ID(4);          // Select GCLK4
   while (GCLK->STATUS.bit.SYNCBUSY);              // Wait for synchronization

Now that it's a clocked signal you can divide it down/use it like any other clock signal.

So if you need tight high frequency timing (up to 32MHz) just throw a crystal between PA14 and PA15 (with load caps), add the above code to your setup routine and you should be good to go.

I tested this with the SAMD21G18A and a 25MHz, 18pF crystal; and it worked perfectly

1 Like

You can actually use the 32 kHz XOSC to be the reference of the 48 MHz DPLL that feeds the CPU clock.

The original 48MHz you might have had before uses the internal 8MHz

1 Like